diff --git a/src/components/searchBar/ClubSearchBar.tsx b/src/components/searchBar/ClubSearchBar.tsx index 7523c719..6e2ffa8d 100644 --- a/src/components/searchBar/ClubSearchBar.tsx +++ b/src/components/searchBar/ClubSearchBar.tsx @@ -21,6 +21,12 @@ export const ClubSearchBar = () => { setSearch={setSearch} searchResults={data || []} onClick={onClickSearchResult} + submitButton + submitLogic={() => { + if (data && data[0]) { + onClickSearchResult(data[0]); + } + }} /> ); }; diff --git a/src/components/searchBar/DebouncedSearchBar.tsx b/src/components/searchBar/DebouncedSearchBar.tsx index 395f898c..1fe89774 100644 --- a/src/components/searchBar/DebouncedSearchBar.tsx +++ b/src/components/searchBar/DebouncedSearchBar.tsx @@ -18,6 +18,8 @@ export type DebouncedSearchBarProps = { setSearch: Dispatch>; searchResults?: Array; onClick?: (input: T) => void; + submitButton?: boolean; + submitLogic?: () => void; }; export const DebouncedSearchBar = ({ placeholder, @@ -25,6 +27,8 @@ export const DebouncedSearchBar = ({ setSearch, searchResults, onClick, + submitButton, + submitLogic, }: DebouncedSearchBarProps) => { const [input, setInput] = useState(value ?? ''); const [focused, setFocused] = useState(false); @@ -48,6 +52,8 @@ export const DebouncedSearchBar = ({ onChange={handleSearch} onFocus={() => setFocused(true)} onBlur={() => setTimeout(() => setFocused(false), 300)} + submitButton={submitButton} + submitLogic={submitLogic} /> {input && focused && searchResults && searchResults.length > 0 && (
diff --git a/src/components/searchBar/EventClubSearchBar.tsx b/src/components/searchBar/EventClubSearchBar.tsx index afce98f5..0c65a28f 100644 --- a/src/components/searchBar/EventClubSearchBar.tsx +++ b/src/components/searchBar/EventClubSearchBar.tsx @@ -1,5 +1,5 @@ 'use client'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; import type { SelectClub as Club } from '@src/server/db/models'; import { api } from '@src/trpc/react'; import { DebouncedSearchBar } from './DebouncedSearchBar'; @@ -9,29 +9,30 @@ type EventClubSearchBarProps = { }; export const EventClubSearchBar = ({ addClub }: EventClubSearchBarProps) => { const [search, setSearch] = useState(''); - const [res, setRes] = useState([]); const utils = api.useUtils(); - const clubQuery = api.club.byName.useQuery( + const { data } = api.club.byName.useQuery( { name: search }, { enabled: !!search, }, ); - useEffect(() => { - if (clubQuery.data) { - setRes(clubQuery.data); - } - }, [clubQuery.data]); + const submit = (club: Club) => { + void utils.club.byId.prefetch({ id: club.id }); + addClub(club.id); + setSearch(''); + }; return ( { - void utils.club.byId.prefetch({ id: club.id }); - addClub(club.id); - setSearch(''); + searchResults={data || []} + onClick={submit} + submitButton + submitLogic={() => { + if (data && data[0]) { + submit(data[0]); + } }} /> ); diff --git a/src/components/searchBar/EventSearchBar.tsx b/src/components/searchBar/EventSearchBar.tsx index 04adb555..7e32cd98 100644 --- a/src/components/searchBar/EventSearchBar.tsx +++ b/src/components/searchBar/EventSearchBar.tsx @@ -1,5 +1,5 @@ 'use client'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { api } from '@src/trpc/react'; import type { SelectEvent as Event } from '@src/server/db/models'; @@ -8,26 +8,28 @@ import { DebouncedSearchBar } from './DebouncedSearchBar'; export const EventSearchBar = () => { const router = useRouter(); const [search, setSearch] = useState(''); - const [res, setRes] = useState([]); - const eventQuery = api.event.byName.useQuery( + const { data } = api.event.byName.useQuery( { name: search, sortByDate: true, }, { enabled: !!search }, ); - useEffect(() => { - if (eventQuery.data) setRes(eventQuery.data); - }, [eventQuery.data]); - + const onClickSearchResult = (event: Event) => { + router.push(`/event/${event.id}`); + }; return ( { - router.push(`/event/${event.id}`); + searchResults={data || []} + onClick={onClickSearchResult} + submitButton + submitLogic={() => { + if (data && data[0]) { + onClickSearchResult(data[0]); + } }} /> ); diff --git a/src/components/searchBar/UserSearchBar.tsx b/src/components/searchBar/UserSearchBar.tsx index 487d4f40..94fad809 100644 --- a/src/components/searchBar/UserSearchBar.tsx +++ b/src/components/searchBar/UserSearchBar.tsx @@ -1,42 +1,41 @@ 'use client'; -import { type SelectUserMetadata } from '@src/server/db/models'; import { api } from '@src/trpc/react'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; import { DebouncedSearchBar } from './DebouncedSearchBar'; type UserSearchBarProps = { passUser: (user: { id: string; name: string }) => void; }; -type User = { - name: string; -} & SelectUserMetadata; export const UserSearchBar = ({ passUser }: UserSearchBarProps) => { const [search, setSearch] = useState(''); - const [res, setRes] = useState([]); const userQuery = api.userMetadata.searchByName.useQuery( { name: search }, { enabled: !!search, }, ); - useEffect(() => { - if (userQuery.data) { - const newData = userQuery.data.map((val) => { + const formattedData = userQuery.isSuccess + ? userQuery.data.map((val) => { return { name: val.firstName + ' ' + val.lastName, ...val }; - }); - setRes(newData); - } - }, [userQuery.data]); + }) + : []; return ( { passUser({ id: user.id, name: user.name }); setSearch(''); }} + submitLogic={() => { + if (formattedData && formattedData[0]) { + const user = formattedData[0]; + passUser({ id: user.id, name: user.name }); + setSearch(''); + } + }} /> ); }; diff --git a/src/components/searchBar/index.tsx b/src/components/searchBar/index.tsx index 2fbe4389..228aeb96 100644 --- a/src/components/searchBar/index.tsx +++ b/src/components/searchBar/index.tsx @@ -1,9 +1,14 @@ -import { SearchIcon } from '@src/icons/Icons'; -import { ComponentProps } from 'react'; +import { RightArrowIcon, SearchIcon } from '@src/icons/Icons'; +import { type ComponentProps } from 'react'; -type SearchBarProps = Omit, 'type'>; +type SearchBarProps = Omit, 'type'> & { + submitButton?: boolean; + submitLogic?: () => void; +}; export const SearchBar = (props: SearchBarProps) => { + const submitButton = props.submitButton; + const submitLogic = props.submitLogic; return (
@@ -12,8 +17,29 @@ export const SearchBar = (props: SearchBarProps) => { { + if (e.key === 'Enter' && typeof submitLogic !== 'undefined') { + submitLogic(); + } + }} /> + {submitButton && ( + + )}
); };