@@ -12,6 +12,7 @@ import './product.css';
12
12
const Products = ( ) => {
13
13
const dispatch = useDispatch ( ) ;
14
14
const [ currentPage , setCurrentPage ] = useState ( 0 ) ;
15
+ const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
15
16
16
17
const productList = useSelector ( ( state ) => state . productList ) ;
17
18
const { loading, error, products, numOfPages, sortBy, searchText } = productList ;
@@ -28,11 +29,22 @@ const Products = () => {
28
29
dispatch ( listProducts ( pageNum , productsPerPage , sortBy , searchText ) ) ;
29
30
} , [ productsPerPage ] ) ;
30
31
32
+ //Call Function after stop typing text
33
+ useEffect ( ( ) => {
34
+ const delaySearchFunc = setTimeout ( ( ) => {
35
+ setCurrentPage ( 0 ) ;
36
+ dispatch ( listProducts ( pageNum , productsPerPage , sortBy , searchTerm ) ) ;
37
+ } , 1500 )
38
+
39
+ return ( ) => clearTimeout ( delaySearchFunc )
40
+ } , [ searchTerm ] )
41
+
31
42
const handleSortBy = ( e ) => {
32
43
const sortByValue = e . target . value ;
33
44
setCurrentPage ( 0 ) ;
34
45
dispatch ( listProducts ( pageNum , productsPerPage , sortByValue , searchText ) ) ;
35
46
}
47
+
36
48
37
49
return (
38
50
< >
@@ -47,15 +59,30 @@ const Products = () => {
47
59
< div className = "card" >
48
60
< div className = "card-body" >
49
61
< h4 className = "card-title" > Products</ h4 >
50
- < Link to = "/product/add" className = "btn btn-outline-primary btn-fw float-right" >
51
- Add Product
52
- </ Link >
62
+ < div className = "row" >
63
+ < div className = "col-md-12" >
64
+ < div className = "form-group row" >
65
+ < div className = "offset-md-2 col-sm-4" >
66
+ < input type = "text" placeholder = "Search" className = "form-control"
67
+ name = "search" onChange = { ( e ) => setSearchTerm ( e . target . value ) } />
68
+ </ div >
69
+ < div className = "col-sm-3 float-right" >
70
+ < select className = "form-select form-control" aria-label = "Sort By" onChange = { handleSortBy } >
71
+ < option value = "" > Sort By</ option >
72
+ < option value = "name" > Name</ option >
73
+ < option value = "price" > Price</ option >
74
+ </ select >
75
+ </ div >
76
+ < div className = "col-sm-2" >
77
+ < Link to = "/product/add" className = "btn btn-outline-primary btn-fw float-right" >
78
+ Add Product
79
+ </ Link >
80
+ </ div >
81
+ </ div >
82
+ </ div >
83
+ </ div >
53
84
< div className = "float-right mr-5" >
54
- < select className = "form-select" aria-label = "Sort By" onChange = { handleSortBy } >
55
- < option value = "" > Sort By</ option >
56
- < option value = "name" > Name</ option >
57
- < option value = "price" > Price</ option >
58
- </ select >
85
+
59
86
</ div >
60
87
< p className = "card-description" >
61
88
</ p >
0 commit comments