diff --git a/frontend/src/components/Layout/SearchBar.tsx b/frontend/src/components/Layout/SearchBar.tsx index 93f3da1..1a20502 100644 --- a/frontend/src/components/Layout/SearchBar.tsx +++ b/frontend/src/components/Layout/SearchBar.tsx @@ -1,50 +1,40 @@ -import React, { useState, useEffect } from "react"; +import React, { useState } from "react"; import Input from "./Input"; import { Search as SearchIcon } from "lucide-react"; const SearchBar: React.FC = () => { const [searchQuery, setSearchQuery] = useState(""); - const [debouncedQuery, setDebouncedQuery] = useState(searchQuery); - // Debounce the search query - useEffect(() => { - const timer = setTimeout(() => { - setDebouncedQuery(searchQuery); - }, 500); // Wait 500ms after user stops typing + const handleSearch = async () => { + if (searchQuery.trim()) { + try { + const response = await fetch("/api/search", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ query: searchQuery }), + }); - return () => clearTimeout(timer); - }, [searchQuery]); - - // Perform search when debounced query changes - useEffect(() => { - 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 + const data = await response.json(); + console.log("Search results:", data); + // Handle the search results here + } catch (error) { + console.error("Error performing search:", error); + } } - }, [debouncedQuery]); - + }; const handleSearchChange = (e: React.ChangeEvent) => { setSearchQuery(e.target.value); }; + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === "Enter") { + handleSearch(); + } + }; + return ( );