Skip to content

Commit c760286

Browse files
added back search button to click and do something
1 parent 5bd8cad commit c760286

File tree

1 file changed

+57
-37
lines changed

1 file changed

+57
-37
lines changed

src/components/searchbar.tsx

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import { useState, useCallback, useRef, useEffect } from "react";
44
import axios, { AxiosError } from "axios";
55
import { Search } from "lucide-react";
6-
import debounce from 'debounce';
7-
import { useRouter } from "next/navigation";
6+
import debounce from "debounce";
7+
import { useRouter } from "next/navigation";
88
import { Input } from "@/components/ui/input";
99

10-
function SearchBar () {
11-
const router = useRouter();
10+
function SearchBar() {
11+
const router = useRouter();
1212
const [searchText, setSearchText] = useState("");
1313
const [suggestions, setSuggestions] = useState<string[]>([]);
1414
const [error, setError] = useState<string | null>(null);
@@ -25,12 +25,15 @@ function SearchBar () {
2525
});
2626

2727
const { subjects } = searchResponse.data;
28-
const suggestionList = subjects.map((subjectObj: { subject: string }) => subjectObj.subject);
28+
const suggestionList = subjects.map(
29+
(subjectObj: { subject: string }) => subjectObj.subject,
30+
);
2931
setSuggestions(suggestionList);
3032
setError(null);
3133
} catch (error) {
3234
const typedError = error as AxiosError<{ message?: string }>;
33-
const errorMessage = typedError.response?.data?.message ?? "Error fetching suggestions";
35+
const errorMessage =
36+
typedError.response?.data?.message ?? "Error fetching suggestions";
3437
setError(errorMessage);
3538
} finally {
3639
setLoading(false);
@@ -39,7 +42,7 @@ function SearchBar () {
3942
setSuggestions([]);
4043
}
4144
}, 500),
42-
[]
45+
[],
4346
);
4447

4548
const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@@ -48,7 +51,7 @@ function SearchBar () {
4851
if (text.length <= 1) {
4952
setSuggestions([]);
5053
}
51-
debouncedSearch(text);
54+
debouncedSearch(text);
5255
};
5356

5457
const handleSelectSuggestion = async (suggestion: string) => {
@@ -58,7 +61,10 @@ function SearchBar () {
5861
};
5962

6063
const handleClickOutside = (event: MouseEvent) => {
61-
if (suggestionsRef.current && !suggestionsRef.current.contains(event.target as Node)) {
64+
if (
65+
suggestionsRef.current &&
66+
!suggestionsRef.current.contains(event.target as Node)
67+
) {
6268
setSuggestions([]);
6369
}
6470
};
@@ -75,38 +81,52 @@ function SearchBar () {
7581
<form className="w-full max-w-xl">
7682
<div className="relative">
7783
<Input
78-
type="text"
79-
value={searchText}
84+
type="text"
85+
value={searchText}
8086
onChange={handleSearchChange}
81-
placeholder="Search..."
82-
className={`w-full rounded-xl border px-4 py-6 pr-10 bg-[#7480FF] placeholder:text-white text-white opacity-50 shadow-sm focus:outline-none focus:ring-2 ${loading ? 'opacity-70' : ''}`}
87+
placeholder="Search..."
88+
className={`w-full rounded-xl border bg-[#7480FF] px-4 py-6 pr-10 text-white opacity-50 shadow-sm placeholder:text-white focus:outline-none focus:ring-2 ${loading ? "opacity-70" : ""}`}
8389
/>
84-
<button type="submit" className="absolute inset-y-0 right-0 flex items-center pr-3" disabled> {/* disabled={loading} */}
90+
<button
91+
type="submit"
92+
className="absolute inset-y-0 right-0 flex items-center pr-3"
93+
disabled={loading}
94+
>
95+
{" "}
96+
{/* disabled={loading} replace with disabled */}
8597
<Search className="h-5 w-5 text-white opacity-50" />
8698
</button>
87-
{loading && (
88-
<div className="absolute z-20 w-full max-w-xl border bg-white border-[#7480FF] dark:bg-[#030712] rounded-md mt-2 p-2 text-center">
89-
Loading suggestions...
90-
</div>
91-
)}
92-
{(suggestions.length > 0 || error) && !loading && (
93-
<ul ref={suggestionsRef} className="absolute mx-0.5 md:mx-0 w-full text-center max-w-xl z-20 border bg-white border-[#7480FF] dark:bg-[#030712] rounded-md mt-2">
94-
{error ? (
95-
<li className="p-2 text-red">{error}</li>
96-
) : (
97-
suggestions.map((suggestion, index) => (
98-
<li
99-
key={index}
100-
onClick={() => handleSelectSuggestion(suggestion)}
101-
className="cursor-pointer p-2 truncate hover:opacity-50"
102-
style={{ width: '100%', overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' }}
103-
>
104-
{suggestion}
105-
</li>
106-
))
107-
)}
108-
</ul>
109-
)}
99+
{loading && (
100+
<div className="absolute z-20 mt-2 w-full max-w-xl rounded-md border border-[#7480FF] bg-white p-2 text-center dark:bg-[#030712]">
101+
Loading suggestions...
102+
</div>
103+
)}
104+
{(suggestions.length > 0 || error) && !loading && (
105+
<ul
106+
ref={suggestionsRef}
107+
className="absolute z-20 mx-0.5 mt-2 w-full max-w-xl rounded-md border border-[#7480FF] bg-white text-center dark:bg-[#030712] md:mx-0"
108+
>
109+
{error ? (
110+
<li className="text-red p-2">{error}</li>
111+
) : (
112+
suggestions.map((suggestion, index) => (
113+
<li
114+
key={index}
115+
onClick={() => handleSelectSuggestion(suggestion)}
116+
className="cursor-pointer truncate p-2 hover:opacity-50"
117+
style={{
118+
width: "100%",
119+
overflow: "hidden",
120+
whiteSpace: "nowrap",
121+
textOverflow: "ellipsis",
122+
}}
123+
>
124+
{suggestion}
125+
</li>
126+
))
127+
)}
128+
</ul>
129+
)}
110130
</div>
111131
</form>
112132
</div>

0 commit comments

Comments
 (0)