import {Reducer} from 'redux'; import {clone} from 'lodash'; import {ITriggerAction, SUBMIT_SET_DATA, TRIGGER_SET_DATA} from './actions'; export type IState = Object; export const initialState: IState = {}; export const reducer: Reducer = (state: IState = initialState, actions: ITriggerAction): IState => { switch (actions.type) { case TRIGGER_SET_DATA: { let payload = actions.payload; state = clone(state); payload.forEach(pay => { let model = pay.model; if (!(model in state)) { state[model] = {}; } else { state[model] = clone(state[model]); } let customer = pay.customer; state[model][customer] = pay.data; }); return state; } case SUBMIT_SET_DATA: { let payload = actions.payload; let model = payload.model; let customer = payload.customer; if (!(model in state)) { console.error('state unexpected has model', model); return state; } state = clone(state); state[model] = clone(state[model]); let modelState = state[model]; if (!(customer in modelState)) { console.error('unexpected data customer name', customer); return state; } let customerScope = modelState[customer]; if (customerScope instanceof Array) { customerScope = customerScope.concat(payload.data); } else if (customerScope instanceof Object) { Object.assign(customerScope, payload.data); } state[model][customer] = customerScope; return state; } default: return state; } };