Skip to content

Commit

Permalink
Fix form reset (#1570)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraGeowerkstatt authored Oct 3, 2024
2 parents 1865ae6 + 6eff383 commit 9b6312d
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 142 deletions.
36 changes: 34 additions & 2 deletions src/client/cypress/e2e/detailPage/boreholeform.cy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { evaluateSelect, isDisabled, setSelect } from "../helpers/formHelpers";
import { createBorehole, goToRouteAndAcceptTerms, newEditableBorehole } from "../helpers/testHelpers";
import { clickOnRowWithText, showTableAndWaitForData, sortBy } from "../helpers/dataGridHelpers";
import { evaluateInput, evaluateSelect, isDisabled, setSelect } from "../helpers/formHelpers";
import { createBorehole, goToRouteAndAcceptTerms, newEditableBorehole, returnToOverview } from "../helpers/testHelpers";

describe("Test for the borehole form.", () => {
it("Creates a borehole and fills dropdowns.", () => {
Expand Down Expand Up @@ -58,6 +59,37 @@ describe("Test for the borehole form.", () => {
});
});

it("Checks if form values are updated when borehole changes", () => {
showTableAndWaitForData();
// sort by Name descending
sortBy("Name");
clickOnRowWithText("Zena Rath");

evaluateSelect("restriction", "");
evaluateSelect("national_interest", "0"); // No
evaluateSelect("spatial_reference_system", "20104002"); // LV03
evaluateSelect("location_precision", "20113005");

evaluateInput("elevation_z", "3'519.948980314633");
evaluateInput("reference_elevation", "3'554.9389396584306");
evaluateSelect("elevation_precision", "");
evaluateSelect("qt_reference_elevation", "");
evaluateSelect("reference_elevation_type", "");

returnToOverview();
clickOnRowWithText("Zena Mraz");
evaluateSelect("restriction", "");
evaluateSelect("national_interest", "1"); // Yes
evaluateSelect("spatial_reference_system", "20104002"); // LV03
evaluateSelect("location_precision", "20113007"); // not specified

evaluateInput("elevation_z", "3'062.9991330499756");
evaluateInput("reference_elevation", "3'478.1368118609007");
evaluateSelect("elevation_precision", "20114003"); // 1
evaluateSelect("qt_reference_elevation", "20114005"); // 0.1
evaluateSelect("reference_elevation_type", "30000013"); // kelly bushing
});

it("switches tabs", () => {
let boreholeId;
createBorehole({ "extended.original_name": "LSENALZE" }).as("borehole_id");
Expand Down
1 change: 0 additions & 1 deletion src/client/public/locale/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
"errorMissingBedrockSolution": "Tiefe Top Fels automatisch aus der Bohrdaten übernehmen",
"errorOverlap": "Überlappende Schichten",
"errorStartEditing": "Sie sollten die Schaltfläche 'Bearbeitung beginnen' drücken",
"errorStartEditingWrongStatus": "Bohrung mit dem Status {{status}} kann nicht editiert werden.",
"errorStartWrong": "Erste Schicht beginnt nicht auf dem Referenzniveau",
"errorToDepthTooHigh": "bis Tiefe ist zu gross",
"errorWrongDepth": "Falsche Bohrendteufe",
Expand Down
1 change: 0 additions & 1 deletion src/client/public/locale/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
"errorMissingBedrockSolution": "Take the top bedrock depth automatically from the borehole data",
"errorOverlap": "Overlapping layers",
"errorStartEditing": "You should press the 'start editing' button",
"errorStartEditingWrongStatus": "Borehole with status {{status}} not editable",
"errorStartWrong": "First layer does not start at the reference level",
"errorToDepthTooHigh": "to depth is too high",
"errorWrongDepth": "Incorrect borehole end depth",
Expand Down
1 change: 0 additions & 1 deletion src/client/public/locale/fr/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
"errorMissingBedrockSolution": "Reprendre automatiquement la profondeur du toit du rocher à partir de la page des données de forage",
"errorOverlap": "Des couches se recouvrent",
"errorStartEditing": "Veuillez appuyer sur le bouton 'Commencer l'édition'",
"errorStartEditingWrongStatus": "Forage avec le statut {{status}} non modifiable",
"errorStartWrong": "La première couche ne débute pas au niveau de la référence altitudinale",
"errorToDepthTooHigh": "de la profondeur est trop élevée",
"errorWrongDepth": "Profondeur finale erronée",
Expand Down
1 change: 0 additions & 1 deletion src/client/public/locale/it/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
"errorMissingBedrockSolution": "Crea automaticamente la profondità del substrato roccioso attingendo dai dati inseriti della perforazione",
"errorOverlap": "Sovrapposizione di strati",
"errorStartEditing": "Per favore premere il pulsante 'iniziare la modifica'",
"errorStartEditingWrongStatus": "Perforazione con stato {{status}} non modificabile",
"errorStartWrong": "Il primo strato non inizia dalla riferimento della quota",
"errorToDepthTooHigh": "la profondità è troppo alta",
"errorWrongDepth": "Profondità di perforazione finale errata",
Expand Down
81 changes: 81 additions & 0 deletions src/client/src/components/form/simpleBooleanSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { FC } from "react";
import { useTranslation } from "react-i18next";
import { MenuItem, SxProps } from "@mui/material";
import { TextField } from "@mui/material/";
import { FormSelectMenuItem } from "./formSelect.tsx";

// This component is needed as an intermediate step to refactor borehole input.
// The standard form components are not usable with autosave components as they are now.
// Once the saving mechanism is refactored, this component can be replaced.

interface SimpleBooleanSelectProps {
fieldName: string;
label: string;
required?: boolean;
disabled?: boolean;
readonly?: boolean;
selected?: number | boolean | null;
sx?: SxProps;
className?: string;
onUpdate?: (value: number | boolean | string | null) => void;
}

export const SimpleBooleanSelect: FC<SimpleBooleanSelectProps> = ({
fieldName,
label,
required,
disabled,
readonly,
selected,
sx,
className,
onUpdate,
}) => {
const { t } = useTranslation();

const menuItems: FormSelectMenuItem[] = [];
if (!required) {
menuItems.push({ key: 0, value: undefined, label: t("reset"), italic: true });
}

const value = selected === true ? 1 : selected === false ? 0 : 2;

const values = [
{ key: 1, name: t("yes") },
{ key: 0, name: t("no") },
{ key: 2, name: t("np") },
];

values.forEach(v =>
menuItems.push({
key: v.key,
value: v.key,
label: v.name,
}),
);

return (
<TextField
required={required}
className={`${readonly ? "readonly" : ""} ${className || ""}`}
select
sx={{ ...sx }}
label={t(label)}
name={fieldName}
onChange={e =>
onUpdate
? onUpdate(parseInt(e.target.value) === 1 ? true : parseInt(e.target.value) === 0 ? false : null)
: null
}
value={value}
disabled={disabled ?? false}
data-cy={fieldName + "-formSelect"}
InputProps={{ readOnly: readonly, disabled: disabled }}>
{menuItems.map(item => (
<MenuItem key={item.key} value={item.value}>
{item.italic ? <em>{item.label}</em> : item.label}
</MenuItem>
))}
</TextField>
);
};
80 changes: 80 additions & 0 deletions src/client/src/components/form/simpleDomainSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { FC } from "react";
import { useTranslation } from "react-i18next";
import { MenuItem, SxProps } from "@mui/material";
import { TextField } from "@mui/material/";
import { useDomains } from "../../api/fetchApiV2";
import { Codelist } from "../legacyComponents/domain/domainInterface.ts";
import { FormSelectMenuItem } from "./formSelect.tsx";

// This component is needed as an intermediate step to refactor borehole input.
// The standard form components are not usable with autosave components as they are now.
// Once the saving mechanism is refactored, this component can be replaced.

interface SimpleDomainSelectProps {
fieldName: string;
schemaName: string;
label: string;
required?: boolean;
disabled?: boolean;
readonly?: boolean;
selected?: number | boolean | null;
sx?: SxProps;
className?: string;
onUpdate?: (value: number | boolean | string | null) => void;
}

export const SimpleDomainSelect: FC<SimpleDomainSelectProps> = ({
fieldName,
label,
required,
disabled,
readonly,
selected,
schemaName,
sx,
className,
onUpdate,
}) => {
const { t, i18n } = useTranslation();
const { data: domains } = useDomains();

const menuItems: FormSelectMenuItem[] = [];
if (!required) {
menuItems.push({ key: 0, value: undefined, label: t("reset"), italic: true });
}
domains
?.filter((d: Codelist) => d.schema === schemaName)
.sort((a: Codelist, b: Codelist) => a.order - b.order)
.forEach((d: Codelist) =>
menuItems.push({
key: d.id,
value: d.id,
label: d[i18n.language] as string,
}),
);

return (
<TextField
required={required}
className={`${readonly ? "readonly" : ""} ${className || ""}`}
select
sx={{ ...sx }}
label={t(label)}
name={fieldName}
onChange={e => {
if (onUpdate) {
onUpdate(e.target.value);
}
}}
value={selected}
disabled={disabled ?? false}
data-cy={fieldName + "-formSelect"}
InputProps={{ readOnly: readonly, disabled: disabled }}>
{menuItems.map(item => (
<MenuItem key={item.key} value={item.value}>
{item.italic ? <em>{item.label}</em> : item.label}
</MenuItem>
))}
</TextField>
);
};
68 changes: 68 additions & 0 deletions src/client/src/components/form/simpleFormInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { FC } from "react";
import { useTranslation } from "react-i18next";
import { InputProps, SxProps, TextField } from "@mui/material";
import { FormValueType } from "./form.ts";
import { NumericFormatWithThousandSeparator } from "./numericFormatWithThousandSeparator.tsx";

interface SimpleInputProps {
label: string;
required?: boolean;
disabled?: boolean;
readonly?: boolean;
type?: FormValueType;
multiline?: boolean;
rows?: number;
value?: string | number | Date | null;
sx?: SxProps;
className?: string;
inputProps?: InputProps;
onUpdate?: (value: string) => void;
withThousandSeparator?: boolean;
}

// This component is needed as an intermediate step to refactor borehole input.
// The standard form components are not usable with autosave components as they are now.
// Once the saving mechanism is refactored, this component can be replaced.

export const SimpleFormInput: FC<SimpleInputProps> = ({
value,
className,
required,
readonly,
rows,
onUpdate,
label,
type,
multiline,
withThousandSeparator,
inputProps,
disabled,
sx,
}) => {
const { t } = useTranslation();

return (
<TextField
required={required || false}
sx={{ ...sx }}
className={`${readonly ? "readonly" : ""} ${className || ""}`}
type={type || FormValueType.Text}
multiline={multiline || false}
rows={rows}
value={value}
label={t(label)}
data-cy={label + "-formInput"}
onChange={e => {
if (onUpdate) {
onUpdate(e.target.value);
}
}}
InputProps={{
...inputProps /* eslint-disable @typescript-eslint/no-explicit-any */,
...(withThousandSeparator && { inputComponent: NumericFormatWithThousandSeparator as any }),
readOnly: readonly,
disabled: disabled,
}}
/>
);
};
6 changes: 0 additions & 6 deletions src/client/src/pages/detail/detailPageContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ class DetailPageContent extends React.Component {
checkLock() {
const { t, editingEnabled, editableByCurrentUser } = this.props;
if (this.props.borehole.data.role !== "EDIT") {
this.context.showAlert(
t("common:errorStartEditingWrongStatus", {
status: this.props.borehole.data.role,
}),
"error",
);
return false;
}
if (!editingEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Card, CardContent, CardHeader } from "@mui/material";
import { fetchApiV2 } from "../../../../api/fetchApiV2.js";
import { LabelingButton } from "../../../../components/buttons/labelingButton.tsx";
import { FormContainer, FormCoordinate, FormSelect } from "../../../../components/form/form";
import { FormDomainSelect } from "../../../../components/form/formDomainSelect.tsx";
import { SimpleDomainSelect } from "../../../../components/form/simpleDomainSelect.tsx";
import {
getPrecisionFromString,
parseFloatWithThousandsSeparator,
Expand Down Expand Up @@ -474,7 +474,7 @@ const CoordinatesSegment: React.FC<CoordinatesSegmentProps> = ({
/>
</FormContainer>
</FormContainer>
<FormDomainSelect
<SimpleDomainSelect
fieldName={`location_precision`}
label="location_precision"
readonly={!editingEnabled}
Expand Down
Loading

0 comments on commit 9b6312d

Please sign in to comment.