From ec5d53449e51971087429494e3869acad4f1a908 Mon Sep 17 00:00:00 2001 From: MiraGeowerkstatt Date: Tue, 17 Dec 2024 15:48:38 +0100 Subject: [PATCH 1/2] Fix location fetch and map update --- .../detail/form/location/locationSegment.tsx | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/client/src/pages/detail/form/location/locationSegment.tsx b/src/client/src/pages/detail/form/location/locationSegment.tsx index f9a83cea5..d3895c239 100644 --- a/src/client/src/pages/detail/form/location/locationSegment.tsx +++ b/src/client/src/pages/detail/form/location/locationSegment.tsx @@ -1,4 +1,4 @@ -import { useCallback } from "react"; +import { useCallback, useState } from "react"; import { UseFormReturn } from "react-hook-form"; import { Card, Grid, Stack } from "@mui/material"; import { fetchApiV2 } from "../../../../api/fetchApiV2"; @@ -17,6 +17,8 @@ interface LocationSegmentProps extends LocationBaseProps { } const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMethods }: LocationSegmentProps) => { + const [currentLV95X, setCurrentLV95X] = useState(borehole.locationX ? Number(borehole.locationX) : null); + const [currentLV95Y, setCurrentLV95Y] = useState(borehole.locationY ? Number(borehole.locationY) : null); const transformCoordinates = useCallback(async (referenceSystem: string, x: number, y: number) => { let apiUrl; if (referenceSystem === referenceSystems.LV95.name) { @@ -70,12 +72,18 @@ const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMeth if (!response) return; // Ensure response is valid const maxPrecision = Math.max(XPrecision, YPrecision); - const transformedX = parseFloat(response.easting).toFixed(maxPrecision); - const transformedY = parseFloat(response.northing).toFixed(maxPrecision); + const transformedX = parseFloat(response.easting); + const transformedY = parseFloat(response.northing); - const location = await fetchApiV2(`location/identify?east=${X}&north=${Y}`, "GET"); + const XLV95 = sourceSystem === ReferenceSystemKey.LV95 ? X : transformedX; + const YLV95 = sourceSystem === ReferenceSystemKey.LV95 ? Y : transformedY; + + setCurrentLV95X(XLV95); + setCurrentLV95Y(YLV95); + + const location = await fetchApiV2(`location/identify?east=${XLV95}&north=${YLV95}`, "GET"); setValuesForCountryCantonMunicipality(location); - setValuesForReferenceSystem(targetSystem, transformedX, transformedY); + setValuesForReferenceSystem(targetSystem, transformedX.toFixed(maxPrecision), transformedY.toFixed(maxPrecision)); }, [setValuesForCountryCantonMunicipality, setValuesForReferenceSystem, transformCoordinates], ); @@ -130,8 +138,8 @@ const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMeth }} id={borehole.id} isEditable={editingEnabled} - x={borehole.locationX ? Number(borehole.locationX) : null} - y={borehole.locationY ? Number(borehole.locationY) : null} + x={currentLV95X} + y={currentLV95Y} /> From ccd5dee93acec5ac8c775820b6a81c0050a0d035 Mon Sep 17 00:00:00 2001 From: MiraGeowerkstatt Date: Tue, 17 Dec 2024 16:50:26 +0100 Subject: [PATCH 2/2] Add cypress test --- .../cypress/e2e/detailPage/coordinates.cy.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/client/cypress/e2e/detailPage/coordinates.cy.js b/src/client/cypress/e2e/detailPage/coordinates.cy.js index dfc949d1a..0eb51e565 100644 --- a/src/client/cypress/e2e/detailPage/coordinates.cy.js +++ b/src/client/cypress/e2e/detailPage/coordinates.cy.js @@ -196,4 +196,44 @@ describe("Tests for editing coordinates of a borehole.", () => { checkDecimalPlaces("@LV03X-input", 5); checkDecimalPlaces("@LV03Y-input", 5); }); + + it("updates canton and municipality when changing coordinates", () => { + // Type coordinates for Samaden in LV95 + cy.get("@LV95X-input").type("2789000"); + cy.get("@LV95Y-input").type("1155000"); + cy.wait("@location"); + cy.wait(4000); + + cy.get("@country").should("have.value", "Schweiz"); + cy.get("@canton").should("have.value", "Graubünden"); + cy.get("@municipality").should("have.value", "Samaden"); + + // Type coordinates for Unterentfelden in LV95 + cy.get("@LV95X-input").clear().type("2646000"); + cy.get("@LV95Y-input").clear().type("1247000"); + + cy.get("@country").should("have.value", "Schweiz"); + cy.get("@canton").should("have.value", "Aargau"); + cy.get("@municipality").should("have.value", "Unterentfelden"); + + // switch reference system to LV03 + setSelect("originalReferenceSystem", 1); + handlePrompt("Changing the coordinate system will reset the coordinates. Do you want to continue?", "Confirm"); + + // Type coordinates for Samaden in LV03 + cy.get("@LV03X-input").clear().type("789000"); + cy.get("@LV03Y-input").clear().type("155000"); + + cy.get("@country").should("have.value", "Schweiz"); + cy.get("@canton").should("have.value", "Graubünden"); + cy.get("@municipality").should("have.value", "Samaden"); + + // Type coordinates for Unterentfelden in LV03 + cy.get("@LV03X-input").clear().type("646000"); + cy.get("@LV03Y-input").clear().type("247000"); + + cy.get("@country").should("have.value", "Schweiz"); + cy.get("@canton").should("have.value", "Aargau"); + cy.get("@municipality").should("have.value", "Unterentfelden"); + }); });