Browse Source

filter products

master
azri 2 weeks ago
parent
commit
a80f2f8b72
1 changed files with 20 additions and 19 deletions
  1. 20
    19
      src/components/ProductList/ProductList.jsx

+ 20
- 19
src/components/ProductList/ProductList.jsx View File

71
   useEffect(() => {
71
   useEffect(() => {
72
 
72
 
73
     console.log("Products: ", products)
73
     console.log("Products: ", products)
74
+    
74
     if (products.length > 0) {
75
     if (products.length > 0) {
75
-      let productType = sessionStorage.getItem('amber-select-product-type')
76
-      let newFilteredProducts = products.filter(
77
-        (product) => product.productType === productType
78
-      );
79
 
76
 
80
-      setTimeout(() => {
81
-        setFilteredProducts(newFilteredProducts)
82
-      }, 100)
77
+      let newFilteredProducts = filterProducts()
78
+      setFilteredProducts(newFilteredProducts)
83
 
79
 
84
       const tagList = getAllTags(newFilteredProducts);
80
       const tagList = getAllTags(newFilteredProducts);
85
       setTagFilterOption(tagList);
81
       setTagFilterOption(tagList);
102
 
98
 
103
   useEffect(() => {
99
   useEffect(() => {
104
 
100
 
101
+    let newFilteredProducts = filterProducts()
102
+    debugger
103
+    setFilteredProducts(newFilteredProducts)
104
+
105
+  }, [tags, collection, sort])
106
+
107
+  const filterProducts = () => {
108
+
105
     if (products?.length > 0) {
109
     if (products?.length > 0) {
106
 
110
 
107
       let productType = sessionStorage.getItem('amber-select-product-type')
111
       let productType = sessionStorage.getItem('amber-select-product-type')
135
         }
139
         }
136
       );
140
       );
137
 
141
 
138
-
139
       if (sort === "title") {
142
       if (sort === "title") {
140
         newFilteredProducts = newFilteredProducts.sort((a, b) => a.title.localeCompare(b.title));
143
         newFilteredProducts = newFilteredProducts.sort((a, b) => a.title.localeCompare(b.title));
141
       } else if (sort === "new") {
144
       } else if (sort === "new") {
144
         newFilteredProducts = newFilteredProducts.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
147
         newFilteredProducts = newFilteredProducts.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
145
       }
148
       }
146
 
149
 
147
-
148
-      setFilteredProducts([]) //cause I want those fadeIn animation
149
-      setTimeout(() => {
150
-        setFilteredProducts(newFilteredProducts)
151
-      }, 100)
150
+      return newFilteredProducts
152
 
151
 
153
     }
152
     }
154
 
153
 
155
-  }, [tags, collection, sort])
154
+    return []
155
+
156
+  }
156
 
157
 
157
   const handleChange = (event) => {
158
   const handleChange = (event) => {
158
     //setInput({ ...input, [event.target.name]: event.target.value });
159
     //setInput({ ...input, [event.target.name]: event.target.value });
200
                 },
201
                 },
201
                 boxShadow: 0,
202
                 boxShadow: 0,
202
                 fontSize: 10
203
                 fontSize: 10
203
-              }} 
204
+              }}
204
               variant="contained">
205
               variant="contained">
205
               NEW
206
               NEW
206
             </Button>}
207
             </Button>}
269
 
270
 
270
         {/* Right Side: Option Inputs */}
271
         {/* Right Side: Option Inputs */}
271
         <Box sx={{
272
         <Box sx={{
272
-          display: "flex", gap: 2, flexDirection: "row", flexWrap:"wrap", justifyContent:"space-between", py:{
273
-            xs:2,
274
-            sm:2,
275
-            md:0
273
+          display: "flex", gap: 2, flexDirection: "row", flexWrap: "wrap", justifyContent: "space-between", py: {
274
+            xs: 2,
275
+            sm: 2,
276
+            md: 0
276
           }
277
           }
277
         }}>
278
         }}>
278
 
279
 

Loading…
Cancel
Save