|
| 1 | +import { useMap } from 'react-leaflet'; |
| 2 | +import { useEffect, useState } from 'react'; |
| 3 | +import { useAppContext } from 'src/context/appContext'; |
| 4 | + |
| 5 | +import styles from './Map.module.scss'; |
| 6 | + |
| 7 | + |
| 8 | +function getCoords(geojson){ |
| 9 | + return [geojson.geometry.coordinates[1], geojson.geometry.coordinates[0]] |
| 10 | +} |
| 11 | + |
| 12 | +export default function Footprint() { |
| 13 | + const map = useMap(); |
| 14 | + const [activeMarkerHover, setActiveMarkerHover] = useState(null); |
| 15 | + const [previousMarkerHover, setPreviousMarkerHover] = useState(null); |
| 16 | + const [activeMarkerClick, setActiveMarkerClick] = useState(null); |
| 17 | + const [previousMarkerClick, setPreviousMarkerClick] = useState(null); |
| 18 | + const { selectedOpportunity, hoveredOpportunity } = useAppContext(); |
| 19 | + const footprintIcon = new L.divIcon({className: styles.footprintCircle}); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + if (!activeMarkerClick && previousMarkerClick){ |
| 23 | + map.removeLayer(previousMarkerClick); |
| 24 | + setPreviousMarkerClick(null); |
| 25 | + } |
| 26 | + if (!activeMarkerHover && previousMarkerHover){ |
| 27 | + map.removeLayer(previousMarkerHover); |
| 28 | + setPreviousMarkerHover(null); |
| 29 | + } |
| 30 | + if (activeMarkerClick && previousMarkerClick && (activeMarkerClick !== previousMarkerClick)){ |
| 31 | + map.removeLayer(previousMarkerClick); |
| 32 | + setPreviousMarkerClick(activeMarkerClick); |
| 33 | + } |
| 34 | + previousMarkerHover && map.removeLayer(previousMarkerHover); |
| 35 | + activeMarkerHover && map.addLayer(activeMarkerHover); |
| 36 | + activeMarkerClick && map.addLayer(activeMarkerClick) && map.flyTo(getCoords(selectedOpportunity), 9); |
| 37 | + },[activeMarkerHover, previousMarkerHover, activeMarkerClick, previousMarkerClick]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + activeMarkerHover && setPreviousMarkerHover(activeMarkerHover); |
| 41 | + activeMarkerClick && setPreviousMarkerClick(activeMarkerClick); |
| 42 | + if (hoveredOpportunity){ |
| 43 | + setActiveMarkerHover(new L.Marker(getCoords(hoveredOpportunity), { |
| 44 | + icon: footprintIcon |
| 45 | + })); |
| 46 | + } |
| 47 | + else { |
| 48 | + setActiveMarkerHover(null); |
| 49 | + } |
| 50 | + |
| 51 | + if(selectedOpportunity) { |
| 52 | + setActiveMarkerClick(new L.Marker(getCoords(selectedOpportunity), { |
| 53 | + icon: footprintIcon |
| 54 | + })); |
| 55 | + setActiveMarkerHover(null); |
| 56 | + } |
| 57 | + else { |
| 58 | + setActiveMarkerClick(null); |
| 59 | + } |
| 60 | + }, [hoveredOpportunity, selectedOpportunity]); |
| 61 | + |
| 62 | + return null; |
| 63 | +} |
0 commit comments