Skip to content

fix: 🐛 Subsequent form submissions open popup in wrong location #414

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

Merged
merged 1 commit into from
Aug 5, 2024
Merged
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
55 changes: 40 additions & 15 deletions src/setupForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
});
Expand Down