Last active
November 18, 2019 17:42
-
-
Save codeBelt/dd687754786fdc1b3034e0d8fb50f6ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const initialState = { | |
currentShowId: '74', | |
show: null, | |
episodes: [], | |
actors: [], | |
}; | |
export const showsReducer = baseReducer(initialState, { | |
[ShowsAction.REQUEST_SHOW_FINISHED](state, action) { | |
return { | |
...state, | |
show: action.payload, | |
}; | |
}, | |
[ShowsAction.REQUEST_EPISODES_FINISHED](state, action) { | |
return { | |
...state, | |
episodes: action.payload, | |
}; | |
}, | |
[ShowsAction.REQUEST_CAST_FINISHED](state, action) { | |
return { | |
...state, | |
actors: action.payload, | |
}; | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function baseReducer(initialState, reducerDictionary) { | |
// returns a redux reducing function | |
return (state = initialState, action) => { | |
// if the action type is used for a reducer name then this be a reference to it. | |
const reducer = reducerDictionary[action.type]; | |
// if the action type "reducer" const is undefined or the action is an error | |
// return the state. | |
if (!reducer || action.error) { | |
return state; | |
} | |
// if there is a valid reducer call it with the state and action objects. | |
return reducer(state, action); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment