Skip to content

Commit 1f445fe

Browse files
committed
feat: implement SearchPageComponent and useSearch context for search functionality
1 parent 820d6b3 commit 1f445fe

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use client';
2+
3+
import { faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons';
4+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
5+
import { useSearchParams } from 'next/navigation';
6+
import { useEffect } from 'react';
7+
8+
import { useSearch } from './client/context/useSearch';
9+
10+
export const SearchPageComponent = () => {
11+
const searchParams = useSearchParams();
12+
const { results, query, fetchSearchResults } = useSearch();
13+
14+
useEffect(() => {
15+
const query = searchParams.get('query') || '';
16+
const page = searchParams.get('page') || '1';
17+
const limit = searchParams.get('limit') || '20';
18+
19+
fetchSearchResults(query, parseInt(page), parseInt(limit));
20+
}, []);
21+
22+
return (
23+
<>
24+
{/* Search Header */}
25+
<div className='relative w-0 h-0' aria-hidden={true}>
26+
<FontAwesomeIcon
27+
icon={faMagnifyingGlass}
28+
className='absolute text-[8rem] md:text-[12rem] text-zinc-400 opacity-15 rotate-[-15deg] translate-y-8'
29+
/>
30+
</div>
31+
32+
<h1 className='text-center text-5xl text-zinc-300 font-light uppercase mt-12 mb-6'>
33+
Search Results
34+
</h1>
35+
{query && (
36+
<h2 className='text-center text-xl font-light mb-12'>
37+
{`Showing results for "${query}"`}
38+
</h2>
39+
)}
40+
41+
{results.length === 0 ? (
42+
<div className='text-center text-xl font-light'>
43+
No results found. Try searching for something else.
44+
</div>
45+
) : (
46+
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4'>
47+
{JSON.stringify(results, null, 2)}
48+
</div>
49+
)}
50+
</>
51+
);
52+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use client';
2+
3+
import { create } from 'zustand';
4+
5+
import axios from '@web/src/lib/axios';
6+
7+
type SearchState = {
8+
fetchSearchResults: (
9+
query: string,
10+
page: number,
11+
limit: number,
12+
) => Promise<void>;
13+
query: string;
14+
page: number;
15+
limit: number;
16+
results: any[];
17+
isLoading: boolean;
18+
};
19+
20+
export const useSearch = create<SearchState>((set, get) => {
21+
const fetchSearchResults = async (
22+
query: string,
23+
page: number,
24+
limit: number,
25+
) => {
26+
set({ isLoading: true });
27+
28+
const result = await axios.get('/user', {
29+
params: {
30+
query: query,
31+
page: page,
32+
limit: limit,
33+
},
34+
});
35+
36+
set({
37+
query,
38+
page,
39+
limit,
40+
results: result.data,
41+
isLoading: false,
42+
});
43+
};
44+
45+
return {
46+
fetchSearchResults,
47+
query: '',
48+
page: 1,
49+
limit: 20,
50+
results: [],
51+
isLoading: false,
52+
};
53+
});

web/src/modules/search/context/search.ts

Whitespace-only changes.

web/src/modules/shared/components/layout/BlockSearchProps.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client';
22
import { faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons';
33
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
4+
import { useRouter } from 'next/navigation';
45
import { useState } from 'react';
56

67
import { cn } from '@web/src/lib/tailwind.utils';
@@ -9,6 +10,7 @@ import { Popover, PopoverContent, PopoverTrigger } from './popover';
910

1011
export const BlockSearch = () => {
1112
const [query, setQuery] = useState('');
13+
const router = useRouter();
1214

1315
return (
1416
<Popover>
@@ -33,7 +35,13 @@ export const BlockSearch = () => {
3335
<button
3436
className='bg-zinc-600 text-white rounded-md p-2 hover:bg-zinc-500 w-12 h-12'
3537
onClick={() => {
36-
console.log('Searching for:', query);
38+
const queryParam = new URLSearchParams({
39+
page: '1',
40+
limit: '20',
41+
query,
42+
});
43+
44+
router.push(`/search?${queryParam.toString()}`);
3745
}}
3846
>
3947
<FontAwesomeIcon icon={faMagnifyingGlass} />

0 commit comments

Comments
 (0)