mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-06-23 08:58:39 -04:00
Hook (almost) everything up to Redux
This commit is contained in:
@ -1,8 +1,26 @@
|
||||
var assign = require('object-assign');
|
||||
|
||||
var AccountConstants = require('../constants/AccountConstants');
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
module.exports = function(state = {}, action) {
|
||||
function accountChildren(accounts) {
|
||||
var children = {};
|
||||
for (var accountId in accounts) {
|
||||
if (accounts.hasOwnProperty(accountId)) {
|
||||
var parentAccountId = accounts[accountId].ParentAccountId;
|
||||
if (!children.hasOwnProperty(parentAccountId))
|
||||
children[parentAccountId] = [];
|
||||
if (!children.hasOwnProperty(accountId))
|
||||
children[accountId] = [];
|
||||
children[parentAccountId].push(accountId);
|
||||
}
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
const initialState = {map: {}, children: {}};
|
||||
|
||||
module.exports = function(state = initialState, action) {
|
||||
switch (action.type) {
|
||||
case AccountConstants.ACCOUNTS_FETCHED:
|
||||
var accounts = {};
|
||||
@ -10,17 +28,29 @@ module.exports = function(state = {}, action) {
|
||||
var account = action.accounts[i];
|
||||
accounts[account.AccountId] = account;
|
||||
}
|
||||
return accounts;
|
||||
return {
|
||||
map: accounts,
|
||||
children: accountChildren(accounts)
|
||||
};
|
||||
case AccountConstants.ACCOUNT_CREATED:
|
||||
case AccountConstants.ACCOUNT_UPDATED:
|
||||
var account = action.account;
|
||||
return assign({}, state, {
|
||||
var accounts = assign({}, state.map, {
|
||||
[account.AccountId]: account
|
||||
});
|
||||
return {
|
||||
map: accounts,
|
||||
children: accountChildren(accounts)
|
||||
};
|
||||
case AccountConstants.ACCOUNT_REMOVED:
|
||||
var newstate = assign({}, state);
|
||||
delete newstate[action.accountId];
|
||||
return newstate;
|
||||
var accounts = assign({}, state.map);
|
||||
delete accounts[action.accountId];
|
||||
return {
|
||||
map: accounts,
|
||||
children: accountChildren(accounts)
|
||||
};
|
||||
case UserConstants.USER_LOGGEDOUT:
|
||||
return initialState;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
17
js/reducers/ErrorReducer.js
Normal file
17
js/reducers/ErrorReducer.js
Normal file
@ -0,0 +1,17 @@
|
||||
var ErrorConstants = require('../constants/ErrorConstants');
|
||||
|
||||
var Error = require('../models').Error;
|
||||
|
||||
module.exports = function(state = new Error(), action) {
|
||||
switch (action.type) {
|
||||
case ErrorConstants.ERROR_AJAX:
|
||||
case ErrorConstants.ERROR_SERVER:
|
||||
case ErrorConstants.ERROR_CLIENT:
|
||||
case ErrorConstants.ERROR_USER:
|
||||
return action.error;
|
||||
case ErrorConstants.CLEAR_ERROR:
|
||||
return new Error();
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
@ -1,9 +1,17 @@
|
||||
var Redux = require('redux');
|
||||
|
||||
var UserReducer = require('./UserReducer');
|
||||
var SessionReducer = require('./SessionReducer');
|
||||
var AccountReducer = require('./AccountReducer');
|
||||
var SecurityReducer = require('./SecurityReducer');
|
||||
var SelectedAccountReducer = require('./SelectedAccountReducer');
|
||||
var ErrorReducer = require('./ErrorReducer');
|
||||
|
||||
module.exports = Redux.combineReducers({
|
||||
user: UserReducer,
|
||||
session: SessionReducer,
|
||||
accounts: AccountReducer,
|
||||
selectedAccount: SelectedAccountReducer
|
||||
securities: SecurityReducer,
|
||||
selectedAccount: SelectedAccountReducer,
|
||||
error: ErrorReducer
|
||||
});
|
||||
|
30
js/reducers/SecurityReducer.js
Normal file
30
js/reducers/SecurityReducer.js
Normal file
@ -0,0 +1,30 @@
|
||||
var assign = require('object-assign');
|
||||
|
||||
var SecurityConstants = require('../constants/SecurityConstants');
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
module.exports = function(state = {}, action) {
|
||||
switch (action.type) {
|
||||
case SecurityConstants.SECURITIES_FETCHED:
|
||||
var securities = {};
|
||||
for (var i = 0; i < action.securities.length; i++) {
|
||||
var security = action.securities[i];
|
||||
securities[security.SecurityId] = security;
|
||||
}
|
||||
return securities;
|
||||
case SecurityConstants.SECURITY_CREATED:
|
||||
case SecurityConstants.SECURITY_UPDATED:
|
||||
var security = action.security;
|
||||
return assign({}, state, {
|
||||
[security.SecurityId]: security
|
||||
});
|
||||
case SecurityConstants.SECURITY_REMOVED:
|
||||
var newstate = assign({}, state);
|
||||
delete newstate[action.securityId];
|
||||
return newstate;
|
||||
case UserConstants.USER_LOGGEDOUT:
|
||||
return {};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
@ -1,4 +1,5 @@
|
||||
var AccountConstants = require('../constants/AccountConstants');
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
module.exports = function(state = -1, action) {
|
||||
switch (action.type) {
|
||||
@ -14,6 +15,8 @@ module.exports = function(state = -1, action) {
|
||||
return state;
|
||||
case AccountConstants.ACCOUNT_SELECTED:
|
||||
return action.accountId;
|
||||
case UserConstants.USER_LOGGEDOUT:
|
||||
return -1;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
14
js/reducers/SessionReducer.js
Normal file
14
js/reducers/SessionReducer.js
Normal file
@ -0,0 +1,14 @@
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
var Session = require('../models').Session;
|
||||
|
||||
module.exports = function(state = new Session(), action) {
|
||||
switch (action.type) {
|
||||
case UserConstants.USER_LOGGEDIN:
|
||||
return action.session;
|
||||
case UserConstants.USER_LOGGEDOUT:
|
||||
return new Session();
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
15
js/reducers/UserReducer.js
Normal file
15
js/reducers/UserReducer.js
Normal file
@ -0,0 +1,15 @@
|
||||
var UserConstants = require('../constants/UserConstants');
|
||||
|
||||
var User = require('../models').User;
|
||||
|
||||
module.exports = function(state = new User(), action) {
|
||||
switch (action.type) {
|
||||
case UserConstants.USER_FETCHED:
|
||||
case UserConstants.USER_UPDATED:
|
||||
return action.user;
|
||||
case UserConstants.USER_LOGGEDOUT:
|
||||
return new User();
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user