From 6f39466d9f60c01b5a41eef8a5233d1858285f87 Mon Sep 17 00:00:00 2001 From: Connor Hudson Date: Wed, 1 Jun 2016 17:07:21 -0400 Subject: [PATCH] Reduxified --- src/redux/session.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/redux/session.js b/src/redux/session.js index 2c2a04b58..99f4f43a9 100644 --- a/src/redux/session.js +++ b/src/redux/session.js @@ -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; } }