diff --git a/src/app/events/search/EventSearch.tsx b/src/app/events/search/EventSearch.tsx new file mode 100644 index 00000000..9748825d --- /dev/null +++ b/src/app/events/search/EventSearch.tsx @@ -0,0 +1,75 @@ +'use client'; +import { useState, ChangeEvent, FormEvent, useEffect } from 'react'; +import { useRouter, useSearchParams } from 'next/navigation'; +import { api } from '@src/trpc/react'; +import type { Event } from '@src/server/db/models'; +import { type Session } from 'next-auth'; +import EventCard from '@src/components/events/EventCard'; + +interface EventSearchComponentProps { + userSearch: string; + session: Session | null; +} + +export const EventSearchComponent = ({ + userSearch, + session, +}: EventSearchComponentProps) => { + const router = useRouter(); + const searchParams = useSearchParams(); + const [search, setSearch] = useState(userSearch); + + const { data } = api.event.byName.useQuery( + { name: search }, + { enabled: !!search }, + ); + + const handleSearchChange = (e: ChangeEvent) => { + setSearch(e.target.value); + }; + + const handleSearchSubmit = (e: FormEvent) => { + e.preventDefault(); + router.push(`/search?query=${search}`); + }; + + useEffect(() => { + const query = searchParams.get('query'); + if (query) { + setSearch(query); + } + }, [searchParams]); + + if (!data) { + return

; + } + if (data.length === 0) { + return ( +

+ No Search Results Found +
+ ); + } + + return ( +
+
+ + +
+
+ {data.map((event: Event) => ( + + ))} +
+
+ ); +}; \ No newline at end of file diff --git a/src/server/api/routers/event.ts b/src/server/api/routers/event.ts index e883f40e..fdae0685 100644 --- a/src/server/api/routers/event.ts +++ b/src/server/api/routers/event.ts @@ -280,4 +280,4 @@ export const eventRouter = createTRPCRouter({ throw e; } }), -}); +}); \ No newline at end of file