Reduxified

This commit is contained in:
Connor Hudson 2016-06-01 17:07:21 -04:00
parent fd1401fe1b
commit 6f39466d9f

View file

@ -1,11 +1,13 @@
var keyMirror = require('keymirror');
var defaultsDeep = require('lodash.defaultsdeep');
var api = require('../mixins/api.jsx').api;
var tokenActions = require('./token.js');
var Types = keyMirror({
SET_SESSION: null,
SET_SESSION_ERROR: null
SET_SESSION_ERROR: null,
SET_STATUS: null,
});
module.exports.Status = keyMirror({
@ -25,7 +27,9 @@ module.exports.sessionReducer = function (state, action) {
}
switch (action.type) {
case Types.SET_SESSION:
return action.session;
return defaultsDeep({"results": action.session}, state);
case Types.SET_STATUS:
return defaultsDeep({"status": action.status}, state)
case Types.SET_SESSION_ERROR:
// TODO: do something with action.error
return state;
@ -41,21 +45,27 @@ module.exports.setSessionError = function (error) {
};
};
module.exports.setSession = function (status, results) {
module.exports.setSession = function (results) {
return {
type: Types.SET_SESSION,
session: {'status': status,'results': results}
session: results
};
};
module.exports.setStatus = function(status){
return {
type: Types.SET_STATUS,
status: status
};
}
module.exports.refreshSession = function () {
return function (dispatch) {
dispatch(module.exports.setSession(module.exports.Status.NOT_FETCHED, {}));
dispatch(module.exports.setStatus(module.exports.Status.FETCHING));
api({
host: '',
uri: '/session/'
}, function (err, body) {
dispatch(module.exports.setSession(module.exports.Status.FETCHING, {}));
if (err) return dispatch(module.exports.setSessionError(err));
if (typeof body !== 'undefined') {
@ -63,7 +73,8 @@ module.exports.refreshSession = function () {
return window.location = body.url;
} else {
dispatch(tokenActions.getToken());
dispatch(module.exports.setSession(module.exports.Status.FETCHED, body));
dispatch(module.exports.setSession(body));
dispatch(module.exports.setStatus(module.exports.Status.FETCHED));
return;
}
}