Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"html-webpack-plugin": "^5.5.0",
"jest": "^27.4.3",
"jest-watch-typeahead": "^1.0.0",
"leaflet": "^1.8.0",
"leaflet": "^1.9.3",
"mini-css-extract-plugin": "^2.4.5",
"postcss": "^8.4.4",
"postcss-flexbugs-fixes": "^5.0.2",
Expand Down
5 changes: 5 additions & 0 deletions public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,8 @@ ol {
background-position: 0 0;
background-size: 65px 20px;
}

#mapbox-legend {
background-color: white;
padding: 1em;
Copy link
Copy Markdown
Owner

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?

}
30 changes: 27 additions & 3 deletions src/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);

Expand All @@ -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 <>
Expand All @@ -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"
Expand Down Expand Up @@ -116,4 +121,23 @@ const ViewChanger = ({center}) => {
return null;
}

const Legend = ({metadata}) => {
const map = useMap()
useEffect(() => {
if (map && metadata.updated_at) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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>`;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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;