-
Notifications
You must be signed in to change notification settings - Fork 7
Add in a legend component with last updated metadata #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,8 @@ import "leaflet/dist/leaflet.css"; | |
| import {MapContainer, TileLayer} from "react-leaflet"; | ||
| import {LayerGroup} from "react-leaflet/LayerGroup"; | ||
| import {useMap} from "react-leaflet/hooks"; | ||
|
|
||
| import TournamentMarker from 'components/TournamentMarker' | ||
| import L from "leaflet"; | ||
| import TournamentMarker from 'components/TournamentMarker'; | ||
|
|
||
| const DEFAULT_ZOOM_LEVEL = 10; | ||
| const LOCATION_CACHE_KEY = "userLatLng"; | ||
|
|
@@ -55,6 +55,7 @@ const getLocation = () => { | |
|
|
||
| const Map = () => { | ||
| const [tourneyData, setTourneyData] = useState([]); | ||
| const [tourneyMetadata, setTourneyMetadata] = useState({}); | ||
| const [mapCenter, setMapCenter] = useState(FALLBACK_INITIAL_LOCATION); | ||
| const [locationLoading, setLocationLoading] = useState(true); | ||
|
|
||
|
|
@@ -76,7 +77,10 @@ const Map = () => { | |
| return await response.json() | ||
| } | ||
|
|
||
| fetchTournamentData().then((tourneyData) => { setTourneyData(tourneyData.tournament_data) }) | ||
| fetchTournamentData().then((tourneyData) => { | ||
| setTourneyData(tourneyData.tournament_data) | ||
| setTourneyMetadata(tourneyData.metadata) | ||
| }) | ||
| }, []); | ||
|
|
||
| return <> | ||
|
|
@@ -87,6 +91,7 @@ const Map = () => { | |
| <div className="loader"></div> | ||
| </>} | ||
| <ViewChanger center={mapCenter}/> | ||
| <Legend metadata={tourneyMetadata}/> | ||
| <TileLayer | ||
| attribution='© <a href="https://www.mapbox.com/about/maps/">Mapbox</a> © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback/" target="_blank">Improve this map</a></strong>' | ||
| url="https://api.mapbox.com/styles/v1/mapbox/outdoors-v11/tiles/512/{z}/{x}/{y}?access_token=pk.eyJ1IjoiZ3JhdmlkZGQiLCJhIjoiY2wwZDh3eDE2MDZ1OTNrcGYybjhsNmN2diJ9.cPvRZK6WTt_wjQSa-DzblQ" | ||
|
|
@@ -116,4 +121,23 @@ const ViewChanger = ({center}) => { | |
| return null; | ||
| } | ||
|
|
||
| const Legend = ({metadata}) => { | ||
| const map = useMap() | ||
| useEffect(() => { | ||
| if (map && metadata.updated_at) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to make sure something is actually inside of the metadata object before we go through with adding the legend to the map, otherwise it'll populate prematurely without waiting for the actual data. |
||
| const legend = new L.Control({ position: "bottomleft" }); | ||
|
|
||
| legend.onAdd = () => { | ||
| const div = L.DomUtil.create("div"); | ||
| div.id = "mapbox-legend" | ||
| div.innerHTML = `<h4>Last Updated: ${new Date(metadata.updated_at * 1000).toLocaleString()}</h4>`; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having to manually manipulate innerHTML feels a little suspicious to me. I suspect there may be a better way to do this with the react-leaflet library, but moreso I bet there's a way we can accomplish this without needing to do that, either. In general one should probably not need to touch innerHTML or similar things inside a react project. |
||
| return div; | ||
| }; | ||
|
|
||
| legend.addTo(map); | ||
| } | ||
| }, [map, metadata.updated_at]); | ||
| return null; | ||
| } | ||
|
|
||
| export default Map; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's shrink the padding so this takes up less space - maybe something similar to whatever the padding is for the attribution in the other corner?