DebasishDhal99 commited on
Commit
04b006b
·
1 Parent(s): 7e40962

Make the distance in geoDistance feature change when distance unit is changed

Browse files

Earlier, if i opted for distance between two points, and then changed the distance unit, the actual numerical distance didn't change. Added a useEffect to geoHandleClick, so that it fetches the api yet again, when distance unit is changed.

Files changed (1) hide show
  1. frontend/src/components/Map.js +27 -0
frontend/src/components/Map.js CHANGED
@@ -204,6 +204,33 @@ const Map = ( { onMapClick, searchQuery, contentType } ) => {
204
  }
205
  }, [geoPoints, geoUnit]);
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  return (
208
  <div ref={containerRef} style={{ display: 'flex', height: '100vh', width: '100%', overflow: 'hidden' }}>
209
  {panelSize !== 'closed' && (
 
204
  }
205
  }, [geoPoints, geoUnit]);
206
 
207
+ useEffect(() => {
208
+ if (geoPoints.length === 2) {
209
+ const fetchDistance = async () => {
210
+ try{
211
+ const res = await fetch(`${BACKEND_URL}/geodistance`, {
212
+ method: 'POST',
213
+ headers: { 'Content-Type': 'application/json' },
214
+ body: JSON.stringify({
215
+ lat1: geoPoints[0].lat,
216
+ lon1: geoPoints[0].lon,
217
+ lat2: geoPoints[1].lat,
218
+ lon2: geoPoints[1].lon,
219
+ unit: geoUnit
220
+ }),
221
+ });
222
+ const data = await res.json();
223
+ setGeoDistance(data.distance);
224
+ }
225
+ catch (err) {
226
+ console.error('Failed to fetch distance:', err);
227
+ setGeoDistance(null);
228
+ }
229
+ };
230
+ fetchDistance();
231
+ }
232
+ }, [geoPoints, geoUnit]);
233
+
234
  return (
235
  <div ref={containerRef} style={{ display: 'flex', height: '100vh', width: '100%', overflow: 'hidden' }}>
236
  {panelSize !== 'closed' && (