lbry-desktop/ui/util/redux-utils.js

17 lines
502 B
JavaScript
Raw Normal View History

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