mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-07-01 20:08: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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user