UPDATE: Changed Search Bar to use button press instead of debouncing
This commit is contained in:
@@ -1,50 +1,40 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState } from "react";
|
||||||
import Input from "./Input";
|
import Input from "./Input";
|
||||||
import { Search as SearchIcon } from "lucide-react";
|
import { Search as SearchIcon } from "lucide-react";
|
||||||
|
|
||||||
const SearchBar: React.FC = () => {
|
const SearchBar: React.FC = () => {
|
||||||
const [searchQuery, setSearchQuery] = useState("");
|
const [searchQuery, setSearchQuery] = useState("");
|
||||||
const [debouncedQuery, setDebouncedQuery] = useState(searchQuery);
|
|
||||||
|
|
||||||
// Debounce the search query
|
const handleSearch = async () => {
|
||||||
useEffect(() => {
|
if (searchQuery.trim()) {
|
||||||
const timer = setTimeout(() => {
|
try {
|
||||||
setDebouncedQuery(searchQuery);
|
const response = await fetch("/api/search", {
|
||||||
}, 500); // Wait 500ms after user stops typing
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ query: searchQuery }),
|
||||||
|
});
|
||||||
|
|
||||||
return () => clearTimeout(timer);
|
const data = await response.json();
|
||||||
}, [searchQuery]);
|
console.log("Search results:", data);
|
||||||
|
// Handle the search results here
|
||||||
// Perform search when debounced query changes
|
} catch (error) {
|
||||||
useEffect(() => {
|
console.error("Error performing search:", error);
|
||||||
if (debouncedQuery.trim()) {
|
}
|
||||||
const fetchSearchResults = async () => {
|
|
||||||
try {
|
|
||||||
const response = await fetch("/api/search", {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ query: debouncedQuery }), // <-- Fixed payload
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
console.log("Search results:", data);
|
|
||||||
// Handle the search results here
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error performing search:", error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fetchSearchResults(); // Call the async function
|
|
||||||
}
|
}
|
||||||
}, [debouncedQuery]);
|
};
|
||||||
|
|
||||||
|
|
||||||
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setSearchQuery(e.target.value);
|
setSearchQuery(e.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||||
|
if (e.key === "Enter") {
|
||||||
|
handleSearch();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id="search-bar" className="flex items-center">
|
<div id="search-bar" className="flex items-center">
|
||||||
<Input
|
<Input
|
||||||
@@ -52,9 +42,9 @@ const SearchBar: React.FC = () => {
|
|||||||
placeholder="Search..."
|
placeholder="Search..."
|
||||||
value={searchQuery}
|
value={searchQuery}
|
||||||
onChange={handleSearchChange}
|
onChange={handleSearchChange}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
extraClasses="pr-[30px] focus:outline-none focus:border-purple-500 focus:w-[30vw]"
|
extraClasses="pr-[30px] focus:outline-none focus:border-purple-500 focus:w-[30vw]"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<SearchIcon className="-translate-x-[28px] top-1/2 h-6 w-6 text-white" />
|
<SearchIcon className="-translate-x-[28px] top-1/2 h-6 w-6 text-white" />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user