|
|
@ -23,13 +23,16 @@ export const Search: Component<{ initialQueryValue?: string }> = ({
|
|
|
|
|
|
|
|
|
|
|
|
const { isLoading, error, data } = useQuery({
|
|
|
|
const { isLoading, error, data } = useQuery({
|
|
|
|
queryKey: ["search", "suggestions", searchQueryDebounced],
|
|
|
|
queryKey: ["search", "suggestions", searchQueryDebounced],
|
|
|
|
queryFn: () => client.getSearchSuggestions(searchQueryDebounced),
|
|
|
|
queryFn: () => {
|
|
|
|
enabled: searchQueryDebounced.length !== 0
|
|
|
|
if (searchQueryDebounced.length === 0) return [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return client.getSearchSuggestions(searchQueryDebounced);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const handleSubmit = useCallback(() => {
|
|
|
|
const submit = useCallback((query: string) => {
|
|
|
|
router.push(`/results?search_query=${searchQuery}`);
|
|
|
|
router.push(`/results?search_query=${query}`);
|
|
|
|
}, [searchQuery]);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const suggestions = useMemo(
|
|
|
|
const suggestions = useMemo(
|
|
|
|
() =>
|
|
|
|
() =>
|
|
|
@ -41,21 +44,26 @@ export const Search: Component<{ initialQueryValue?: string }> = ({
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<form onSubmit={() => submit(searchQuery)}>
|
|
|
|
<Autocomplete
|
|
|
|
<Autocomplete
|
|
|
|
isClearable
|
|
|
|
isClearable
|
|
|
|
name="search-bar"
|
|
|
|
name="search_query"
|
|
|
|
value={searchQuery}
|
|
|
|
value={searchQuery}
|
|
|
|
isLoading={isLoading}
|
|
|
|
isLoading={isLoading}
|
|
|
|
defaultInputValue={initialQueryValue}
|
|
|
|
defaultInputValue={initialQueryValue}
|
|
|
|
onValueChange={setSearchQuery}
|
|
|
|
onValueChange={setSearchQuery}
|
|
|
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
|
|
|
|
submit(searchQuery);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
startContent={<SearchIcon className="text-xl" />}
|
|
|
|
startContent={<SearchIcon className="text-xl" />}
|
|
|
|
defaultItems={suggestions}
|
|
|
|
defaultItems={suggestions}
|
|
|
|
onSelectionChange={(key) => {
|
|
|
|
onSelectionChange={(key) => {
|
|
|
|
if (key === null) return;
|
|
|
|
if (key === null) return;
|
|
|
|
|
|
|
|
|
|
|
|
setSearchQuery(key.toString());
|
|
|
|
setSearchQuery(key.toString());
|
|
|
|
handleSubmit();
|
|
|
|
submit(key.toString());
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
errorMessage={error !== null ? error.toString() : ""}
|
|
|
|
errorMessage={error !== null ? error.toString() : ""}
|
|
|
|
isInvalid={error !== null}
|
|
|
|
isInvalid={error !== null}
|
|
|
|