From 814606ca30b849d6569800975a520ccf29a45644 Mon Sep 17 00:00:00 2001 From: Samuel Therrien Date: Thu, 28 Nov 2024 12:24:09 -0500 Subject: [PATCH] hotfix map page autozoom on preselection and site height --- .../src/components/MainLayout.tsx | 2 +- canopeum_frontend/src/pages/MapPage.scss | 2 +- canopeum_frontend/src/pages/MapPage.tsx | 27 +++++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/canopeum_frontend/src/components/MainLayout.tsx b/canopeum_frontend/src/components/MainLayout.tsx index 022caa78a..1c80dd410 100644 --- a/canopeum_frontend/src/components/MainLayout.tsx +++ b/canopeum_frontend/src/components/MainLayout.tsx @@ -20,7 +20,7 @@ const NavbarLayout = () => (
{/* TODO: Can we do better than hardcode a number relating to the expected header size here? */} -
+
diff --git a/canopeum_frontend/src/pages/MapPage.scss b/canopeum_frontend/src/pages/MapPage.scss index 80d00eaf8..462991d18 100644 --- a/canopeum_frontend/src/pages/MapPage.scss +++ b/canopeum_frontend/src/pages/MapPage.scss @@ -18,7 +18,7 @@ @media screen and (max-width: $large-width) { #map-sites-list-container, #map-container { - padding: 0 3rem; + padding: 0 2rem; } } diff --git a/canopeum_frontend/src/pages/MapPage.tsx b/canopeum_frontend/src/pages/MapPage.tsx index 289abfc46..9084f782f 100644 --- a/canopeum_frontend/src/pages/MapPage.tsx +++ b/canopeum_frontend/src/pages/MapPage.tsx @@ -18,9 +18,9 @@ const PIN_FOCUS_ZOOM_LEVEL = 15 const MAP_DISTANCE_ZOOM_MULTIPLIER = 20 /** - * The initial map location if the user doesn't provide location + * The default initial map location if the user doesn't provide location */ -const initialMapLocation = (sites: SiteMap[]) => { +const defaultMapLocation = (sites: SiteMap[]) => { // eslint-disable-next-line unicorn/no-array-reduce -- Find the middle point between all sites const { minLat, maxLat, minLong, maxLong } = sites.reduce( (previous, current) => { @@ -115,21 +115,32 @@ const MapPage = () => { navigator.geolocation.getCurrentPosition(resolve, resolve) }, ), - ]).then(([fetchedSites, position]) => - 'code' in position + ]).then(([fetchedSites, position]) => { + // If there's a pre-selected site-id (from URL), zoom on it + const preSelectedSiteId = Number(searchParams.get('site')) || null + const preSelectedSite = fetchedSites.find(site => site.id === preSelectedSiteId) + if (preSelectedSite?.coordinate.ddLatitude && preSelectedSite.coordinate.ddLongitude) { + setMapViewState({ + latitude: preSelectedSite.coordinate.ddLatitude, + longitude: preSelectedSite.coordinate.ddLongitude, + zoom: PIN_FOCUS_ZOOM_LEVEL, + }) + } else if ('code' in position) { // If there's an error obtaining the user position, use our default position instead // Note that getCurrentPosition always error code 2 in http - ? setMapViewState(mvs => ({ + setMapViewState(mvs => ({ ...mvs, - ...initialMapLocation(fetchedSites), + ...defaultMapLocation(fetchedSites), })) + } else { // Otherwise focus on the user's position - : setMapViewState(mvs => ({ + setMapViewState(mvs => ({ ...mvs, latitude: position.coords.latitude, longitude: position.coords.longitude, })) - ), [fetchData]) + } + }), [fetchData, searchParams]) return (