-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move marker fetching to map
- Loading branch information
Rupert Redington
authored and
Rupert Redington
committed
Nov 22, 2021
1 parent
689609c
commit 0cf3266
Showing
8 changed files
with
90 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useMemo, useCallback } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
import { useFindLoosNearbyQuery } from '../../api-client/graphql'; | ||
import { Marker, useMapEvents, useMap } from 'react-leaflet'; | ||
import ToiletMarkerIcon from './ToiletMarkerIcon'; | ||
|
||
const KEY_ENTER = 13; | ||
|
||
function mapToFindVars(map) { | ||
return { | ||
lat: map.getCenter().lat, | ||
lng: map.getCenter().lng, | ||
radius: Math.ceil( | ||
map.getBounds().getNorthEast().distanceTo(map.getCenter()) | ||
), | ||
}; | ||
} | ||
|
||
const Markers = ({ focus }) => { | ||
const router = useRouter(); | ||
const map = useMap(); | ||
|
||
const { data, refetch } = useFindLoosNearbyQuery({ | ||
variables: { | ||
...mapToFindVars(map), | ||
}, | ||
}); | ||
|
||
useMapEvents({ | ||
moveend: () => refetch(mapToFindVars(map)), | ||
}); | ||
|
||
const memoizedMarkers = useMemo(() => { | ||
if (!data?.loosByProximity) { | ||
return null; | ||
} | ||
return data.loosByProximity.map((toilet) => ( | ||
<Marker | ||
key={toilet.id} | ||
position={toilet.location} | ||
zIndexOffset={toilet.id === focus?.id ? 1000 : 0} | ||
icon={ | ||
new ToiletMarkerIcon({ | ||
isHighlighted: toilet.id === focus?.id, | ||
toiletId: toilet.id, | ||
isUseOurLoosCampaign: toilet.campaignUOL, | ||
}) | ||
} | ||
alt={toilet.name || 'Unnamed toilet'} | ||
eventHandlers={{ | ||
click: () => { | ||
router.push(`/loos/${toilet.id}`); | ||
}, | ||
keydown: (event: { originalEvent: { keyCode: number } }) => { | ||
if (event.originalEvent.keyCode === KEY_ENTER) { | ||
router.push(`/loos/${toilet.id}`); | ||
} | ||
}, | ||
}} | ||
keyboard={false} | ||
/> | ||
)); | ||
}, [data, router, focus]); | ||
|
||
return <>{memoizedMarkers}</>; | ||
}; | ||
|
||
export default Markers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters