2016-10-05 13:36:47 -04:00
|
|
|
var assign = require('object-assign');
|
|
|
|
|
|
|
|
var SecurityConstants = require('../constants/SecurityConstants');
|
|
|
|
var UserConstants = require('../constants/UserConstants');
|
|
|
|
|
2017-06-21 21:27:05 -04:00
|
|
|
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) {
|
2016-10-05 13:36:47 -04:00
|
|
|
switch (action.type) {
|
|
|
|
case SecurityConstants.SECURITIES_FETCHED:
|
|
|
|
var securities = {};
|
2017-06-21 21:27:05 -04:00
|
|
|
var list = [];
|
|
|
|
var currency_list = [];
|
2016-10-05 13:36:47 -04:00
|
|
|
for (var i = 0; i < action.securities.length; i++) {
|
|
|
|
var security = action.securities[i];
|
|
|
|
securities[security.SecurityId] = security;
|
2017-06-21 21:27:05 -04:00
|
|
|
list.push(security);
|
|
|
|
if (security.Type == SecurityType.Currency)
|
|
|
|
currency_list.push(security);
|
2016-10-05 13:36:47 -04:00
|
|
|
}
|
2017-06-21 21:27:05 -04:00
|
|
|
return {
|
|
|
|
map: securities,
|
|
|
|
list: list,
|
|
|
|
currency_list: currency_list
|
|
|
|
};
|
2016-10-05 13:36:47 -04:00
|
|
|
case SecurityConstants.SECURITY_CREATED:
|
|
|
|
case SecurityConstants.SECURITY_UPDATED:
|
|
|
|
var security = action.security;
|
2017-06-21 21:27:05 -04:00
|
|
|
var map = assign({}, state.map, {
|
2016-10-05 13:36:47 -04:00
|
|
|
[security.SecurityId]: security
|
|
|
|
});
|
2017-06-21 21:27:05 -04:00
|
|
|
return {
|
|
|
|
map: map,
|
|
|
|
list: mapToList(map),
|
|
|
|
currency_list: mapToCurrencyList(map)
|
|
|
|
};
|
2016-10-05 13:36:47 -04:00
|
|
|
case SecurityConstants.SECURITY_REMOVED:
|
2017-06-21 21:27:05 -04:00
|
|
|
var map = assign({}, state.map);
|
|
|
|
delete map[action.securityId];
|
|
|
|
return {
|
|
|
|
map: map,
|
|
|
|
list: mapToList(map),
|
|
|
|
currency_list: mapToCurrencyList(map)
|
|
|
|
};
|
2016-10-05 13:36:47 -04:00
|
|
|
case UserConstants.USER_LOGGEDOUT:
|
2017-06-21 21:27:05 -04:00
|
|
|
return initialState;
|
2016-10-05 13:36:47 -04:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|