2016-10-03 19:49:15 -04:00
|
|
|
var assign = require('object-assign');
|
|
|
|
|
|
|
|
var AccountConstants = require('../constants/AccountConstants');
|
2016-10-05 13:36:47 -04:00
|
|
|
var UserConstants = require('../constants/UserConstants');
|
2016-10-03 19:49:15 -04:00
|
|
|
|
2016-10-05 13:36:47 -04:00
|
|
|
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) {
|
2016-10-03 19:49:15 -04:00
|
|
|
switch (action.type) {
|
|
|
|
case AccountConstants.ACCOUNTS_FETCHED:
|
|
|
|
var accounts = {};
|
|
|
|
for (var i = 0; i < action.accounts.length; i++) {
|
|
|
|
var account = action.accounts[i];
|
|
|
|
accounts[account.AccountId] = account;
|
|
|
|
}
|
2016-10-05 13:36:47 -04:00
|
|
|
return {
|
|
|
|
map: accounts,
|
|
|
|
children: accountChildren(accounts)
|
|
|
|
};
|
2016-10-03 19:49:15 -04:00
|
|
|
case AccountConstants.ACCOUNT_CREATED:
|
|
|
|
case AccountConstants.ACCOUNT_UPDATED:
|
|
|
|
var account = action.account;
|
2016-10-05 13:36:47 -04:00
|
|
|
var accounts = assign({}, state.map, {
|
2016-10-03 19:49:15 -04:00
|
|
|
[account.AccountId]: account
|
|
|
|
});
|
2016-10-05 13:36:47 -04:00
|
|
|
return {
|
|
|
|
map: accounts,
|
|
|
|
children: accountChildren(accounts)
|
|
|
|
};
|
2016-10-03 19:49:15 -04:00
|
|
|
case AccountConstants.ACCOUNT_REMOVED:
|
2016-10-05 13:36:47 -04:00
|
|
|
var accounts = assign({}, state.map);
|
|
|
|
delete accounts[action.accountId];
|
|
|
|
return {
|
|
|
|
map: accounts,
|
|
|
|
children: accountChildren(accounts)
|
|
|
|
};
|
|
|
|
case UserConstants.USER_LOGGEDOUT:
|
|
|
|
return initialState;
|
2016-10-03 19:49:15 -04:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|