FEAT: Results Page from Search Bar
This commit is contained in:
@@ -12,6 +12,7 @@ const Input: React.FC<InputProps> = ({
|
||||
value = "",
|
||||
extraClasses = "",
|
||||
onChange = () => { },
|
||||
onKeyDown = () => { },
|
||||
children,
|
||||
...props // all other HTML input props
|
||||
}) => {
|
||||
@@ -24,6 +25,7 @@ const Input: React.FC<InputProps> = ({
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
onKeyDown={onKeyDown}
|
||||
{...props}
|
||||
className={`${extraClasses} relative p-2 rounded-[1rem] w-[20vw] focus:w-[21vw] bg-black/40 border border-gray-300 focus:border-purple-500 focus:outline-purple-500 text-center text-white text-xl transition-all`}
|
||||
/>
|
||||
|
||||
@@ -1,45 +1,56 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import Input from "./Input";
|
||||
import { Search as SearchIcon } from "lucide-react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
const SearchBar: React.FC = () => {
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [debouncedQuery, setDebouncedQuery] = useState(searchQuery);
|
||||
//const [debouncedQuery, setDebouncedQuery] = useState(searchQuery);
|
||||
const navigate = useNavigate();
|
||||
|
||||
// Debounce the search query
|
||||
{/*
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
setDebouncedQuery(searchQuery);
|
||||
}, 500); // Wait 500ms after user stops typing
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [searchQuery]);
|
||||
}, [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 handleSearch = async () => {
|
||||
if (!searchQuery.trim()) return;
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/search", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ query: searchQuery }), // <-- Fixed payload
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
console.log("Search results:", data);
|
||||
|
||||
navigate("/results", { state: { searchResults: data, query: searchQuery } });
|
||||
|
||||
// Handle the search results here
|
||||
} catch (error) {
|
||||
console.error("Error performing search:", error);
|
||||
}
|
||||
}, [debouncedQuery]);
|
||||
|
||||
};
|
||||
|
||||
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
if (e.key === "Enter") {
|
||||
console.log("Key pressed:", e.key); // Debugging
|
||||
|
||||
e.preventDefault(); // Prevent unintended form submission
|
||||
handleSearch(); // Trigger search when Enter key is pressed
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setSearchQuery(e.target.value);
|
||||
@@ -52,9 +63,12 @@ const SearchBar: React.FC = () => {
|
||||
placeholder="Search..."
|
||||
value={searchQuery}
|
||||
onChange={handleSearchChange}
|
||||
onKeyDown={handleKeyPress}
|
||||
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" />
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user