Created
May 25, 2018 16:30
-
-
Save timdeschryver/1f8b2fe4070be86f318c7e55bdbce0c9 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 function reducer(state = initialState, action: CartActions) { | |
switch (action.type) { | |
case CartActionTypes.AddToCart: | |
return { | |
...state, | |
cartItems: { | |
...state.cartItems, | |
[action.payload.sku]: (state.cartItems[action.payload.sku] || 0) + 1, | |
}, | |
}; | |
case CartActionTypes.RemoveFromCart: | |
return { | |
...state, | |
cartItems: { | |
...state.cartItems, | |
[action.payload.sku]: Math.max((state.cartItems[action.payload.sku] || 0) - 1, 0), | |
}, | |
}; | |
case CartActionTypes.EmptyCart: | |
return initialState; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment