From 739f6fc10479d050b054154318817fade5f5df91 Mon Sep 17 00:00:00 2001 From: Seb-sti1 <65665540+seb-sti1@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:41:12 +0200 Subject: [PATCH] ForceMapUpdate: Allow to force update the map or to go to specific position --- .../src/Components/Map/ForceMapUpdate.tsx | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 frontend/src/Components/Map/ForceMapUpdate.tsx diff --git a/frontend/src/Components/Map/ForceMapUpdate.tsx b/frontend/src/Components/Map/ForceMapUpdate.tsx new file mode 100644 index 00000000..2034f8bd --- /dev/null +++ b/frontend/src/Components/Map/ForceMapUpdate.tsx @@ -0,0 +1,31 @@ +import { LatLng } from '../../models/models'; +import { useMap } from 'react-leaflet'; +import React, { useEffect } from 'react'; + +interface Props { + /** A modification of this value is used to trigger an update of the map */ + triggerUpdate: number; + /** The coordinates of the map center*/ + position?: LatLng; +} + +/** + * This component is used to force an update of the map. It is used in conjunction with the useMap hook. + */ +const ForceMapUpdate: React.FC = ({ triggerUpdate, position }) => { + const map = useMap(); + + useEffect(() => { + map.invalidateSize(); + }, [triggerUpdate]); + + useEffect(() => { + if (position) { + map.flyTo(position); + } + }, [position]); + + return null; +}; + +export default ForceMapUpdate;