Skip to content

Commit 6ddfee9

Browse files
committed
Misc PR adjustments based on sandbox testing.
1 parent 6f74044 commit 6ddfee9

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

src/app/[locale]/page.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default function Index() {
4040
const [zoomLevel, setZoomLevel] = useState(2);
4141
const [locationError, setLocationError] = useState<string | null>(null);
4242
const [searchQuery, setSearchQuery] = useState<string>('');
43+
const [searchResults, setSearchResults] = useState<any[]>([]);
4344

4445
// Default map center (example: New York City)
4546
const defaultMapCenter = { lat: 20, lng: -74.0060 };
@@ -74,14 +75,15 @@ export default function Index() {
7475
}
7576
};
7677

77-
// handle search query update from SearchBar
78-
const handleSearch = (query: string) => {
78+
// handle search query update from SearchBar and associated results
79+
const handleSearch = (query: string, results: any[]) => {
7980
setSearchQuery(query);
81+
setSearchResults(results);
8082
}
8183

8284
return (
8385
<>
84-
<DynamicMap center={[mapCenter.lat, mapCenter.lng]} zoom={zoomLevel} searchQuery={searchQuery} />
86+
<DynamicMap center={[mapCenter.lat, mapCenter.lng]} zoom={zoomLevel} searchQuery={searchQuery} searchResults={searchResults || []} />
8587
<SearchBar page={'default'} onSearch={handleSearch} />
8688
<div className="absolute bottom-8 z-10 flex justify-between gap-[22px] px-6 right-0 left-0 m-auto">
8789
<Link href="/seller/registration">

src/components/shared/SearchBar/SearchBar.tsx

+5-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { fetchSellers } from '@/services/sellerApi';
1111
import logger from '../../../../logger.config.mjs';
1212

1313
interface searchBarProps {
14-
onSearch?: (query: string) => void;
14+
onSearch?: (query: string, results: any[]) => void;
1515
page: 'map_center' | 'default';
1616
}
1717

@@ -53,15 +53,10 @@ const SearchBar: React.FC<searchBarProps> = ({ onSearch, page }) => {
5353
const handleSubmitSearch = async (event: FormEvent) => {
5454
event.preventDefault();
5555
const query = searchBarValue.trim();
56-
57-
if (query) {
58-
logger.debug(`Search query submitted: ${query}`);
59-
const results = await fetchSearchResults(query);
60-
if (onSearch) {
61-
onSearch(query); // notify parent of the search query to update map
62-
}
63-
} else {
64-
logger.warn('Search query is empty');
56+
logger.debug(`Search query submitted: ${query}`);
57+
const results = await fetchSearchResults(query);
58+
if (onSearch) {
59+
onSearch(query, results); // notify parent of the search query to update map
6560
}
6661
};
6762

src/components/shared/map/Map.tsx

+21-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const fetchSellerCoordinates = async (origin: LatLngTuple, radius: number, searc
2727

2828
try {
2929
const sellersData = await fetchSellers(formattedOrigin, radius, searchQuery);
30-
const sellersWithCoordinates = sellersData.map((seller: any) => {
30+
const sellersWithCoordinates = sellersData?.map((seller: any) => {
3131
const [lng, lat] = seller.sell_map_center.coordinates;
3232
return {
3333
...seller,
@@ -53,7 +53,7 @@ const removeDuplicates = (sellers: ISeller[]): ISeller[] => {
5353
return Object.values(uniqueSellers);
5454
};
5555

56-
const Map = ({ center, zoom, searchQuery }: { center: LatLngExpression, zoom: number, searchQuery: string }) => {
56+
const Map = ({ center, zoom, searchQuery, searchResults }: { center: LatLngExpression, zoom: number, searchQuery: string, searchResults: ISeller[] }) => {
5757
const t = useTranslations();
5858

5959
const customIcon = L.icon({
@@ -87,12 +87,27 @@ const Map = ({ center, zoom, searchQuery }: { center: LatLngExpression, zoom: nu
8787
}
8888
}, [center]);
8989

90-
// Update map markers when searchQuery prop changes
9190
useEffect(() => {
9291
if (searchQuery) {
93-
fetchInitialCoordinates();
92+
setLoading(true);
93+
94+
const sellersWithCoordinates = searchResults
95+
.map((seller: any) => {
96+
const [lng, lat] = seller.sell_map_center.coordinates;
97+
return {
98+
...seller,
99+
coordinates: [lat, lng] as LatLngTuple
100+
};
101+
});
102+
103+
// Remove duplicates
104+
const uniqueSellers = removeDuplicates(sellersWithCoordinates);
105+
106+
// Update the sellers state
107+
setSellers(uniqueSellers);
108+
setLoading(false);
94109
}
95-
}, [searchQuery, origin, radius]);
110+
}, [searchQuery, searchResults]);
96111

97112
// Log sellers array for debugging
98113
useEffect(() => {
@@ -117,7 +132,7 @@ const Map = ({ center, zoom, searchQuery }: { center: LatLngExpression, zoom: nu
117132
}
118133
};
119134

120-
// Function to handle map interactions (zoom and move)
135+
// Function to handle map interactions (zoom and move); lazy-loading implementation
121136
const handleMapInteraction = async (newBounds: L.LatLngBounds, mapInstance: L.Map) => {
122137
const newCenter = newBounds.getCenter();
123138
const newRadius = calculateRadius(newBounds, mapInstance);

0 commit comments

Comments
 (0)