mirror of
https://github.com/nowcommunity/nowdatabase.git
synced 2026-10-12 19:13:52 +03:00
fix bug where sometimes selected marker wasn't cleared
This commit is contained in:
@@ -28,6 +28,8 @@ type CustomMarker = L.Marker & { options: CustomMarkerOptions }
|
|||||||
|
|
||||||
export const LocalitiesMap = ({ localitiesQueryData, localitiesQueryIsFetching }: Props) => {
|
export const LocalitiesMap = ({ localitiesQueryData, localitiesQueryIsFetching }: Props) => {
|
||||||
const [selectedLocality, setSelectedLocality] = useState<string | null>(null)
|
const [selectedLocality, setSelectedLocality] = useState<string | null>(null)
|
||||||
|
// Map instance tracked in state for attaching event listeners
|
||||||
|
const [mapInstance, setMapInstance] = useState<L.Map | null>(null)
|
||||||
const leafletMapRef = useRef<L.Map | null>(null)
|
const leafletMapRef = useRef<L.Map | null>(null)
|
||||||
const mapRef = useRef<HTMLDivElement | null>(null)
|
const mapRef = useRef<HTMLDivElement | null>(null)
|
||||||
const [isOpen, setIsOpen] = useState(false)
|
const [isOpen, setIsOpen] = useState(false)
|
||||||
@@ -42,6 +44,7 @@ export const LocalitiesMap = ({ localitiesQueryData, localitiesQueryIsFetching }
|
|||||||
if (!mapRef.current || leafletMapRef.current) return
|
if (!mapRef.current || leafletMapRef.current) return
|
||||||
|
|
||||||
const map = L.map(mapRef.current, { maxZoom: 16 })
|
const map = L.map(mapRef.current, { maxZoom: 16 })
|
||||||
|
setMapInstance(map)
|
||||||
const coords: LatLngExpression = [15, 13]
|
const coords: LatLngExpression = [15, 13]
|
||||||
|
|
||||||
// To prevent eslint from complaining about that 'markers' variable below:
|
// To prevent eslint from complaining about that 'markers' variable below:
|
||||||
@@ -129,23 +132,23 @@ export const LocalitiesMap = ({ localitiesQueryData, localitiesQueryIsFetching }
|
|||||||
return () => {
|
return () => {
|
||||||
map.remove()
|
map.remove()
|
||||||
leafletMapRef.current = null
|
leafletMapRef.current = null
|
||||||
|
setMapInstance(null)
|
||||||
}
|
}
|
||||||
}, [localitiesQueryData, localitiesQueryIsFetching, columnFilters])
|
}, [localitiesQueryData, localitiesQueryIsFetching, columnFilters])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!leafletMapRef.current) return
|
if (!mapInstance) return
|
||||||
const map = leafletMapRef.current
|
const map = mapInstance
|
||||||
|
|
||||||
const handleMapClick = () => {
|
const handleMapClick = () => {
|
||||||
setSelectedLocality(null)
|
setSelectedLocality(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
map.on('click', handleMapClick)
|
map.on('click', handleMapClick)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
map.off('click', handleMapClick)
|
map.off('click', handleMapClick)
|
||||||
}
|
}
|
||||||
}, [])
|
}, [mapInstance])
|
||||||
|
|
||||||
// useEffect(() => {
|
// useEffect(() => {
|
||||||
// if (localityDetailsQueryData) {
|
// if (localityDetailsQueryData) {
|
||||||
|
|||||||
Reference in New Issue
Block a user