From 5347b18a6dcb6aacb80c6faac0159edeecd32fbe Mon Sep 17 00:00:00 2001 From: Benoit Simard Date: Thu, 8 Dec 2022 14:07:21 +0100 Subject: [PATCH 1/4] front: editor - fix id generation on creation --- front/src/applications/editor/data/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/src/applications/editor/data/api.ts b/front/src/applications/editor/data/api.ts index 6622fb61d17..5828f250f5b 100644 --- a/front/src/applications/editor/data/api.ts +++ b/front/src/applications/editor/data/api.ts @@ -116,7 +116,7 @@ export async function editorSave( obj_type: feature.objType, railjson: { ...feature.properties, - id: feature.properties.id || uuid(), + id: uuid(), }, }) ), From 406a119eb65f057644718f2ef99ff27602180fce Mon Sep 17 00:00:00 2001 From: Benoit Simard Date: Thu, 8 Dec 2022 14:07:58 +0100 Subject: [PATCH 2/4] front: edition - fix length field when creating a new tracksection --- .../editor/components/EditorForm.tsx | 14 ++++--- .../LinearMetadata/FormLineStringLength.tsx | 39 ++++++++----------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/front/src/applications/editor/components/EditorForm.tsx b/front/src/applications/editor/components/EditorForm.tsx index 9691915cb65..72035e0fea4 100644 --- a/front/src/applications/editor/components/EditorForm.tsx +++ b/front/src/applications/editor/components/EditorForm.tsx @@ -3,11 +3,12 @@ import Form, { Field, UiSchema } from '@rjsf/core'; import { useSelector } from 'react-redux'; import { GeoJsonProperties } from 'geojson'; import { JSONSchema7 } from 'json-schema'; +import { isNil } from 'lodash'; import './EditorForm.scss'; import { EditorEntity } from '../../../types'; import { FormComponent, FormLineStringLength } from './LinearMetadata'; -import { getJsonSchemaForLayer, getLayerForObjectType } from '../data/utils'; +import { getJsonSchemaForLayer, getLayerForObjectType, NEW_ENTITY_ID } from '../data/utils'; import { EditorState } from '../tools/types'; const fields = { @@ -91,13 +92,16 @@ const EditorForm: React.FC> = ({ ...(overrideUiSchema || {}), }} formData={formData} - formContext={{ geometry: data.geometry, length: data.properties?.length }} + formContext={{ + geometry: data.geometry, + length: data.properties?.length, + isCreation: isNil(formData?.id) || formData?.id === NEW_ENTITY_ID, + }} onError={() => setSubmited(true)} - onSubmit={async (event) => { + onSubmit={async () => { try { setError(null); - setFormData(event.formData); - await onSubmit({ ...data, properties: { ...data.properties, ...event.formData } }); + await onSubmit({ ...data, properties: { ...data.properties, ...formData } }); } catch (e) { if (e instanceof Error) setError(e.message); else setError(JSON.stringify(e)); diff --git a/front/src/applications/editor/components/LinearMetadata/FormLineStringLength.tsx b/front/src/applications/editor/components/LinearMetadata/FormLineStringLength.tsx index 1a3bbc7dfcf..f495b559d7f 100644 --- a/front/src/applications/editor/components/LinearMetadata/FormLineStringLength.tsx +++ b/front/src/applications/editor/components/LinearMetadata/FormLineStringLength.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState, useCallback } from 'react'; import { WidgetProps } from '@rjsf/core'; import { useTranslation } from 'react-i18next'; @@ -8,30 +8,29 @@ export const FormLineStringLength: React.FC = (props) => { const { t } = useTranslation(); const { id, value, required, readonly, onChange, formContext } = props; - const [length, setLength] = useState(value); + const [length, setLength] = useState(value || 0); const [min, setMin] = useState(-Infinity); const [max, setMax] = useState(Infinity); const [geoLength, setGeoLength] = useState(0); + useEffect(() => { + setLength(value || 0); + }, [value]); + /** * When the geometry changes * => recompute min & max plus its length */ useEffect(() => { const distance = getLineStringDistance(formContext.geometry); - setMin(Math.round(distance - distance * DISTANCE_ERROR_RANGE)); - setMax(Math.round(distance + distance * DISTANCE_ERROR_RANGE)); - setGeoLength(distance); - }, [formContext.geometry]); - - /** - * When the input value change - * => if it is valid, we call the onChange - */ - useEffect(() => { - if (value !== undefined) setLength(value); - else setLength(geoLength); - }, [value, geoLength]); + if (formContext.isCreation) { + setTimeout(() => onChange(distance), 0); + } else { + setMin(Math.round(distance - distance * DISTANCE_ERROR_RANGE)); + setMax(Math.round(distance + distance * DISTANCE_ERROR_RANGE)); + setGeoLength(distance); + } + }, [formContext.geometry, formContext.isCreation, onChange]); return (
@@ -43,19 +42,15 @@ export const FormLineStringLength: React.FC = (props) => { id={id} required={required} type="number" - min={min} - max={max} - step="any" value={length} onChange={(e) => { const nValue = parseFloat(e.target.value); - if (nValue >= min && nValue <= max) onChange(nValue); - else setLength(nValue); + onChange(nValue); }} /> )} - {(length === undefined || length < min || length > max) && ( -

+ {geoLength && (length === undefined || length < min || length > max) && ( +

{t('Editor.errors.length-out-of-sync-with-geometry', { min, max })}.{' '}