All files / src/redux/slices/cart index.ts

34.37% Statements 11/32
6.25% Branches 1/16
13.33% Functions 2/15
37.03% Lines 10/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74          540x   540x           540x         540x                                                               41x         540x   540x   540x       540x     540x              
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { safeParse } from "@utils/index";
import { CartItemType } from "./CartItemType";
import { CartState } from "./CartState";
 
const localCartItems = localStorage.getItem("cartItems");
 
const cartItemsData = localCartItems !== null ? safeParse(localCartItems) || [] : [];
 
interface RootState {
  cart: CartState;
}
 
const initialState: CartState = {
  cartItems: cartItemsData,
  isCartLoading: false,
};
 
const cartSlice = createSlice({
  name: "cart",
  initialState,
  reducers: {
    setCartItems: (state: CartState, action: PayloadAction<CartItemType[]>) => {
      state.cartItems = action.payload;
      // localStorage.setItem("cartItems", JSON.stringify(action.payload))
    },
    setCartItem: (state: CartState, action: PayloadAction<CartItemType>) => {
      const existingItem = state.cartItems.find(
        (item) => item.productVariantID === action.payload.productVariantID,
      );
 
      if (existingItem && action.payload.quantity > 0) {
        const updatedItems = state.cartItems.map((item) => {
          if (existingItem.productVariantID === item.productVariantID)
            return action.payload;
          return item;
        });
 
        state.cartItems = updatedItems;
      } else if (existingItem && action.payload.quantity === 0) {
        state.cartItems = state.cartItems.filter(
          (item) => item.productVariantID !== action.payload.productVariantID,
        );
      } else {
        state.cartItems.push(action.payload);
      }
 
      // localStorage.setItem("cartItems", JSON.stringify(state.cartItems));
    },
    setCartLoading: (state: CartState, action: PayloadAction<boolean>) => {
      state.isCartLoading = action.payload;
    },
  },
});
 
export const { setCartItems, setCartItem, setCartLoading } = cartSlice.actions;
 
export const selectCart = (state: RootState) => state.cart;
 
export const findCartItemQuantity = (id: number) => (state: RootState) =>
  state.cart.cartItems.find((item) => item.productVariantID === id)?.quantity ||
  0;
 
export const findCartItem = (id: number) => (state: RootState) =>
  state.cart.cartItems.find((item) => item.productVariantID === id);
 
export const selectCartTotal = (state: RootState) =>
  state.cart.cartItems.reduce(
    (sum: number, item: CartItemType) => sum + item.price * item.quantity,
    0,
  );
 
export default cartSlice.reducer;