Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active November 18, 2019 17:42
Show Gist options
  • Save codeBelt/dd687754786fdc1b3034e0d8fb50f6ba to your computer and use it in GitHub Desktop.
Save codeBelt/dd687754786fdc1b3034e0d8fb50f6ba to your computer and use it in GitHub Desktop.
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,
};
},
});
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