Skip to content

Instantly share code, notes, and snippets.

@timdeschryver
Created May 25, 2018 16:30
Show Gist options
  • Save timdeschryver/1f8b2fe4070be86f318c7e55bdbce0c9 to your computer and use it in GitHub Desktop.
Save timdeschryver/1f8b2fe4070be86f318c7e55bdbce0c9 to your computer and use it in GitHub Desktop.
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