-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReducer.js
More file actions
33 lines (31 loc) · 834 Bytes
/
Copy pathReducer.js
File metadata and controls
33 lines (31 loc) · 834 Bytes
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
export const initialState = {
basket : [],
user : null,
}
const reducer = (state,action) => {
switch (action.type) {
case 'ADD_TO_BASKET':
return{
...state,
basket : [...state.basket,action.item]
}
case 'REMOVE_FROM_BASKET' :
const tempArray = [...state.basket];
let firstIndex=state.basket.findIndex(basketitem => basketitem.id===action.id);
if(firstIndex>=0){
tempArray.splice(firstIndex,1);
}
return{
...state,
basket:[...tempArray]
}
case 'SET_USER' :
return({
...state,
user:action.user,
})
default:
break;
}
}
export default reducer