2017-11-27 12:07:57 -05:00
|
|
|
// util for creating reducers
|
|
|
|
// based off of redux-actions
|
|
|
|
// https://redux-actions.js.org/docs/api/handleAction.html#handleactions
|
2017-12-08 15:14:35 -05:00
|
|
|
|
2017-12-21 22:21:22 -03:00
|
|
|
export const handleActions = (actionMap, defaultState) => (state = defaultState, action) => {
|
2017-12-13 18:36:30 -03:00
|
|
|
const handler = actionMap[action.type];
|
2017-12-08 15:14:35 -05:00
|
|
|
|
2017-12-13 18:36:30 -03:00
|
|
|
if (handler) {
|
|
|
|
const newState = handler(state, action);
|
|
|
|
return Object.assign({}, state, newState);
|
|
|
|
}
|
2017-12-08 15:14:35 -05:00
|
|
|
|
2017-12-13 18:36:30 -03:00
|
|
|
// just return the original state if no handler
|
|
|
|
// returning a copy here breaks redux-persist
|
|
|
|
return state;
|
2017-11-27 12:07:57 -05:00
|
|
|
};
|