From 9a87857c8f33628cb25946b5d5c31ae28c791455 Mon Sep 17 00:00:00 2001 From: mohammadpiriyan <50291430+mohammadpiriyan@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:08:27 +0330 Subject: [PATCH] add reduce and add dispatch to reducer of sliceer --- src/Redux/slices/cartSlices.ts | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Redux/slices/cartSlices.ts b/src/Redux/slices/cartSlices.ts index 5b36224..66f2a5e 100644 --- a/src/Redux/slices/cartSlices.ts +++ b/src/Redux/slices/cartSlices.ts @@ -51,13 +51,31 @@ const cartSlices = createSlice({ // console.log(orderOfCart.orderQuantity); }, handeleEmptyCart: (state) => { - return initialState + return initialState; // state.CartProducts=[] // state.CartProducts.splice(0, state.CartProducts.length); // return produce(state, (draftState) => { // draftState.CartProducts = []; // }); }, + handleAddCountOrder: (state, action) => { + const existingOrder = state.CartProducts.find( + (order) => order._id === action.payload._id + ); + if (existingOrder) { + existingOrder.orderQuantity += 1; + existingOrder.totalPriceproduct += action.payload.price; + } + }, + handleReduceCountOrder: (state, action) => { + const existingOrder = state.CartProducts.find( + (order) => order._id === action.payload._id + ); + if (existingOrder) { + existingOrder.orderQuantity -= 1; + existingOrder.totalPriceproduct -= action.payload.price; + } + }, handeleRemoveFromCart: (state, action) => { console.log(action.payload); const afterRemove = state.CartProducts.filter( @@ -71,5 +89,10 @@ const cartSlices = createSlice({ // export default cartSlices.reducer; const reducers = combineReducers({ cartSlices: cartSlices.reducer }); export const persistedReducer = persistReducer(presistConfig as any, reducers); -export const { handeleAddTOCart, handeleEmptyCart, handeleRemoveFromCart } = - cartSlices.actions; +export const { + handeleAddTOCart, + handeleEmptyCart, + handeleRemoveFromCart, + handleAddCountOrder, + handleReduceCountOrder +} = cartSlices.actions;