mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-20 03:15:29 -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 keyMirror = require('keymirror');
|
||||||
|
var defaultsDeep = require('lodash.defaultsdeep');
|
||||||
|
|
||||||
var api = require('../mixins/api.jsx').api;
|
var api = require('../mixins/api.jsx').api;
|
||||||
var tokenActions = require('./token.js');
|
var tokenActions = require('./token.js');
|
||||||
|
|
||||||
var Types = keyMirror({
|
var Types = keyMirror({
|
||||||
SET_SESSION: null,
|
SET_SESSION: null,
|
||||||
SET_SESSION_ERROR: null
|
SET_SESSION_ERROR: null,
|
||||||
|
SET_STATUS: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports.Status = keyMirror({
|
module.exports.Status = keyMirror({
|
||||||
|
@ -25,7 +27,9 @@ module.exports.sessionReducer = function (state, action) {
|
||||||
}
|
}
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case Types.SET_SESSION:
|
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:
|
case Types.SET_SESSION_ERROR:
|
||||||
// TODO: do something with action.error
|
// TODO: do something with action.error
|
||||||
return state;
|
return state;
|
||||||
|
@ -41,21 +45,27 @@ module.exports.setSessionError = function (error) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.setSession = function (status, results) {
|
module.exports.setSession = function (results) {
|
||||||
return {
|
return {
|
||||||
type: Types.SET_SESSION,
|
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 () {
|
module.exports.refreshSession = function () {
|
||||||
return function (dispatch) {
|
return function (dispatch) {
|
||||||
dispatch(module.exports.setSession(module.exports.Status.NOT_FETCHED, {}));
|
dispatch(module.exports.setStatus(module.exports.Status.FETCHING));
|
||||||
api({
|
api({
|
||||||
host: '',
|
host: '',
|
||||||
uri: '/session/'
|
uri: '/session/'
|
||||||
}, function (err, body) {
|
}, function (err, body) {
|
||||||
dispatch(module.exports.setSession(module.exports.Status.FETCHING, {}));
|
|
||||||
if (err) return dispatch(module.exports.setSessionError(err));
|
if (err) return dispatch(module.exports.setSessionError(err));
|
||||||
|
|
||||||
if (typeof body !== 'undefined') {
|
if (typeof body !== 'undefined') {
|
||||||
|
@ -63,7 +73,8 @@ module.exports.refreshSession = function () {
|
||||||
return window.location = body.url;
|
return window.location = body.url;
|
||||||
} else {
|
} else {
|
||||||
dispatch(tokenActions.getToken());
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue