http: add
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
const http = require('../../../http')
|
||||
|
||||
module.exports = {
|
||||
state: {
|
||||
title: 'Button pushing machine 3000'
|
||||
},
|
||||
reducers: {
|
||||
set: function (action, state) {
|
||||
return { 'api:title': action.payload }
|
||||
}
|
||||
},
|
||||
effects: {
|
||||
good: performGoodRequest,
|
||||
bad: performBadRequest
|
||||
}
|
||||
}
|
||||
|
||||
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' })
|
||||
if (res.statusCode !== 200) {
|
||||
const message = (body && body.message)
|
||||
? body.message
|
||||
: 'unknown server error'
|
||||
return send('error', { payload: message })
|
||||
}
|
||||
if (!body) {
|
||||
return send('error', { payload: 'fatal: no body received' })
|
||||
}
|
||||
send('api:set', { payload: body.title })
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Create a new error. Errors are pushed into an array so they can be
|
||||
// queued. We're doing a bit of timestamp trickery here so each error
|
||||
// is shown for exactly one second before being dropped. Probably
|
||||
// useful for real world scenarios; especially when coupled to
|
||||
// fancy CSS animation thingies
|
||||
|
||||
// Each error is displayed 1 second
|
||||
const ERROR_TIMEOUT = 1000
|
||||
|
||||
module.exports = {
|
||||
state: {
|
||||
error: [],
|
||||
errorTimeDone: null,
|
||||
triggerTime: null
|
||||
},
|
||||
reducers: {
|
||||
error: function (action, state) {
|
||||
const now = Date.now()
|
||||
const timeDone = state.errorTimeDone
|
||||
const newTimestamp = (timeDone && timeDone >= now)
|
||||
? timeDone + ERROR_TIMEOUT
|
||||
: now + ERROR_TIMEOUT
|
||||
|
||||
return {
|
||||
error: state.error.concat(action.payload),
|
||||
errorTimeDone: newTimestamp
|
||||
}
|
||||
},
|
||||
'error:delete': function (action, state) {
|
||||
state.error.shift()
|
||||
return { error: state.error }
|
||||
}
|
||||
},
|
||||
effects: {
|
||||
error: function (action, state, send) {
|
||||
const timeout = state.errorTimeDone - Date.now()
|
||||
setTimeout(function () {
|
||||
send('error:delete')
|
||||
}, timeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user