examples/http: update for ns
This commit is contained in:
+13
-26
@@ -1,45 +1,32 @@
|
||||
const http = require('../../../http')
|
||||
|
||||
module.exports = {
|
||||
namespace: 'api',
|
||||
state: {
|
||||
title: 'Button pushing machine 3000'
|
||||
},
|
||||
reducers: {
|
||||
set: function (action, state) {
|
||||
return { 'api:title': action.payload }
|
||||
}
|
||||
set: (action, state) => ({ 'title': action.payload })
|
||||
},
|
||||
effects: {
|
||||
good: performGoodRequest,
|
||||
bad: performBadRequest
|
||||
good: (action, state, send) => request('/good', send),
|
||||
bad: (action, state, send) => request('/bad', send)
|
||||
}
|
||||
}
|
||||
|
||||
function performGoodRequest (action, state, send) {
|
||||
http('/good', { json: true }, function (err, res, body) {
|
||||
if (err) return send('error', { payload: 'HTTP error' })
|
||||
if (res.statusCode !== 200) {
|
||||
return send('error', { payload: body.payload })
|
||||
}
|
||||
if (!body) {
|
||||
return send('error', { payload: 'fatal: no body received' })
|
||||
}
|
||||
send('api:set', { payload: body.message })
|
||||
})
|
||||
}
|
||||
|
||||
function performBadRequest (action, state, send) {
|
||||
http('/bad', { json: true }, function (err, res, body) {
|
||||
if (err) return send('error', { payload: 'HTTP error' })
|
||||
function request (uri, send) {
|
||||
http(uri, { json: true }, function (err, res, body) {
|
||||
if (err) return send('app:error', { payload: 'HTTP error' })
|
||||
if (res.statusCode !== 200) {
|
||||
const message = (body && body.message)
|
||||
? body.message
|
||||
: 'unknown server error'
|
||||
return send('error', { payload: message })
|
||||
? body.message
|
||||
: 'unknown server error'
|
||||
return send('app:error', { payload: message })
|
||||
}
|
||||
if (!body) {
|
||||
return send('error', { payload: 'fatal: no body received' })
|
||||
console.log('req made!')
|
||||
return send('app:error', { payload: 'fatal: no body received' })
|
||||
}
|
||||
send('api:set', { payload: body.title })
|
||||
send('api:set', { payload: body.message || body.title })
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
const ERROR_TIMEOUT = 1000
|
||||
|
||||
module.exports = {
|
||||
namespace: 'app',
|
||||
state: {
|
||||
error: [],
|
||||
errorTimeDone: null,
|
||||
@@ -33,9 +34,9 @@ module.exports = {
|
||||
},
|
||||
effects: {
|
||||
error: function (action, state, send) {
|
||||
const timeout = state.errorTimeDone - Date.now()
|
||||
const timeout = state.app.errorTimeDone - Date.now()
|
||||
setTimeout(function () {
|
||||
send('error:delete')
|
||||
send('app:error:delete')
|
||||
}, timeout)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user