1
+ import { useEffect , useState , useCallback } from 'react'
2
+ import { getProducts , getCategories } from '../util/api'
3
+
4
+ const Home = ( ) => {
5
+
6
+ const [ products , setProducts ] = useState ( [ ] )
7
+ const [ results , setResults ] = useState ( [ ] )
8
+ const [ categories , setCategories ] = useState ( [ ] )
9
+ const [ search , setSearch ] = useState ( '' )
10
+
11
+ useEffect ( ( ) => {
12
+ getProducts ( )
13
+ . then ( json => {
14
+ setProducts ( json )
15
+ setResults ( json )
16
+ } )
17
+ } , [ ] )
18
+
19
+ useEffect ( ( ) => {
20
+ getCategories ( )
21
+ . then ( json => {
22
+ setCategories ( json )
23
+ } )
24
+ } , [ ] )
25
+
26
+ const handleFilter = useCallback ( ( ) => {
27
+ let stuff = results . filter ( product => product . title . toLowerCase ( ) . includes ( search . toLowerCase ( ) ) )
28
+ setProducts ( stuff )
29
+ } , [ results , search ] )
30
+
31
+ const loadProductByCategory = ( category ) => {
32
+ let stuff = results . filter ( product => product . category === category )
33
+ setProducts ( stuff )
34
+ }
35
+
36
+ const loadProductByPrice = ( x , y ) => {
37
+ let stuff = results . filter ( product => product . price >= x && product . price <= y )
38
+ setProducts ( stuff )
39
+ }
40
+
41
+ const loadAllProducts = ( ) => {
42
+ setProducts ( results )
43
+ }
44
+
45
+ useEffect ( ( ) => {
46
+ handleFilter ( )
47
+ } , [ search , handleFilter ] )
48
+
49
+
50
+ return (
51
+ < div className = 'w-full min-h-[calc(100vh-55px)] bg-slate-300' >
52
+
53
+ < div className = 'w-full flex justify-around items-center' >
54
+ < button className = 'py-1 px-6 text-xs font-semibold bg-slate-200' onClick = { loadAllProducts } > All</ button >
55
+
56
+ {
57
+ categories && categories . map ( ( category , index ) => (
58
+ < button className = 'py-1 px-6 text-xs font-semibold bg-slate-200' key = { index } onClick = { ( ) => loadProductByCategory ( category ) } > { category } </ button >
59
+ ) )
60
+ }
61
+ </ div >
62
+ < div className = 'w-full flex justify-center items-center pt-4' >
63
+ < input type = "search" name = "search" value = { search } placeholder = 'Search Products' className = 'w-[50%] py-1 px-1 focus:outline-blue-300' onChange = { ( e ) => setSearch ( e . target . value ) } />
64
+ </ div >
65
+
66
+ < div className = 'w-full flex justify-around items-center my-3' >
67
+ < button className = 'py-1 px-4 rounded-md border-2 border-slate-50 text-xs' onClick = { ( ) => loadProductByPrice ( 0 , 50 ) } > 0-50</ button >
68
+ < button className = 'py-1 px-4 rounded-md border-2 border-slate-50 text-xs' onClick = { ( ) => loadProductByPrice ( 51 , 100 ) } > 51-100</ button >
69
+ < button className = 'py-1 px-4 rounded-md border-2 border-slate-50 text-xs' onClick = { ( ) => loadProductByPrice ( 101 , 150 ) } > 101-150</ button >
70
+ < button className = 'py-1 px-4 rounded-md border-2 border-slate-50 text-xs' onClick = { ( ) => loadProductByPrice ( 151 , 200 ) } > 151-200</ button >
71
+ < button className = 'py-1 px-4 rounded-md border-2 border-slate-50 text-xs' onClick = { ( ) => loadProductByPrice ( 201 , 250 ) } > 201-250</ button >
72
+ < button className = 'py-1 px-4 rounded-md border-2 border-slate-50 text-xs' onClick = { ( ) => loadProductByPrice ( 251 , 500 ) } > 251-500</ button >
73
+ < button className = 'py-1 px-4 rounded-md border-2 border-slate-50 text-xs' onClick = { ( ) => loadProductByPrice ( 501 , 1000 ) } > 501-1000</ button >
74
+ </ div >
75
+ {
76
+ products . length < 1 && < div className = 'w-full bg-slate-800 flex justify-center items-center py-3' > < span className = ' font-semibold text-center text-slate-50' > No Product(s) Found</ span > </ div >
77
+ }
78
+ < div className = 'w-full grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 px-3 py-3 gap-x-3 gap-y-8 place-items-center' >
79
+ {
80
+ products && products . map ( product => (
81
+ < div key = { product . id } className = 'w-full shadow-md shadow-slate-900 p-2 bg-white flex flex-col justify-center items-center' >
82
+ < span className = 'block font-semibold text-md' > { product . title . substring ( 0 , 20 ) } </ span >
83
+ < img src = { product . image } alt = { product . title } className = 'h-[150px] object-cover' />
84
+ < span className = 'text-xs font-semibold border-2 border-slate-300 py-1 px-1 mt-1' > ${ product . price } </ span >
85
+ </ div >
86
+ ) )
87
+ }
88
+ </ div >
89
+
90
+ </ div >
91
+ )
92
+ }
93
+
94
+ export default Home
0 commit comments