forked from vaibhavsharma420/HacktoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterSearch
94 lines (79 loc) · 2.37 KB
/
FilterSearch
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React from 'react'
const Filter_Reducer = (state,action) => {
switch(action.type){
case "LOAD_FILTER_PRODUCTS":
return{
...state,
filter_products: [...action.payload],
all_products: [...action.payload],
};
case "SET_GRID_VIEW":
return {
...state,
grid_view: true
}
case "SET_LIST_VIEW":
return {
...state,
grid_view: false
}
case 'GET_SORT_VALUE':
return{
...state,
sorting_product: action.payload,
};
case 'UPDATE_FILTER_VALUE':
const {name, value}= action.payload;
return{
};
case "SORTING_PRODUCTS":
let newSortData;
let tempSortProduct= [...action.payload];
if(state.sorting_product==='a-z'){
newSortData= tempSortProduct.sort((a,b)=>
a.name.localeCompare(b.name)
);
}
if(state.sorting_product==='z-a'){
newSortData= tempSortProduct.sort((b,a)=>
a.name.localeCompare(b.name)
);
}
if(state.sorting_product==='lowest'){
const sortingProducts=(a,b)=>{
return a.price- b.price;
};
newSortData= tempSortProduct.sort(sortingProducts);
}
if(state.sorting_product==='highest'){
const sortingProducts=(a,b)=>{
return b.price- a.price;
};
newSortData= tempSortProduct.sort(sortingProducts);
}
/*const {filter_products, sorting_product} = state;
let tempSortProduct= [...filter_products];
const sortingProducts=(a,b)=>{
if(sorting_product==='lowest'){
return a.price-b.price;
}
if(sorting_product==='highest'){
return b.price-a.price;
}
if(sorting_product==='a-z'){
return a.name.localeCompare(b.name);
}
if(sorting_product==='z-a'){
return b.name.localeCompare(a.name);
}
};
newSortData= tempSortProduct.sort(sorting_product);*/
return{
...state,
filter_products: newSortData,
}
default:
return state;
}
}
export default Filter_Reducer;