1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-11-01 00:10:06 -04:00
moneygo/js/reducers/SecurityReducer.js
Aaron Lindsay 25b04a4f0f Stop creating lists in containers
Create them only when needed in reducers instead
2017-06-21 21:27:41 -04:00

75 lines
1.9 KiB
JavaScript

var assign = require('object-assign');
var SecurityConstants = require('../constants/SecurityConstants');
var UserConstants = require('../constants/UserConstants');
var SecurityType = require('../models').SecurityType;
const initialState = {
map: {},
list: [],
currency_list: []
}
function mapToList(securities) {
var security_list = [];
for (var securityId in securities) {
if (securities.hasOwnProperty(securityId))
security_list.push(securities[securityId]);
}
return security_list;
}
function mapToCurrencyList(securities) {
var security_list = [];
for (var securityId in securities) {
if (securities.hasOwnProperty(securityId) && securities[securityId].Type == SecurityType.Currency)
security_list.push(securities[securityId]);
}
return security_list;
}
module.exports = function(state = initialState, action) {
switch (action.type) {
case SecurityConstants.SECURITIES_FETCHED:
var securities = {};
var list = [];
var currency_list = [];
for (var i = 0; i < action.securities.length; i++) {
var security = action.securities[i];
securities[security.SecurityId] = security;
list.push(security);
if (security.Type == SecurityType.Currency)
currency_list.push(security);
}
return {
map: securities,
list: list,
currency_list: currency_list
};
case SecurityConstants.SECURITY_CREATED:
case SecurityConstants.SECURITY_UPDATED:
var security = action.security;
var map = assign({}, state.map, {
[security.SecurityId]: security
});
return {
map: map,
list: mapToList(map),
currency_list: mapToCurrencyList(map)
};
case SecurityConstants.SECURITY_REMOVED:
var map = assign({}, state.map);
delete map[action.securityId];
return {
map: map,
list: mapToList(map),
currency_list: mapToCurrencyList(map)
};
case UserConstants.USER_LOGGEDOUT:
return initialState;
default:
return state;
}
};