mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
31 lines
731 B
TypeScript
31 lines
731 B
TypeScript
import beerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
|
import useSWR from 'swr';
|
|
import { z } from 'zod';
|
|
|
|
const useBeerPostSearch = (query: string | undefined) => {
|
|
const { data, isLoading, error } = useSWR(
|
|
`/api/beers/search?search=${query}`,
|
|
async (url) => {
|
|
if (!query) return [];
|
|
|
|
const response = await fetch(url);
|
|
if (!response.ok) {
|
|
throw new Error(response.statusText);
|
|
}
|
|
|
|
const json = await response.json();
|
|
const result = z.array(beerPostQueryResult).parse(json);
|
|
|
|
return result;
|
|
},
|
|
);
|
|
|
|
return {
|
|
searchResults: data,
|
|
searchError: error as Error | undefined,
|
|
isLoading,
|
|
};
|
|
};
|
|
|
|
export default useBeerPostSearch;
|