Skip to content

Commit f0ef493

Browse files
DarinHajouswoocn
andauthored
Fix markers not displaying on initial map load by fetching sellers af… (#273)
* Fix markers not displaying on initial map load by fetching sellers after map initialization * Fix build issue * Fix build issue * Fix build issue * Fix build issue * Fix build * Add type assertion to whenReady() in an attempt to resolve build. --------- Co-authored-by: Darin Hajou <[email protected]> Co-authored-by: swoocn <[email protected]>
1 parent f3085f5 commit f0ef493

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/components/shared/map/Map.tsx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ const Map = ({
8181
const [isLocationAvailable, setIsLocationAvailable] = useState(false);
8282
const [initialLocationSet, setInitialLocationSet] = useState(false);
8383

84-
// Fetch initial seller coordinates when component mounts
85-
useEffect(() => {
86-
logger.info('Component mounted, fetching initial coordinates..');
87-
fetchInitialCoordinates();
88-
}, [searchQuery]);
89-
9084
// Update origin when center prop changes
9185
useEffect(() => {
9286
if (center) {
@@ -160,30 +154,39 @@ const Map = ({
160154
map.panBy(panOffset, { animate: false }); // Disable animation to make the movement instant
161155
};
162156

163-
// Function to fetch initial coordinates
157+
useEffect(() => {
158+
if (mapRef.current) {
159+
fetchInitialCoordinates(); // Fetch sellers when map is ready
160+
}
161+
}, [mapRef.current]);
162+
164163
const fetchInitialCoordinates = async () => {
165164
if (searchQuery) return;
166-
165+
167166
setLoading(true);
168167
setError(null);
168+
169169
try {
170-
// Fetch the current map bounds
171-
// manually set bounds as mapref is null during component rendering phase
172-
const bounds = L.latLngBounds(
173-
L.latLng(-90, -180),
174-
L.latLng(90, 180)
175-
);
176-
177-
let sellersData = await fetchSellerCoordinates(bounds, '');
178-
sellersData = removeDuplicates(sellersData);
179-
setSellers(sellersData);
170+
const mapInstance = mapRef.current; // Access map instance via ref
171+
172+
if (!mapInstance) {
173+
logger.warn('Map instance is not ready yet');
174+
return;
175+
}
176+
177+
const bounds = mapInstance.getBounds();
178+
if (bounds) {
179+
let sellersData = await fetchSellerCoordinates(bounds, '');
180+
sellersData = removeDuplicates(sellersData);
181+
setSellers(sellersData);
182+
}
180183
} catch (error) {
181184
logger.error('Failed to fetch initial coordinates:', { error });
182185
setError('Failed to fetch initial coordinates');
183186
} finally {
184187
setLoading(false);
185188
}
186-
};
189+
};
187190

188191
// Function to handle map interactions (only when there's no search query)
189192
const handleMapInteraction = async (newBounds: L.LatLngBounds, mapInstance: L.Map) => {
@@ -336,8 +339,11 @@ const Map = ({
336339
zoomControl={false}
337340
minZoom={2}
338341
maxZoom={18}
339-
// maxBounds={bounds}
340-
// maxBoundsViscosity={1.0}
342+
whenReady={
343+
((mapInstance: L.Map) => {
344+
mapRef.current = mapInstance;
345+
}) as unknown as () => void // utilize Type assertion
346+
}
341347
className="w-full flex-1 fixed bottom-0 h-[calc(100vh-76.19px)] left-0 right-0"
342348
>
343349
<TileLayer

0 commit comments

Comments
 (0)