@@ -81,12 +81,6 @@ const Map = ({
81
81
const [ isLocationAvailable , setIsLocationAvailable ] = useState ( false ) ;
82
82
const [ initialLocationSet , setInitialLocationSet ] = useState ( false ) ;
83
83
84
- // Fetch initial seller coordinates when component mounts
85
- useEffect ( ( ) => {
86
- logger . info ( 'Component mounted, fetching initial coordinates..' ) ;
87
- fetchInitialCoordinates ( ) ;
88
- } , [ searchQuery ] ) ;
89
-
90
84
// Update origin when center prop changes
91
85
useEffect ( ( ) => {
92
86
if ( center ) {
@@ -160,30 +154,39 @@ const Map = ({
160
154
map . panBy ( panOffset , { animate : false } ) ; // Disable animation to make the movement instant
161
155
} ;
162
156
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
+
164
163
const fetchInitialCoordinates = async ( ) => {
165
164
if ( searchQuery ) return ;
166
-
165
+
167
166
setLoading ( true ) ;
168
167
setError ( null ) ;
168
+
169
169
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
+ }
180
183
} catch ( error ) {
181
184
logger . error ( 'Failed to fetch initial coordinates:' , { error } ) ;
182
185
setError ( 'Failed to fetch initial coordinates' ) ;
183
186
} finally {
184
187
setLoading ( false ) ;
185
188
}
186
- } ;
189
+ } ;
187
190
188
191
// Function to handle map interactions (only when there's no search query)
189
192
const handleMapInteraction = async ( newBounds : L . LatLngBounds , mapInstance : L . Map ) => {
@@ -336,8 +339,11 @@ const Map = ({
336
339
zoomControl = { false }
337
340
minZoom = { 2 }
338
341
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
+ }
341
347
className = "w-full flex-1 fixed bottom-0 h-[calc(100vh-76.19px)] left-0 right-0"
342
348
>
343
349
< TileLayer
0 commit comments