Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search-bar): implement search functionality #16

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions client/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ a {
grid-template-rows: var(--top-bar-height) var(--dashboard-first-row-height) minmax(0, 1fr);
}

.dashboard > div {
.dashboard > div:not(.search-bar-container) {
padding: var(--padding);
border-radius: var(--border-radius);
}
Expand All @@ -189,17 +189,56 @@ a {
font-size: 0.9rem;
}

.search-bar-container {
position: relative;
grid-area: searchbar;
}

.searchbar {
width: 100%;
border: none;
height: 100%;
color: #fff;
display: flex;
font-weight: 300;
align-items: center;
grid-area: searchbar;
padding: 1.15rem 2rem;
border-radius: 0.5rem;
justify-content: center;
background: rgba(0, 0, 0, 0.09) url('/src/search-icon.png') no-repeat left 3% center / 8%;
padding: 0.25rem 2.5rem;
background: rgba(0, 0, 0, 0.09) url('/search-icon.png') no-repeat left 3% center / 8%;
}

.search-bar-container > ul {
position: absolute;
display: none;
z-index: 1;
top: 120%;
gap: 0.1rem;
width: 100%;
padding: 0.5rem 0.75rem;
min-height: 100%;
flex-direction: column;
backdrop-filter: blur(10px);
background: rgba(0, 0, 0, 0.09);
border-radius: var(--border-radius);
-webkit-backdrop-filter: blur(10px);
box-shadow: 0 7px 29px 0 rgba(6, 14, 75, 0.5);
}

.search-bar-container ul li {
cursor: pointer;
overflow: hidden;
text-wrap: nowrap;
font-weight: 300;
font-size: 0.9rem;
padding: 1rem 0.5rem;
text-overflow: ellipsis;
}

.search-bar-container ul li:hover {
background: rgba(0, 0, 0, 0.2);
border-radius: var(--border-radius);
}

.search-bar-container .searchbar:focus + ul {
display: flex;
}

.current-weather-container {
Expand Down
7 changes: 2 additions & 5 deletions client/src/components/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import Map from './map';
import SearchBar from './search-bar/search-bar';
import { PiWind, PiEyeLight, PiSunLight, PiDropLight, PiWindLight, PiThermometerSimple } from 'react-icons/pi';

const Dashboard = () => {
return (
<div className='dashboard'>
<input
type='text'
className='searchbar'
placeholder='Search for location'
/>
<SearchBar />

<div className='current-weather-container'>
<div className='title-container'>
Expand Down
32 changes: 32 additions & 0 deletions client/src/components/search-bar/result-list.tsx
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;
26 changes: 26 additions & 0 deletions client/src/components/search-bar/search-bar.tsx
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;
13 changes: 13 additions & 0 deletions client/src/utils/utility-functions.ts
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);
};
};