-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductReducer.js
31 lines (28 loc) · 1023 Bytes
/
ProductReducer.js
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
import { createSlice } from "@reduxjs/toolkit";
export const productSlice = createSlice({
name:"product",
initialState:{
product:[],
},
reducers:{
getProducts:(state,action) => {
state.product.push({...action.payload});
},
incrementQty:(state,action) => {
const itemPresent = state.product.find((item) => item.id === action.payload.id);
itemPresent.quantity++;
},
decrementQty:(state,action) => {
const itemPresent = state.product.find((item) => item.id === action.payload.id);
if(itemPresent.quantity == 1){
itemPresent.quantity = 0;
const removeItem = state.product.filter((item) => item.id !== action.payload.id);
state.cart = removeItem;
}else{
itemPresent.quantity--;
}
}
}
});
export const {getProducts,incrementQty,decrementQty} = productSlice.actions;
export default productSlice.reducer;