Skip to content

Commit 090c279

Browse files
authored
Merge pull request #252 from map-of-pi/feature/lazy-loading-modification
Approved (1).
2 parents 3d1b6f6 + f1c3891 commit 090c279

File tree

6 files changed

+142
-161
lines changed

6 files changed

+142
-161
lines changed

context/AppContextProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const AppContextProvider = ({ children }: AppContextProviderProps) => {
6868
logger.info('User authenticated successfully.');
6969
setTimeout(() => {
7070
setIsSigningInUser(false); // hide the splash screen after the delay
71-
}, 5000);
71+
}, 2500);
7272
} else if (res.status === 500) {
7373
setCurrentUser(null);
7474
logger.error('User authentication failed.');
@@ -94,7 +94,7 @@ const AppContextProvider = ({ children }: AppContextProviderProps) => {
9494
setCurrentUser(res.data);
9595
setTimeout(() => {
9696
setIsSigningInUser(false); // hide the splash screen after the delay
97-
}, 5000);
97+
}, 2500);
9898
} else {
9999
setCurrentUser(null);
100100
logger.warn('Auto-login failed.');

src/app/[locale]/page.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
'use client';
22

3+
import L from 'leaflet';
4+
35
import { useTranslations } from 'next-intl';
46
import dynamic from 'next/dynamic';
57
import Image from 'next/image';
68
import Link from 'next/link';
7-
import { useContext, useEffect, useState } from 'react';
9+
import { useContext, useEffect, useState, useRef } from 'react';
810

911
import { Button } from '@/components/shared/Forms/Buttons/Buttons';
1012
import SearchBar from '@/components/shared/SearchBar/SearchBar';
13+
import { fetchSellers } from '@/services/sellerApi';
1114
import { fetchUserLocation } from '@/services/userSettingsApi';
1215

1316
import { AppContext } from '../../../context/AppContextProvider';
@@ -18,6 +21,7 @@ export default function Index() {
1821
const DynamicMap = dynamic(() => import('@/components/shared/map/Map'), {
1922
ssr: false,
2023
});
24+
const mapRef = useRef<L.Map | null>(null);
2125

2226
const [mapCenter, setMapCenter] = useState<{ lat: number; lng: number }>({
2327
lat: 0,
@@ -71,17 +75,29 @@ export default function Index() {
7175
};
7276

7377
// handle search query update from SearchBar and associated results
74-
const handleSearch = (query: string, results: any[]) => {
78+
const handleSearch = async (query: string) => {
7579
setSearchQuery(query);
7680
setSearchClicked(true);
77-
setSearchResults(results);
81+
82+
// Fetch sellers based on current map bounds and search query
83+
try {
84+
const mapInstance = mapRef.current;
85+
if (mapInstance) {
86+
const bounds = mapInstance.getBounds();
87+
const results = await fetchSellers(bounds, query); // Use API to fetch sellers
88+
setSearchResults(results || []); // Update searchResults
89+
}
90+
} catch (error) {
91+
logger.error('Failed to fetch sellers for search query.', { error });
92+
}
7893
};
7994

8095
return (
8196
<>
8297
<DynamicMap
8398
center={[mapCenter.lat, mapCenter.lng]}
8499
zoom={zoomLevel}
100+
mapRef={mapRef}
85101
searchQuery={searchQuery}
86102
isSearchClicked={isSearchClicked}
87103
searchResults={searchResults || []}

src/components/shared/SearchBar/SearchBar.tsx

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,23 @@
33
import './SearchBar.scss';
44

55
import { useTranslations } from 'next-intl';
6-
import { useContext, useState, useRef, ChangeEvent, FormEvent } from 'react';
6+
import { useContext, useState, ChangeEvent, FormEvent } from 'react';
77
import { FormControl, TextField } from '@mui/material';
88
import SearchIcon from '@mui/icons-material/Search';
9-
import { fetchSellers } from '@/services/sellerApi';
10-
119
import logger from '../../../../logger.config.mjs';
1210
import { AppContext } from '../../../../context/AppContextProvider';
1311

14-
interface searchBarProps {
15-
onSearch?: (query: string, results: any[]) => void;
12+
interface SearchBarProps {
13+
onSearch?: (query: string) => void;
1614
page: 'map_center' | 'default';
1715
}
1816

19-
const SearchBar: React.FC<searchBarProps> = ({ onSearch, page }) => {
17+
const SearchBar: React.FC<SearchBarProps> = ({ onSearch, page }) => {
2018
const t = useTranslations();
2119

2220
const [searchBarValue, setSearchBarValue] = useState('');
23-
const [loading, setLoading] = useState(false);
24-
2521
const { isSigningInUser } = useContext(AppContext);
2622

27-
const inputRef = useRef<HTMLInputElement>(null);
28-
2923
const getPlaceholderText = (page: 'map_center' | 'default'): string => {
3024
return page === 'map_center'
3125
? t('SHARED.MAP_CENTER.SEARCH_BAR_PLACEHOLDER')
@@ -39,47 +33,36 @@ const SearchBar: React.FC<searchBarProps> = ({ onSearch, page }) => {
3933
setSearchBarValue(event.target.value);
4034
};
4135

42-
const fetchSearchResults = async (query: string): Promise<any[]> => {
43-
setLoading(true);
44-
try {
45-
logger.debug(`Fetching search results for query: "${query}"`);
46-
const data = await fetchSellers(undefined, undefined, query);
47-
return data || []; // Return response.data or empty array if undefined
48-
} catch (error) {
49-
logger.error(`Error fetching search results: ${ error }`);
50-
return [];
51-
} finally {
52-
setLoading(false);
53-
}
54-
};
55-
56-
const handleSubmitSearch = async (event: FormEvent) => {
36+
const handleSubmitSearch = (event: FormEvent) => {
5737
event.preventDefault();
5838
const query = searchBarValue.trim();
5939
logger.debug(`Search query submitted: ${query}`);
60-
const results = await fetchSearchResults(query);
6140
if (onSearch) {
62-
onSearch(query, results); // notify parent of the search query to update map
41+
onSearch(query); // Pass the query to the parent component
6342
}
6443
};
6544

6645
return (
6746
<div className="w-[90%] m-auto left-0 right-0 max-w-[504px] fixed top-[120px] z-10 flex">
6847
<div className="w-[100%] m-auto flex items-center">
69-
<form className="w-full flex items-center gap-2 justify-between" onSubmit={handleSubmitSearch}>
48+
<form
49+
className="w-full flex items-center gap-2 justify-between"
50+
onSubmit={handleSubmitSearch}
51+
>
7052
<FormControl className="flex-grow mr-3">
7153
<TextField
72-
id="search-input"
73-
type="text"
74-
variant="outlined"
75-
color="success"
76-
className={`w-full rounded ${isSigningInUser ? 'bg-gray-200' : 'bg-white hover:bg-gray-100'}`}
77-
label={`${isSigningInUser ? '' : placeholder}`}
78-
value={searchBarValue}
79-
onChange={handleSearchBarChange}
80-
ref={inputRef}
81-
disabled={isSigningInUser}
82-
InputLabelProps={{ className: 'custom-label' }}
54+
id="search-input"
55+
type="text"
56+
variant="outlined"
57+
color="success"
58+
className={`w-full rounded ${
59+
isSigningInUser ? 'bg-gray-200' : 'bg-white hover:bg-gray-100'
60+
}`}
61+
label={isSigningInUser ? '' : placeholder}
62+
value={searchBarValue}
63+
onChange={handleSearchBarChange}
64+
disabled={isSigningInUser}
65+
InputLabelProps={{ className: 'custom-label' }}
8366
/>
8467
</FormControl>
8568
<button
@@ -89,7 +72,7 @@ const SearchBar: React.FC<searchBarProps> = ({ onSearch, page }) => {
8972
${isSigningInUser ? 'bg-tertiary' : 'bg-primary hover:bg-gray-500'}`}
9073
disabled={isSigningInUser}
9174
>
92-
<SearchIcon fontSize={"large"} className=" text-[#ffc153]" />
75+
<SearchIcon fontSize={'large'} className="text-[#ffc153]" />
9376
</button>
9477
</form>
9578
</div>

0 commit comments

Comments
 (0)