2016-05-13 12:24:51 +07:00
|
|
|
// 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 = {
|
2016-05-23 10:44:32 +09:00
|
|
|
namespace: 'app',
|
2016-05-13 12:24:51 +07:00
|
|
|
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) {
|
2016-05-23 10:44:32 +09:00
|
|
|
const timeout = state.app.errorTimeDone - Date.now()
|
2016-05-13 12:24:51 +07:00
|
|
|
setTimeout(function () {
|
2016-05-23 10:44:32 +09:00
|
|
|
send('app:error:delete')
|
2016-05-13 12:24:51 +07:00
|
|
|
}, timeout)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|