From b9adca216ede99fba9fea79277811a434b227063 Mon Sep 17 00:00:00 2001 From: Jeff Jacobson Date: Mon, 5 Aug 2024 11:58:20 -0700 Subject: [PATCH] fix: :bug: Subsequent form submissions open popup in wrong location --- src/setupForm.ts | 55 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/setupForm.ts b/src/setupForm.ts index 51e636db..e62b8fe2 100644 --- a/src/setupForm.ts +++ b/src/setupForm.ts @@ -8,6 +8,8 @@ import { type RouteInputEvent, } from "./widgets/route-input/SrmpInputForm"; import type Graphic from "@arcgis/core/Graphic"; +import Viewpoint from "@arcgis/core/Viewpoint"; +import Point from "@arcgis/core/geometry/Point"; import type FeatureLayer from "@arcgis/core/layers/FeatureLayer"; import type MapView from "@arcgis/core/views/MapView"; @@ -25,22 +27,45 @@ export async function setupForm(view: MapView, milepostLayer: FeatureLayer) { form.addEventListener( "srmp-input", (event: RouteInputEvent) => { + async function openPopupAtLocation( + features: Graphic[] | null | undefined, + ) { + if (!features) { + return; + } + if (features.length === 0) { + console.error( + "An array of zero features was returned from addSrmpFromForm. There should be at least one.", + ); + return; + } + const feature = features[0]; + const point = + feature.geometry instanceof Point ? feature.geometry : undefined; + + if (!point) { + console.error( + "No point was returned from addSrmpFromForm. There should be at least one.", + ); + return; + } + + const viewpoint = new Viewpoint({ + targetGeometry: point, + scale: parseFloat(import.meta.env.VITE_ZOOM_SCALE), + }); + + await view.openPopup({ + features, + location: point, + }); + await view.goTo(viewpoint, { + animate: false, + }); + } + addSrmpFromForm(event, view, milepostLayer) - .then((features) => { - if (!features) { - return; - } - view - .openPopup({ - features, - }) - .catch((reason: unknown) => { - console.error( - "Failed to open popup after adding a location via the form", - reason, - ); - }); - }) + .then(openPopupAtLocation) .catch((error: unknown) => { console.error("Error adding SRMP from form", error); });