Skip to content

Instantly share code, notes, and snippets.

@alex-okrushko
Last active December 25, 2018 18:23
Show Gist options
  • Select an option

  • Save alex-okrushko/ff5d02624ef9934bd5c20ccbe1f3bf4c to your computer and use it in GitHub Desktop.

Select an option

Save alex-okrushko/ff5d02624ef9934bd5c20ccbe1f3bf4c to your computer and use it in GitHub Desktop.
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