generated from trywilco/Anythink-Market-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Wilcolab/search-box
add search box with filter
- Loading branch information
Showing
5 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import agentObj from '../../agent'; // Import the API agent | ||
import { debounce } from 'lodash'; // Use lodash for debouncing | ||
|
||
const SearchBox = () => { | ||
const [searchTerm, setSearchTerm] = useState(''); // State to store the search input | ||
const [items, setItems] = useState([]); // State to store the search results | ||
const [loading, setLoading] = useState(false); // State to handle loading state | ||
|
||
// Debounced function to fetch items based on search term | ||
const debouncedSearch = debounce(async (term) => { | ||
if (term.length >= 3) { | ||
setLoading(true); | ||
try { | ||
const result = await agentObj.Items.all(1, term); // Fetch items based on search term | ||
setItems(result.items); // Update items state with the search results | ||
} catch (error) { | ||
console.error('Error fetching search results:', error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
} else { | ||
setItems([]); // Clear results if search term is less than 3 characters | ||
} | ||
}, 500); // 500ms debounce delay | ||
|
||
// Handle input change | ||
const handleInputChange = (e) => { | ||
setSearchTerm(e.target.value); // Update search term | ||
}; | ||
|
||
// Use effect to trigger debounced search when searchTerm changes | ||
useEffect(() => { | ||
debouncedSearch(searchTerm); // Call debouncedSearch whenever the searchTerm changes | ||
return () => { | ||
debouncedSearch.cancel(); // Clean up debounce on unmount | ||
}; | ||
}, [searchTerm]); // Depend on searchTerm to trigger effect | ||
|
||
return ( | ||
<div className="search_box"> | ||
<input | ||
type="text" | ||
id="search-box" | ||
value={searchTerm} | ||
onChange={handleInputChange} | ||
placeholder="What is it you truly desire?" | ||
/> | ||
<i className="bi bi-search"></i> | ||
|
||
{loading && <div>Loading...</div>} {/* Show loading indicator while fetching */} | ||
|
||
<div className="search_results"> | ||
{items.length > 0 ? ( | ||
items.map((item) => ( | ||
<div key={item.slug} className="search_result_item"> | ||
<h3>{item.title}</h3> | ||
<p>{item.description}</p> | ||
</div> | ||
)) | ||
) : ( | ||
<div>No results found</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchBox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"lodash": "^4.17.21" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
lodash@^4.17.21: | ||
version "4.17.21" | ||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" | ||
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== |