@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
2
2
import { Link } from 'react-router-dom' ;
3
3
import FavNotFound from '../components/FavNotFound' ;
4
4
import Header from '../components/Search'
5
+ import Button from "@mui/material/Button" ;
5
6
import { IoIosHeart , IoMdHeartEmpty } from "react-icons/io" ;
6
7
import "../css/ecommerce-category-product.css" ;
7
8
import '../css/filter.css'
@@ -14,8 +15,29 @@ import Box from '@mui/material/Box';
14
15
import Slider from '@mui/material/Slider' ;
15
16
import Typography from '@mui/material/Typography' ;
16
17
import TextField from '@mui/material/TextField'
18
+ import toast , { Toaster } from 'react-hot-toast' ;
19
+ import styled from 'styled-components'
20
+ import FavoriteHeart from '../components/FavoriteAddHeart'
17
21
18
22
23
+ const IconButton = styled ( Button ) `
24
+ && {
25
+ padding: 0;
26
+ min-width: unset;
27
+ width: auto;
28
+ left: 0;
29
+ }
30
+ ` ;
31
+
32
+ const HeartIconEmpty = styled ( IoMdHeartEmpty ) `
33
+ font-size: 24px;
34
+ ` ;
35
+
36
+ const HeartIcon = styled ( IoIosHeart ) `
37
+ font-size: 24px;
38
+ color:red;
39
+ ` ;
40
+
19
41
function Filter ( ) {
20
42
const [ favoritesCount , setFavoritesCount ] = useState ( 0 ) ;
21
43
const [ products , setProducts ] = useState ( [ ] ) ;
@@ -97,6 +119,12 @@ function Filter() {
97
119
98
120
localStorage . setItem ( 'Products' , JSON . stringify ( ProductItems ) ) ;
99
121
setProductItem ( ! ProductItem )
122
+
123
+ toast . success ( 'Product successfully added to cart' , {
124
+ style : {
125
+ boxShadow : 'none' ,
126
+ } ,
127
+ } ) ;
100
128
101
129
}
102
130
useEffect ( ( ) => {
@@ -136,9 +164,48 @@ const handleSearch = (searchValue) => {
136
164
const uniqueCategories = Array . from ( new Set ( filtered . map ( product => product . category ) ) ) ;
137
165
setCategories ( uniqueCategories ) ;
138
166
} ;
167
+ const handleChange = ( event ) => {
168
+ const selectedCategory = event . target . value ;
169
+ let filteredProducts = [ ] ;
170
+ if ( selectedCategory === "All" ) {
171
+ filteredProducts = products ;
172
+ } else {
173
+ filteredProducts = products . filter ( product => product . category === selectedCategory ) ;
174
+ }
175
+ setFilteredProducts ( filteredProducts ) ;
176
+
177
+ const uniqueCategories = Array . from ( new Set ( filteredProducts . map ( product => product . category ) ) ) ;
178
+ setCategories ( uniqueCategories ) ;
179
+ } ;
180
+
181
+
182
+
183
+ const [ minPrice , setMinPrice ] = useState ( '' ) ;
184
+ const [ maxPrice , setMaxPrice ] = useState ( '' ) ;
185
+
186
+ const handlePriceFilter = ( ) => {
187
+ const minPriceValue = parseFloat ( minPrice ) ;
188
+ const maxPriceValue = parseFloat ( maxPrice ) ;
189
+
190
+ if ( isNaN ( minPriceValue ) || isNaN ( maxPriceValue ) ) {
191
+ toast . error ( 'Please enter valid numeric values for both minimum and maximum prices' , {
192
+ style : {
193
+ boxShadow : 'none' ,
194
+ } ,
195
+ } ) ;
196
+ return ;
197
+ }
198
+
199
+ const filteredByPrice = products . filter ( product => {
200
+ return product . price >= minPrice && product . price <= maxPrice ;
201
+ } ) ;
202
+
203
+ setFilteredProducts ( filteredByPrice ) ;
204
+ } ;
139
205
140
206
return (
141
207
< >
208
+ < Toaster />
142
209
< Header onSearch = { handleSearch } />
143
210
< div className = 'MainContainer' style = { { display : 'flex' } } >
144
211
< FormControl className = "FilterBar" >
@@ -149,27 +216,42 @@ const handleSearch = (searchValue) => {
149
216
className = "categories"
150
217
defaultValue = "All"
151
218
name = "radio-buttons-group"
219
+ onChange = { handleChange }
152
220
>
153
221
< FormLabel id = "demo-radio-buttons-group-label" className = 'CategoriesText' > Categories</ FormLabel >
154
222
{ /* <Typography className="line">---------------------------------------------------</Typography> */ }
155
223
< FormControlLabel value = "All" control = { < Radio /> } label = "All" />
156
- < FormControlLabel value = "Smart Phones " control = { < Radio /> } label = "Smart Phones" />
157
- < FormControlLabel value = "Laptops " control = { < Radio /> } label = "Laptops" />
158
- < FormControlLabel value = "Fragrances " control = { < Radio /> } label = "fragrances" />
159
- < FormControlLabel value = "Skincare " control = { < Radio /> } label = "skincare" />
160
- < FormControlLabel value = "Groceries " control = { < Radio /> } label = "groceries" />
161
- < FormControlLabel value = "Home Decoration " control = { < Radio /> } label = "home decoration" />
224
+ < FormControlLabel value = "smartphones " control = { < Radio /> } label = "Smart Phones" />
225
+ < FormControlLabel value = "laptops " control = { < Radio /> } label = "Laptops" />
226
+ < FormControlLabel value = "fragrances " control = { < Radio /> } label = "fragrances" />
227
+ < FormControlLabel value = "skincare " control = { < Radio /> } label = "skincare" />
228
+ < FormControlLabel value = "groceries " control = { < Radio /> } label = "groceries" />
229
+ < FormControlLabel value = "home-decoration " control = { < Radio /> } label = "home decoration" />
162
230
</ RadioGroup >
163
231
164
232
165
- < Box sx = { { width : 300 } } className = "price" >
166
- < Typography className = 'priceText' size = "medium" > Price</ Typography >
167
- { /* <Typography className="line">---------------------------------------------------</Typography> */ }
168
- < div style = { { display : 'flex' , gap : '8px' } } >
169
- < TextField className = "MinPrice" label = "MinPrice" variant = "standard" size = "small" />
170
- < TextField className = "MaxPrice" label = "MaxPrice" variant = "standard" size = "small" />
171
- </ div >
172
- </ Box >
233
+ < Box sx = { { width : 300 } } className = "Filterprice" >
234
+ < Typography className = 'priceText' > Price</ Typography >
235
+ < div style = { { display : 'flex' , gap : '8px' } } >
236
+ < TextField
237
+ className = "MinPrice"
238
+ label = "Min Price"
239
+ variant = "standard"
240
+ size = "small"
241
+ value = { minPrice }
242
+ onChange = { ( e ) => setMinPrice ( e . target . value ) }
243
+ />
244
+ < TextField
245
+ className = "MaxPrice"
246
+ label = "Max Price"
247
+ variant = "standard"
248
+ size = "small"
249
+ value = { maxPrice }
250
+ onChange = { ( e ) => setMaxPrice ( e . target . value ) }
251
+ />
252
+ </ div >
253
+ < Button variant = "contained" onClick = { handlePriceFilter } className = 'Price-Filter-Button' > Filter</ Button >
254
+ </ Box >
173
255
</ FormControl >
174
256
< div className = 'Product-Container-Catogory' >
175
257
@@ -200,12 +282,13 @@ const handleSearch = (searchValue) => {
200
282
< div className = "h-bg" >
201
283
< div className = "h-bg-inner" > </ div >
202
284
</ div >
203
- < Link className = "cart" onClick = { ( ) => toggleProduct ( product . id ) } >
285
+ < Link className = "cart" >
204
286
< span className = "price" > ${ product . price } </ span >
205
287
< span className = "add-to-cart" >
206
288
< span className = "txt" >
207
- < Link to = "/cart" className = 'GoToCartLink' > Go To Cart </ Link >
208
- < IoMdHeartEmpty id = "FTrash" > </ IoMdHeartEmpty >
289
+ < Link className = 'GoToCartLink' onClick = { ( ) => toggleProduct ( product . id ) } > Add Cart</ Link >
290
+
291
+ < FavoriteHeart productId = { product . id } id = "PFTrash" />
209
292
</ span >
210
293
</ span >
211
294
</ Link >
0 commit comments