Skip to content

Commit

Permalink
Add createConditionalSliceReducer utility
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Nov 5, 2017
1 parent 7d7726d commit 5c37b11
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/common/utils/reducerUtils.js
Expand Up @@ -13,3 +13,23 @@ 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 5c37b11

Please sign in to comment.