OwlCyberSecurity - MANAGER
Edit File: creditsBasketSlice.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 creditsBasketSlice = createSlice({ name: "creditsBasket", initialState, reducers: { addToCreditsBasket: (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]; }, removeFromCreditsBasket: (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; }, incrementCreditsCount: (state, action: PayloadAction<cartType>) => { const item = state.cart.find((item: any) => item.id === action.payload); item.count++; }, decrementCreditsCount: (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--; } }, updateCreditsCount: (state, action: PayloadAction<any>) => { const item = state.cart.find( (item: any) => item.slug === action.payload.slug ); item.inventory = action.payload.inventory; item.weight = action.payload.weight; item.singleitemfullprice = action.payload.singleitemfullprice; item.item_price = action.payload.item_price; item.shipping_cost = action.payload.shipping_cost; if (item.inventory < item.count) { item.count = item.inventory; } }, removeAllItemsFromCredits: (state, action: PayloadAction<any>) => { state.cart = []; }, updateCreditsCart: (state, action: any) => { state.cart = action.payload; }, }, }); export const { addToCreditsBasket, removeFromCreditsBasket, incrementCreditsCount, decrementCreditsCount, updateCreditsCount, removeAllItemsFromCredits, updateCreditsCart, } = creditsBasketSlice.actions; export const creditsSelectItems = (state: any) => state.creditsBasket.cart; export const creditsSelectTotalPrice = (state: any) => state.creditsBasket.cart.reduce( (total: any, item: any) => total + item.singleitemfullprice * item.count, 0 ); export const creditsSelectTotalIetmPrice = (state: any) => state.creditsBasket.cart.reduce( (total: any, item: any) => total + item.item_price * item.count, 0 ); export const creditsSelectTotalShoppingCost = (state: any) => state.creditsBasket.cart.reduce( (total: any, item: any) => total + item.shipping_cost * item.count, 0 ); export const creditsSelectTotalWeight = (state: any) => state.creditsBasket.cart.reduce( (total: any, item: any) => total + item.firstweight * item.count, 0 ); // export const cartOpen = (state) => state.basket.cartOpen; export default creditsBasketSlice.reducer;