diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 52147d1..331682e 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -9,6 +9,7 @@ import UserPage from "./pages/UserPage"; import ResetPasswordPage from "./pages/ResetPasswordPage"; import CategoryPage from "./pages/CategoryPage"; import CategoriesPage from "./pages/CategoriesPage"; +import FoundPage from "./pages/FoundPage"; function App() { const [isLoggedIn, setIsLoggedIn] = useState(false); @@ -46,7 +47,7 @@ function App() { }> }> }> - + }> } /> diff --git a/frontend/src/components/Layout/Input.tsx b/frontend/src/components/Layout/Input.tsx index ffc671d..36cf9b2 100644 --- a/frontend/src/components/Layout/Input.tsx +++ b/frontend/src/components/Layout/Input.tsx @@ -12,6 +12,7 @@ const Input: React.FC = ({ value = "", extraClasses = "", onChange = () => { }, + onKeyDown = () => { }, children, ...props // all other HTML input props }) => { @@ -24,6 +25,7 @@ const Input: React.FC = ({ 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`} /> diff --git a/frontend/src/components/Layout/SearchBar.tsx b/frontend/src/components/Layout/SearchBar.tsx index 93f3da1..66bf31f 100644 --- a/frontend/src/components/Layout/SearchBar.tsx +++ b/frontend/src/components/Layout/SearchBar.tsx @@ -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) => { + 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) => { 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]" /> + + ); diff --git a/frontend/src/pages/FoundPage.tsx b/frontend/src/pages/FoundPage.tsx new file mode 100644 index 0000000..5025275 --- /dev/null +++ b/frontend/src/pages/FoundPage.tsx @@ -0,0 +1,60 @@ +import React from 'react' +import { useLocation, useNavigate } from "react-router-dom"; + +const FoundPage: React.FC = ({}) => { + const location = useLocation(); + const navigate = useNavigate(); + const { searchResults, query } = location.state || { searchResults: null, query: "" }; + if (!searchResults) { + return ( +
+

No results found for "{query}"

+ +
+ ); + } + + return ( +
+

Search Results for "{query}"

+ +
+

Categories

+
    + {searchResults.categories.map((category: any, index: number) => ( +
  • {category.category_name}
  • + ))} +
+
+ +
+

Users

+
    + {searchResults.users.map((user: any, index: number) => ( +
  • {user.username} {user.is_live ? "🔴" : ""}
  • + ))} +
+
+ +
+

Streams

+
    + {searchResults.streams.map((stream: any, index: number) => ( +
  • + {stream.title} - {stream.num_viewers} viewers +
  • + ))} +
+
+ + +
+ ); + }; + + +export default FoundPage