Skip to content

Commit

Permalink
Fix location fetch and map update
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraGeowerkstatt committed Dec 17, 2024
1 parent f780876 commit ec5d534
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/client/src/pages/detail/form/location/locationSegment.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) {
Expand Down Expand Up @@ -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],
);
Expand Down Expand Up @@ -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}
/>
</FormSegmentBox>
</Grid>
Expand Down

0 comments on commit ec5d534

Please sign in to comment.