Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { get } from 'lodash';
- export function reducerWrapper(featureName: string, initialState: any = {}) {
- // In this article, our featureName will be USER_INTERESTS
- return function baseReducer(state = initialState, action: IAction) {
- switch (action.type) {
- case `SUCCESS_${featureName}`: {
- // case `SUCCESS_USER_INTERESTS`
- let stateSlice = get(state, action.payload.path);
- // Check if state at path exists, otherwise error will be thrown when adding data
- if (stateSlice) {
- stateSlice.data = action.payload.data;
- stateSlice.status = 'SUCCESS';
- }
- return state;
- }
- case `FAILED_${featureName}`: {
- // case `FAILED_USER_INTERESTS`
- let stateSlice = get(state, action.payload.path);
- if (stateSlice) {
- stateSlice.status = 'FAILURE';
- }
- return state;
- }
- default:
- return state;
- }
- };
- }
Add Comment
Please, Sign In to add comment