@@ -20,14 +20,14 @@ const sanitizeCoordinates = (lat: number, lng: number) => {
20
20
return { lat : sanitizedLat , lng : sanitizedLng } ;
21
21
} ;
22
22
23
- // Function to fetch seller coordinates based on origin and radius
24
- const fetchSellerCoordinates = async ( origin : LatLngTuple , radius : number ) : Promise < ISeller [ ] > => {
23
+ // Function to fetch seller coordinates based on origin, radius, and optional search query
24
+ const fetchSellerCoordinates = async ( origin : LatLngTuple , radius : number , searchQuery ?: string ) : Promise < ISeller [ ] > => {
25
25
const { lat, lng } = sanitizeCoordinates ( origin [ 0 ] , origin [ 1 ] ) ;
26
26
const formattedOrigin = toLatLngLiteral ( [ lat , lng ] ) ;
27
27
28
28
try {
29
- const sellersData = await fetchSellers ( formattedOrigin , radius ) ;
30
- const sellersWithCoordinates = sellersData . map ( ( seller : any ) => {
29
+ const sellersData = await fetchSellers ( formattedOrigin , radius , searchQuery ) ;
30
+ const sellersWithCoordinates = sellersData ? .map ( ( seller : any ) => {
31
31
const [ lng , lat ] = seller . sell_map_center . coordinates ;
32
32
return {
33
33
...seller ,
@@ -53,7 +53,7 @@ const removeDuplicates = (sellers: ISeller[]): ISeller[] => {
53
53
return Object . values ( uniqueSellers ) ;
54
54
} ;
55
55
56
- const Map = ( { center, zoom } : { center : LatLngExpression , zoom : number } ) => {
56
+ const Map = ( { center, zoom, searchQuery , searchResults } : { center : LatLngExpression , zoom : number , searchQuery : string , searchResults : ISeller [ ] } ) => {
57
57
const t = useTranslations ( ) ;
58
58
59
59
const customIcon = L . icon ( {
@@ -72,7 +72,7 @@ const Map = ({ center, zoom }: { center: LatLngExpression, zoom: number }) => {
72
72
const [ locationError , setLocationError ] = useState ( false ) ;
73
73
const [ isLocationAvailable , setIsLocationAvailable ] = useState ( false ) ;
74
74
const [ initialLocationSet , setInitialLocationSet ] = useState ( false ) ;
75
-
75
+
76
76
// Fetch initial seller coordinates when component mounts
77
77
useEffect ( ( ) => {
78
78
logger . info ( 'Component mounted, fetching initial coordinates..' ) ;
@@ -87,6 +87,28 @@ const Map = ({ center, zoom }: { center: LatLngExpression, zoom: number }) => {
87
87
}
88
88
} , [ center ] ) ;
89
89
90
+ useEffect ( ( ) => {
91
+ if ( searchQuery ) {
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 ) ;
109
+ }
110
+ } , [ searchQuery , searchResults ] ) ;
111
+
90
112
// Log sellers array for debugging
91
113
useEffect ( ( ) => {
92
114
logger . debug ( 'Sellers Array:' , { sellers } ) ;
@@ -99,7 +121,7 @@ const Map = ({ center, zoom }: { center: LatLngExpression, zoom: number }) => {
99
121
try {
100
122
const originLiteral = toLatLngLiteral ( origin ) ;
101
123
const originLatLngTuple : LatLngTuple = [ originLiteral . lat , originLiteral . lng ] ;
102
- let sellersData = await fetchSellerCoordinates ( originLatLngTuple , radius ) ;
124
+ let sellersData = await fetchSellerCoordinates ( originLatLngTuple , radius , searchQuery ) ;
103
125
sellersData = removeDuplicates ( sellersData ) ;
104
126
setSellers ( sellersData ) ;
105
127
} catch ( error ) {
@@ -110,7 +132,7 @@ const Map = ({ center, zoom }: { center: LatLngExpression, zoom: number }) => {
110
132
}
111
133
} ;
112
134
113
- // Function to handle map interactions (zoom and move)
135
+ // Function to handle map interactions (zoom and move); lazy-loading implementation
114
136
const handleMapInteraction = async ( newBounds : L . LatLngBounds , mapInstance : L . Map ) => {
115
137
const newCenter = newBounds . getCenter ( ) ;
116
138
const newRadius = calculateRadius ( newBounds , mapInstance ) ;
@@ -121,7 +143,7 @@ const Map = ({ center, zoom }: { center: LatLngExpression, zoom: number }) => {
121
143
setError ( null ) ;
122
144
123
145
try {
124
- let additionalSellers = await fetchSellerCoordinates ( [ newCenter . lat , newCenter . lng ] , largerRadius ) ;
146
+ let additionalSellers = await fetchSellerCoordinates ( [ newCenter . lat , newCenter . lng ] , largerRadius , searchQuery ) ;
125
147
additionalSellers = removeDuplicates ( additionalSellers ) ;
126
148
127
149
logger . info ( 'Fetched additional sellers:' , { additionalSellers } ) ;
@@ -196,6 +218,7 @@ const Map = ({ center, zoom }: { center: LatLngExpression, zoom: number }) => {
196
218
setPosition ( e . latlng ) ;
197
219
setLocationError ( false ) ;
198
220
if ( ! initialLocationSet ) {
221
+ console . log ( 'in location' ) ;
199
222
map . setView ( e . latlng , zoom , { animate : false } ) ;
200
223
setInitialLocationSet ( true ) ;
201
224
}
0 commit comments