From dbe9b3bb1a40d95a115c50ce27cda04a47c83fbc Mon Sep 17 00:00:00 2001 From: SejoB <39242962+SejoB@users.noreply.github.com> Date: Wed, 2 Feb 2022 16:23:56 +0000 Subject: [PATCH] fix gps location map flickering (#853) --- src/components/molecules/Header.tsx | 15 +++++---------- src/components/molecules/LocationSelect.tsx | 16 +++++++++------- src/components/molecules/MapDialog.tsx | 5 ++--- src/components/molecules/map/Map.tsx | 4 ++-- src/pages/HomePage.tsx | 2 +- src/services/MapViewControl.tsx | 8 +------- 6 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/components/molecules/Header.tsx b/src/components/molecules/Header.tsx index 4ffe9fcb..479b2225 100644 --- a/src/components/molecules/Header.tsx +++ b/src/components/molecules/Header.tsx @@ -1,4 +1,4 @@ -import { FC, useEffect, useState } from 'react'; +import { FC, useCallback, useEffect, useState } from 'react'; import { observer } from 'mobx-react-lite'; import { useTranslation } from 'react-i18next'; import { makeStyles } from '@material-ui/core/styles'; @@ -35,24 +35,21 @@ const Header: FC = () => { const store: RootStore = useStore(); const [open, setOpen] = useState(false); - const [location, setLocation] = useState(null); const isUserDetailsRequired: boolean = !store.userInfo?.meta.isCompleteRegistration; const roadSegmentLocation = store.gpsLocationData; - const onLocationChange = (location: IPoint) => { + const onLocationChange = useCallback((location: IPoint) => { store.fetchGpsLocation(location); - setLocation(location); - }; + },[store]); const onLocationSearch = () => { if (roadSegmentLocation) { history.push(`${store.currentLanguageRouteString}/location/${roadSegmentLocation?.road_segment_id}`); setOpen(false); store.setGpsLocationData(null); - setLocation(null); - } - } + }; + }; useEffect(() => { store.getUserLoginDetails(); @@ -92,12 +89,10 @@ const Header: FC = () => { { setOpen(false); - setLocation(null); store.setGpsLocationData(null); }} onSearch={onLocationSearch} diff --git a/src/components/molecules/LocationSelect.tsx b/src/components/molecules/LocationSelect.tsx index 42ef57e0..8e563a11 100644 --- a/src/components/molecules/LocationSelect.tsx +++ b/src/components/molecules/LocationSelect.tsx @@ -1,4 +1,4 @@ -import { FC } from 'react'; +import { FC, memo, useState } from 'react'; import { useMapEvents } from 'react-leaflet'; import { IPoint } from 'models/Point'; import { Marker } from 'components/atoms'; @@ -9,27 +9,29 @@ interface ILocation { } const LocationPicker: FC = ({ onLocationChange }) => { + const [position, setPosition] = useState(null); + useMapEvents({ click: (event) => { const {latlng: { lng, lat }} = event; - onLocationChange({ longitude: lng,latitude: lat }); + setPosition({ longitude: lng, latitude: lat }); + onLocationChange({ longitude: lng, latitude: lat }); }, }); - return null; + + return position === null ? null : }; interface IProps { - location: IPoint | null; onLocationChange: (location: IPoint) => void; } -const LocationSelect: FC = ({ location, onLocationChange }) => { +const LocationSelect: FC = ({ onLocationChange }) => { return ( - {location && } ); }; -export default LocationSelect; +export default memo(LocationSelect); diff --git a/src/components/molecules/MapDialog.tsx b/src/components/molecules/MapDialog.tsx index 3543e7c2..f27e6855 100644 --- a/src/components/molecules/MapDialog.tsx +++ b/src/components/molecules/MapDialog.tsx @@ -9,7 +9,6 @@ import { IPoint } from 'models/Point'; interface IProps { section?: string; open: boolean; - location: IPoint | null; onClose: () => void; onLocationChange: (location: IPoint) => void; onSearch: () => void; @@ -38,7 +37,7 @@ const useStyles = makeStyles((theme: Theme) => }), ); -const MapDialog: FC = ({ section, open, onClose, location, onLocationChange, onSearch }) => { +const MapDialog: FC = ({ section, open, onClose, onLocationChange, onSearch }) => { const classes = useStyles(); const { t } = useTranslation(); @@ -50,7 +49,7 @@ const MapDialog: FC = ({ section, open, onClose, location, onLocationCha - +
{t('mapDialog.chosenSegment')} diff --git a/src/components/molecules/map/Map.tsx b/src/components/molecules/map/Map.tsx index 6dd8761e..f43f8420 100644 --- a/src/components/molecules/map/Map.tsx +++ b/src/components/molecules/map/Map.tsx @@ -33,8 +33,8 @@ const Map: FC = ({ zoom = INITIAL_ZOOM, center=INITIAL_CENTER, data, chi const bounds = getBounds(data); return ( - - + + {children} diff --git a/src/pages/HomePage.tsx b/src/pages/HomePage.tsx index b94166f5..577e07d1 100644 --- a/src/pages/HomePage.tsx +++ b/src/pages/HomePage.tsx @@ -1,4 +1,4 @@ -import React, { FC, useEffect } from 'react'; +import { FC, useEffect } from 'react'; import WidgetsTemplate from '../components/organisms/WidgetsTemplate'; import { Box } from '@material-ui/core'; import SideBar from 'components/organisms/SideBar'; diff --git a/src/services/MapViewControl.tsx b/src/services/MapViewControl.tsx index 2be4daeb..f2611b1a 100644 --- a/src/services/MapViewControl.tsx +++ b/src/services/MapViewControl.tsx @@ -1,19 +1,13 @@ import React from 'react'; import { useMap } from 'react-leaflet'; -import { LatLngLiteral } from 'leaflet'; interface IProps { - zoom?: number; - center?: LatLngLiteral; bounds: any; } -const MapViewControl: React.FC = ({ zoom, center, bounds }) => { +const MapViewControl: React.FC = ({ bounds }) => { const map = useMap(); map.invalidateSize(); - if (zoom && center) { - map.setView(center, zoom); - } map.fitBounds(bounds); return null; };