Last active
December 25, 2018 18:23
-
-
Save alex-okrushko/ff5d02624ef9934bd5c20ccbe1f3bf4c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 function reducer( | |
| state: CartState = initState, | |
| action: cartActions.All | |
| ): CartState { | |
| switch (action.type) { | |
| case cartActions.ADD_ITEM: { | |
| // Concatinating the id to the list | |
| const newCartItemsIds = [...state.cartItemsIds, action.itemId]; | |
| return { | |
| cartItemsIds: newCartItemsIds, | |
| }; | |
| } | |
| case cartActions.ADD_ITEM_ERROR: { | |
| const indexOfItemId = state.cartItemsIds.indexOf(action.itemId); | |
| // Force array to be "immutable" (cloning the one in the state). | |
| const newCartItemsIds = [...state.cartItemsIds]; | |
| // Remove the element. | |
| newCartItemsIds.splice(indexOfItemId, 1); | |
| return { | |
| cartItemsIds: newCartItemsIds, | |
| }; | |
| } | |
| ... | |
| } | |
| return state; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment