diff --git a/frontend/src/components/Home/Banner.js b/frontend/src/components/Home/Banner.js index 60eed20..64a80a1 100644 --- a/frontend/src/components/Home/Banner.js +++ b/frontend/src/components/Home/Banner.js @@ -1,15 +1,21 @@ import React from "react"; import logo from "../../imgs/logo.png"; +import SearchBox from "./SearchBox" + + const Banner = () => { return (
- +
- A place to - get - the cool stuff. +
+ A place to + get + + the cool stuff. +
diff --git a/frontend/src/components/Home/SearchBox.js b/frontend/src/components/Home/SearchBox.js new file mode 100644 index 0000000..bf3ed43 --- /dev/null +++ b/frontend/src/components/Home/SearchBox.js @@ -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 ( +
+ + + + {loading &&
Loading...
} {/* Show loading indicator while fetching */} + +
+ {items.length > 0 ? ( + items.map((item) => ( +
+

{item.title}

+

{item.description}

+
+ )) + ) : ( +
No results found
+ )} +
+
+ ); +}; + +export default SearchBox; \ No newline at end of file diff --git a/frontend/src/custom.scss b/frontend/src/custom.scss index 0b4d757..f7d8c81 100644 --- a/frontend/src/custom.scss +++ b/frontend/src/custom.scss @@ -59,3 +59,34 @@ body { .user-info { min-width: 800px; } + + +.search_wrapper { + display: flex; + justify-content: center; + align-items: center; + span { + margin: 0 3px; + } +} + +.search_box { + background: transparent; + position: relative; + width: fit-content; + input { + border: none; + outline: none; + padding: 10px 0; + text-indent: 5px; + border-radius: 7px; + } + + i { + position: absolute; + right: 0; + top: 50%; + transform: translate(-50%, -50%); + color: #000; + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..cd574b2 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "lodash": "^4.17.21" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..bb4db65 --- /dev/null +++ b/yarn.lock @@ -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==