-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search-bar): implement search functionality
- Loading branch information
1 parent
34bfdb2
commit 51f8c20
Showing
5 changed files
with
119 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { trpc } from '@utils/trpc'; | ||
|
||
const ResultList: React.FC<{ | ||
searchQuery: string; | ||
}> = ({ searchQuery }) => { | ||
const { data, isLoading } = trpc.searchLocations.useQuery(searchQuery); | ||
|
||
if(isLoading) { | ||
return ( | ||
<ul className='result-list'> | ||
<li>Searching...</li> | ||
</ul> | ||
); | ||
}; | ||
|
||
return ( | ||
<ul className='result-list'> | ||
{ | ||
data?.length ? | ||
data.map((location) => ( | ||
<li key={ location.id }> | ||
{ location.name } | ||
</li> | ||
)) | ||
: | ||
<li>No results found</li> | ||
} | ||
</ul> | ||
); | ||
} | ||
|
||
export default ResultList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useState } from 'react'; | ||
import ResultList from './result-list'; | ||
import { debounce } from '@utils/utility-functions'; | ||
|
||
const SearchBar = () => { | ||
const [searchQuery, setSearchQuery] = useState(''); | ||
|
||
return ( | ||
<div className='search-bar-container'> | ||
<input | ||
type='text' | ||
className='searchbar' | ||
placeholder='Search for location' | ||
onChange={ debounce((e: React.ChangeEvent<HTMLInputElement>) => setSearchQuery(e.target.value), 300) } | ||
/> | ||
|
||
{ | ||
searchQuery && ( | ||
<ResultList searchQuery={ searchQuery } /> | ||
) | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const debounce = <T extends (...params: any[]) => any>( | ||
fn: T, | ||
ms: number = 500 | ||
) => { | ||
let timer: NodeJS.Timeout; | ||
|
||
return (...args: Parameters<T>) => { | ||
clearTimeout(timer); | ||
|
||
timer = setTimeout(() => fn(...args), ms); | ||
}; | ||
}; |