Permalink
Browse files

Add createConditionalSliceReducer utility

  • Loading branch information...
markerikson committed Jan 13, 2017
1 parent 9d6b443 commit 3b5b2bff168f62847f0dcccbce5e1ec096b1fe39
Showing with 21 additions and 0 deletions.
  1. +21 −0 src/common/utils/reducerUtils.js
@@ -13,3 +13,24 @@ export function reduceReducers(...reducers) {
previous
);
}
export function createConditionalSliceReducer(sliceName, fnMap) {
// Create a reducer that knows how to handle one slice of state, with these action types
const sliceReducer = createReducer({}, fnMap);
// Create a new wrapping reducer
return (state, action) => {
// Check to see if this slice reducer knows how to handle this action
if(fnMap[action.type]) {
// If it does, pass the slice to the slice reducer, and update the slice
return {
...state,
[sliceName] : sliceReducer(state[sliceName], action),
};
}
// Otherwise, return the existing state unchanged
return state;
}
}

0 comments on commit 3b5b2bf

Please sign in to comment.