File size: 1,544 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
const CartData = {
CartItems: JSON.parse(localStorage.getItem("CartItems")) || [],
};
const CartItemsAdded = (state = CartData, action) => {
switch (action.type) {
case "ADD_TO_CART":
const productData = action.data;
const updatedCart = [...state.CartItems, productData];
localStorage.setItem("CartItems", JSON.stringify(updatedCart));
return {
...state,
CartItems: updatedCart,
};
case "REMOVE_FROM_CART":
const itemId = action.id;
const newCart = state.CartItems.filter(
(element) => element.id !== itemId
);
localStorage.setItem("CartItems", JSON.stringify(newCart));
return {
...state,
CartItems: newCart,
};
case "INCREASE_QUANTITY":
const itemIndex = state.CartItems.findIndex(
(item) => item.id === action.id
);
const newCartItems = [...state.CartItems];
newCartItems[itemIndex].quantity++;
localStorage.setItem("CartItems", JSON.stringify(newCartItems));
return {
...state,
CartItems: newCartItems,
};
case "DECREASE_QUANTITY":
const itemIndex2 = state.CartItems.findIndex(
(item) => item.id === action.id
);
const newCartItems2 = [...state.CartItems];
newCartItems2[itemIndex2].quantity--;
localStorage.setItem("CartItems", JSON.stringify(newCartItems2));
return {
...state,
CartItems: newCartItems2,
};
default:
return state;
}
};
export default CartItemsAdded;
|