Skip to content

Commit 74ebd41

Browse files
author
WebDeveloperGuide
committed
Products > Search Functionality added
1 parent 24b46b1 commit 74ebd41

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

web_admin/src/App.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ textarea.form-control, select.form-control{
6161
background-color: #172d88;
6262
box-shadow: none;
6363
color:#fff;
64+
}
65+
66+
.form-control{
67+
height: calc(2.25rem + 4px);
6468
}

web_admin/src/components/pages/product/Products.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import './product.css';
1212
const Products = () => {
1313
const dispatch = useDispatch();
1414
const [currentPage, setCurrentPage] = useState(0);
15+
const [searchTerm, setSearchTerm] = useState('');
1516

1617
const productList = useSelector((state) => state.productList);
1718
const { loading, error, products, numOfPages, sortBy, searchText } = productList;
@@ -28,11 +29,22 @@ const Products = () => {
2829
dispatch(listProducts(pageNum,productsPerPage, sortBy, searchText));
2930
}, [productsPerPage]);
3031

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+
3142
const handleSortBy = (e) => {
3243
const sortByValue = e.target.value;
3344
setCurrentPage(0);
3445
dispatch(listProducts(pageNum,productsPerPage, sortByValue, searchText));
3546
}
47+
3648

3749
return(
3850
<>
@@ -47,15 +59,30 @@ const Products = () => {
4759
<div className="card">
4860
<div className="card-body">
4961
<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>
5384
<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+
5986
</div>
6087
<p className="card-description">
6188
</p>

web_services/routes/product.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,28 @@ router.get("/", async (req,res)=>{
4242

4343
const itemPerPage = parseInt(req.query.limit || "10"); //Products per page
4444
const pageNum = parseInt(req.query.page || "0"); //Products page number
45-
const sortByVal = (req.query.sortBy || "_id"); //Products page number
45+
const sortByVal = (req.query.sortBy || "_id"); //Products sort by
46+
const searchText = (req.query.searchText || ""); //Products search text
47+
4648

47-
var sortObject = {};
49+
let sortObject = {};
50+
let searchObject = {};
4851
sortByField = sortByVal;
4952
if(sortByVal == 'name'){
5053
sortByField = 'title';
5154
}
5255

56+
if(searchText !== ''){
57+
searchObject = {$or:[{ title: { $regex: searchText, $options:'i' }}
58+
,{ description: { $regex: searchText, $options:'i' } }
59+
]};
60+
}
61+
5362
sortObject[sortByField] = 1;
5463

5564

56-
const totalProducts = await Product.countDocuments({});
57-
const productData = await Product.find({}).sort(sortObject).limit(itemPerPage).skip(itemPerPage * pageNum);
65+
const totalProducts = await Product.countDocuments(searchObject);
66+
const productData = await Product.find(searchObject).sort(sortObject).limit(itemPerPage).skip(itemPerPage * pageNum);
5867

5968

6069

0 commit comments

Comments
 (0)