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