Created
January 1, 2019 00:57
-
-
Save runandrerun/7ce21267e71edb72feefb358036b77a3 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
// 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