OwlCyberSecurity - MANAGER
Edit File: otherBasketSlice.tsx
"use client"; import { cartType } from "@/typing"; import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { toast } from "sonner"; type InitialState = { cart: cartType[] | any; }; const initialState = { cart: [], } as InitialState; export const otherBasketSlice = createSlice({ name: "otherBasket", initialState, reducers: { addToOtherBasket: (state, action: PayloadAction<any>) => { const itemInCart = state.cart.find( (item: cartType) => item.link === action.payload.link ); if (itemInCart) { toast.error( "آیتم در سبد شما موجود می باشد\n\n برای اضافه کردن تعداد به سبد خرید بروید." ); return; } else if (state.cart.length > 4) { toast.error("بیش از 5 آیتم نمی تواند در یک سبد باشد"); return; } else { state.cart.push({ ...action.payload }); toast.success("آیتم به سبد شما اضافه شد."); } }, removeFromOtherBasket: (state, action: PayloadAction<any>) => { const index = state.cart.findIndex( (basketItem: cartType) => basketItem.link === action.payload.link ); 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.link} as its not in basket)` ); } state.cart = newBasket; }, incrementOtherCount: (state, action: PayloadAction<cartType>) => { const item = state.cart.find((item: any) => item.link === action.payload); item.count++; }, decrementOtherCount: (state, action: PayloadAction<cartType>) => { const item = state.cart.find((item: any) => item.link === action.payload); if (item.count === 1) { item.count = 1; } else { item.count--; } }, updateOtherCount: (state, action: PayloadAction<any>) => { const item = state.cart.find( (item: any) => item.link === action.payload.link ); item.singleitemfullprice = action.payload.finalRialPrice; item.buyCost = action.payload.buyCost; item.buyProfit = action.payload.buyProfit; item.shipBroker = action.payload.shipBroker; }, removeAllItemsFromOther: (state, action: PayloadAction<any>) => { state.cart = []; }, updateOtherCart: (state, action: any) => { state.cart = action.payload; }, }, }); export const { addToOtherBasket, removeFromOtherBasket, incrementOtherCount, decrementOtherCount, updateOtherCount, removeAllItemsFromOther, updateOtherCart, } = otherBasketSlice.actions; export const OtherSelectItems = (state: any) => state.otherBasket.cart; export const OtherSelectTotalPrice = (state: any) => state.otherBasket.cart.reduce( (total: any, item: any) => total + item.cost * item.count, 0 ); export const OtherSelectTotalRial = (state: any) => state.otherBasket.cart.reduce( (total: any, item: any) => total + item.singleitemfullprice, 0 ); export const OtherSelectTotalShipBroker = (state: any) => state.otherBasket.cart.reduce( (total: any, item: any) => total + item.shipBroker, 0 ); export const OtherSelectTotalbuyProfit = (state: any) => state.otherBasket.cart.reduce( (total: any, item: any) => total + item.buyProfit, 0 ); export const OtherSelectTotalbuyCost = (state: any) => state.otherBasket.cart.reduce( (total: any, item: any) => total + item.buyCost, 0 ); export default otherBasketSlice.reducer;