mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-27 09:35:56 -05:00
Reduxified
This commit is contained in:
parent
fd1401fe1b
commit
6f39466d9f
1 changed files with 18 additions and 7 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue