|
1 | 1 | "use client";
|
| 2 | + |
2 | 3 | import { useState } from "react";
|
| 4 | +import axios from "axios"; |
3 | 5 | import { Search } from "lucide-react";
|
| 6 | +import Cryptr from "cryptr"; |
| 7 | +import { useRouter } from "next/navigation"; |
| 8 | + |
| 9 | +const cryptr = new Cryptr( |
| 10 | + process.env.NEXT_PUBLIC_CRYPTO_SECRET ?? "default_crypto_secret" |
| 11 | +); |
4 | 12 |
|
5 | 13 | const SearchBar = () => {
|
| 14 | + const router = useRouter(); |
6 | 15 | const [searchText, setSearchText] = useState("");
|
| 16 | + const [suggestions, setSuggestions] = useState<string[]>([]); |
| 17 | + const [error, setError] = useState<string | null>(null); |
| 18 | + |
| 19 | + const handleSearchChange = async (e: React.ChangeEvent<HTMLInputElement>) => { |
| 20 | + const text = e.target.value; |
| 21 | + setSearchText(text); |
| 22 | + |
| 23 | + if (text.length > 1) { |
| 24 | + try { |
| 25 | + const searchResponse = await axios.get("http://localhost:3000/api/search", { |
| 26 | + params: { text }, |
| 27 | + }); |
| 28 | + |
| 29 | + const { res: encryptedSearchResponse } = searchResponse.data; |
| 30 | + const decryptedSearchResponse = cryptr.decrypt(encryptedSearchResponse); |
| 31 | + // console.log("Decrypted Search Response:", decryptedSearchResponse); |
| 32 | + |
| 33 | + const { subjects } = JSON.parse(decryptedSearchResponse); |
| 34 | + const suggestionList = subjects.map((subjectObj: { subject: string }) => subjectObj.subject); |
| 35 | + setSuggestions(suggestionList); |
| 36 | + } catch (error) { |
| 37 | + setError("Error fetching suggestions"); |
| 38 | + } |
| 39 | + } else { |
| 40 | + setSuggestions([]); |
| 41 | + } |
| 42 | + }; |
7 | 43 |
|
8 |
| - const handleSearch = (event: React.FormEvent) => { |
9 |
| - event.preventDefault(); |
10 |
| - console.log("Searching for:", searchText); |
| 44 | + const handleSelectSuggestion = async (suggestion: string) => { |
| 45 | + setSearchText(suggestion); |
| 46 | + setSuggestions([]); |
| 47 | + router.push(`/catalogue?subject=${encodeURIComponent(suggestion)}`); |
11 | 48 | };
|
12 | 49 |
|
13 | 50 | return (
|
14 |
| - <div className="flex min-h-screen items-center justify-center bg-gray-50"> |
15 |
| - <form onSubmit={handleSearch} className="w-full max-w-md"> |
| 51 | + <div className="flex min-h-screen items-center justify-center bg-gray-50 flex-col"> |
| 52 | + <form className="w-full max-w-md"> |
16 | 53 | <div className="relative">
|
17 |
| - <input |
18 |
| - type="text" |
19 |
| - value={searchText} |
20 |
| - onChange={(e) => setSearchText(e.target.value)} |
21 |
| - placeholder="Search..." |
22 |
| - className="w-full rounded-md border border-gray-300 px-4 py-2 pr-10 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500" |
23 |
| - /> |
24 |
| - <button |
25 |
| - type="submit" |
26 |
| - className="absolute inset-y-0 right-0 flex items-center pr-3" |
27 |
| - > |
| 54 | + |
| 55 | + <input type="text" value={searchText} onChange={handleSearchChange} |
| 56 | + placeholder="Search..." className="w-full rounded-md border border-gray-300 px-4 py-2 pr-10 shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500"/> |
| 57 | + |
| 58 | + <button type="submit" className="absolute inset-y-0 right-0 flex items-center pr-3"> |
28 | 59 | <Search className="h-5 w-5 text-gray-400" />
|
29 | 60 | </button>
|
| 61 | + |
30 | 62 | </div>
|
| 63 | + {suggestions.length > 0 && ( |
| 64 | + <ul className="absolute z-10 w-full bg-white border border-gray-300 rounded-md mt-2"> |
| 65 | + {suggestions.map((suggestion, index) => ( |
| 66 | + |
| 67 | + <li key={index} onClick={() => handleSelectSuggestion(suggestion)} className="cursor-pointer p-2 hover:bg-gray-100"> |
| 68 | + {suggestion} |
| 69 | + </li> |
| 70 | + |
| 71 | + ))} |
| 72 | + </ul> |
| 73 | + )} |
31 | 74 | </form>
|
| 75 | + |
| 76 | + {error && <p className="mt-4 text-red">{error}</p>} |
32 | 77 | </div>
|
33 | 78 | );
|
34 | 79 | };
|
|
0 commit comments