OwlCyberSecurity - MANAGER
Edit File: charsooqBasketSlice.tsx
"use client"; import { cartType, charsooqCartType } from "@/typing"; import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { toast } from "sonner"; type InitialState = { cart: charsooqCartType[] | any; }; const initialState = { cart: [], } as InitialState; export const charsooqBasketSlice = createSlice({ name: "charsooqBasket", initialState, reducers: { addToCharsooqBasket: (state, action: PayloadAction<any>) => { const itemInCart = state.cart.find( (item: cartType) => item.id === action.payload.id ); if (itemInCart) { // itemInCart.count++; toast.error( "آیتم در سبد شما موجود می باشد\n\n برای تغییرات به سبد خرید بروید." ); return; } // else if (state.cart.length > 4) { // toast.error("بیش از 5 آیتم نمی تواند در یک سبد باشد"); // return; // } else { state.cart.push({ ...action.payload }); toast.success("آیتم به سبد شما اضافه شد."); } // state.items = [...state.items, action.payload]; }, removeFromCharsooqBasket: (state, action: PayloadAction<any>) => { const index = state.cart.findIndex( (basketItem: cartType) => basketItem.id === action.payload.id ); let newBasket = [...state.cart]; if (index >= 0) { //the item exist in the basket ... remove it newBasket.splice(index, 1); toast.success("آیتم با موفقیت از سبد شما حذف شد."); } else { console.warn( `cant remove product (id: ${action.payload.id} as its not in basket)` ); } state.cart = newBasket; }, incrementCharsooqCount: (state, action: PayloadAction<cartType>) => { const item = state.cart.find((item: any) => item.id === action.payload); item.count++; }, decrementCharsooqCount: (state, action: PayloadAction<cartType>) => { const item = state.cart.find((item: any) => item.id === action.payload); if (item.count === 1) { item.count = 1; } else { item.count--; } }, updateCharsooqCount: (state, action: PayloadAction<any>) => { const item = state.cart.find( (item: any) => item.slug === action.payload.slug ); item.count_for_user = action.payload.count_for_user; item.singleitemfullprice = action.payload.singleitemfullprice; item.cost = action.payload.cost; if (item.count_for_user < item.count) { item.count = item.count_for_user; } }, removeAllItemsFromCharsooq: (state, action: PayloadAction<any>) => { state.cart = []; }, updateCharsooqCart: (state, action: any) => { state.cart = action.payload; }, }, }); export const { addToCharsooqBasket, removeFromCharsooqBasket, incrementCharsooqCount, decrementCharsooqCount, updateCharsooqCount, removeAllItemsFromCharsooq, updateCharsooqCart, } = charsooqBasketSlice.actions; export const charsooqSelectItems = (state: any) => state.charsooqBasket.cart; export const charsooqSelectTotalPrice = (state: any) => state.charsooqBasket.cart.reduce( (total: any, item: any) => total + item.singleitemfullprice * item.count, 0 ); export const charsooqSelectTotalIetmPrice = (state: any) => state.charsooqBasket.cart.reduce( (total: any, item: any) => total + item.item_price * item.count, 0 ); export const charsooqSelectTotalShoppingCost = (state: any) => state.charsooqBasket.cart.reduce( (total: any, item: any) => total + item.shipping_cost * item.count, 0 ); export const charsooqSelectTotalWeight = (state: any) => state.charsooqBasket.cart.reduce( (total: any, item: any) => total + item.firstweight * item.count, 0 ); // export const cartOpen = (state) => state.basket.cartOpen; export default charsooqBasketSlice.reducer;