Skip to content

Instantly share code, notes, and snippets.

@runandrerun
Created January 1, 2019 00:57
Show Gist options
  • Save runandrerun/7ce21267e71edb72feefb358036b77a3 to your computer and use it in GitHub Desktop.
Save runandrerun/7ce21267e71edb72feefb358036b77a3 to your computer and use it in GitHub Desktop.
// this reducer takes in the initial state and the action called
bodegaReducer = (state = initialBodegaState, action) => {
// the below switch case decides what to do based on the action
switch(action.type) {
// since 'ENTER_BODEGA' is called
// insideBodega is toggled to be true
case 'ENTER_BODEGA':
return {
...state,
insideBodega: true,
};
// when food is brought, we alter the state/quantity of this key/value pair
case 'BUY_CHOPPED_CHEESE':
return {
...state,
choppedCheese: Math.max(0, state.choppedCheese - action.buy),
};
// once more, we alter the state, but this time a string datatype
case 'PET_BODEGA_CAT':
return {
...state,
catMood: "Happy",
};
// lastly we toggle the insideBodega value as we leave
case 'LEAVE_BODEGA':
return {
...state,
insideBodega: false,
};
// default catch in case an action doesn't exist
// this will return the initialState
default:
return state;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment