diff --git a/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx b/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx index 77d6f7eada..ff2ba0c7ea 100644 --- a/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx +++ b/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx @@ -6,35 +6,28 @@ */ import { ModificationDialog } from '../../../commons/modificationDialog'; -import { useCallback, useEffect, useState } from 'react'; -import VoltageLevelModificationForm from './voltage-level-modification-form'; -import { - EQUIPMENT_NAME, - HIGH_SHORT_CIRCUIT_CURRENT_LIMIT, - HIGH_VOLTAGE_LIMIT, - LOW_SHORT_CIRCUIT_CURRENT_LIMIT, - LOW_VOLTAGE_LIMIT, - NOMINAL_V, - SUBSTATION_ID, -} from 'components/utils/field-constants'; -import yup from 'components/utils/yup-config'; -import { yupResolver } from '@hookform/resolvers/yup'; +import { useCallback, useEffect, useMemo, useState } from 'react'; import { convertInputValue, - convertOutputValue, CustomFormProvider, - emptyProperties, + DeepNullable, EquipmentType, EquipmentWithProperties, FieldConstants, FieldType, getConcatenatedProperties, - getPropertiesFromModification, - modificationPropertiesSchema, snackWithFallback, - toModificationProperties, useSnackMessage, + VoltageLevelDto, + VoltageLevelModificationDto, + VoltageLevelModificationForm, + VoltageLevelModificationFormData, + voltageLevelModificationDtoToForm, + voltageLevelModificationEmptyFormData, + voltageLevelModificationFormSchema, + voltageLevelModificationFormToDto, } from '@gridsuite/commons-ui'; +import { yupResolver } from '@hookform/resolvers/yup'; import { useOpenShortWaitFetching } from '../../../commons/handle-modification-form'; import { FORM_LOADING_DELAY } from 'components/network/constants'; import { EQUIPMENT_INFOS_TYPES } from 'components/utils/equipment-types'; @@ -49,21 +42,8 @@ import { CurrentTreeNode } from '../../../../graph/tree-node.type'; type FetchStatusType = (typeof FetchStatus)[keyof typeof FetchStatus]; -interface EditData { - uuid?: UUID; - equipmentId?: string; - equipmentName?: { value: string }; - substationId?: { value: string }; - nominalV?: { value: number }; - lowVoltageLimit?: { value: number }; - highVoltageLimit?: { value: number }; - ipMin?: { value: number }; - ipMax?: { value: number }; - properties?: any; -} - interface VoltageLevelModificationDialogProps { - editData?: EditData; + editData?: VoltageLevelModificationDto; defaultIdValue?: string | null; currentNode: CurrentTreeNode | null; currentRootNetworkUuid: UUID; @@ -73,59 +53,6 @@ interface VoltageLevelModificationDialogProps { [key: string]: any; } -interface VoltageLevelFormData { - [EQUIPMENT_NAME]?: string; - [SUBSTATION_ID]?: string; - [NOMINAL_V]?: number; - [LOW_VOLTAGE_LIMIT]?: number; - [HIGH_VOLTAGE_LIMIT]?: number; - [LOW_SHORT_CIRCUIT_CURRENT_LIMIT]?: number; - [HIGH_SHORT_CIRCUIT_CURRENT_LIMIT]?: number; - [FieldConstants.ADDITIONAL_PROPERTIES]?: any; - [key: string]: any; -} - -const emptyFormData = { - [EQUIPMENT_NAME]: '', - [SUBSTATION_ID]: null, - [NOMINAL_V]: null, - [LOW_VOLTAGE_LIMIT]: null, - [HIGH_VOLTAGE_LIMIT]: null, - [LOW_SHORT_CIRCUIT_CURRENT_LIMIT]: null, - [HIGH_SHORT_CIRCUIT_CURRENT_LIMIT]: null, - ...emptyProperties, -}; - -const formSchema = yup - .object() - .shape({ - [EQUIPMENT_NAME]: yup.string().nullable(), - [SUBSTATION_ID]: yup.string().nullable(), - [NOMINAL_V]: yup.number().nullable().min(0, 'mustBeGreaterOrEqualToZero'), - [LOW_VOLTAGE_LIMIT]: yup - .number() - .nullable() - .min(0, 'mustBeGreaterOrEqualToZero') - .when([HIGH_VOLTAGE_LIMIT], { - is: (highVoltageLimit: number) => highVoltageLimit != null, - then: (schema) => schema.max(yup.ref(HIGH_VOLTAGE_LIMIT), 'voltageLevelNominalVoltageMaxValueError'), - }), - [HIGH_VOLTAGE_LIMIT]: yup.number().nullable().min(0, 'mustBeGreaterOrEqualToZero'), - [LOW_SHORT_CIRCUIT_CURRENT_LIMIT]: yup - .number() - .nullable() - .min(0, 'ShortCircuitCurrentLimitMustBeGreaterOrEqualToZero') - .when([HIGH_SHORT_CIRCUIT_CURRENT_LIMIT], { - is: (highShortCircuitCurrentLimit: number) => highShortCircuitCurrentLimit != null, - then: (schema) => - schema.max(yup.ref(HIGH_SHORT_CIRCUIT_CURRENT_LIMIT), 'ShortCircuitCurrentLimitMinMaxError'), - }), - [HIGH_SHORT_CIRCUIT_CURRENT_LIMIT]: yup - .number() - .nullable() - .min(0, 'ShortCircuitCurrentLimitMustBeGreaterOrEqualToZero'), - }) - .concat(modificationPropertiesSchema); const VoltageLevelModificationDialog = ({ editData, // contains data when we try to edit an existing hypothesis from the current node's list defaultIdValue, // Used to pre-select an equipmentId when calling this dialog from the network map @@ -139,25 +66,33 @@ const VoltageLevelModificationDialog = ({ const currentNodeUuid = currentNode?.id; const { snackError } = useSnackMessage(); const [selectedId, setSelectedId] = useState(defaultIdValue ?? null); - const [voltageLevelInfos, setVoltageLevelInfos] = useState(null); + const [voltageLevelToModify, setVoltageLevelToModify] = useState(); const [dataFetchStatus, setDataFetchStatus] = useState(FetchStatus.IDLE); - const formMethods = useFormWithDirtyTracking({ + const emptyFormData = useMemo( + () => ({ + ...voltageLevelModificationEmptyFormData, + [FieldConstants.HIDE_SUBSTATION_FIELD]: false, + }), + [] + ); + + const formMethods = useFormWithDirtyTracking>({ defaultValues: emptyFormData, - resolver: yupResolver(formSchema), + resolver: yupResolver>(voltageLevelModificationFormSchema), }); const { reset, getValues, subscribe, trigger } = formMethods; useEffect(() => { const callback = subscribe({ - name: [`${HIGH_VOLTAGE_LIMIT}`], + name: [FieldConstants.HIGH_VOLTAGE_LIMIT], formState: { values: true, }, callback: ({ isSubmitted }) => { if (isSubmitted) { - trigger(`${LOW_VOLTAGE_LIMIT}`).then(); + trigger(FieldConstants.LOW_VOLTAGE_LIMIT).then(); } }, }); @@ -166,21 +101,10 @@ const VoltageLevelModificationDialog = ({ useEffect(() => { if (editData) { - if (editData?.equipmentId) { + if (editData.equipmentId) { setSelectedId(editData.equipmentId); } - reset({ - [EQUIPMENT_NAME]: editData?.equipmentName?.value ?? '', - [SUBSTATION_ID]: editData?.substationId?.value ?? null, - [NOMINAL_V]: editData?.nominalV?.value ?? null, - [LOW_VOLTAGE_LIMIT]: editData?.lowVoltageLimit?.value ?? null, - [HIGH_VOLTAGE_LIMIT]: editData?.highVoltageLimit?.value ?? null, - [LOW_SHORT_CIRCUIT_CURRENT_LIMIT]: - convertInputValue(FieldType.LOW_SHORT_CIRCUIT_CURRENT_LIMIT, editData?.ipMin?.value) ?? null, - [HIGH_SHORT_CIRCUIT_CURRENT_LIMIT]: - convertInputValue(FieldType.HIGH_SHORT_CIRCUIT_CURRENT_LIMIT, editData?.ipMax?.value) ?? null, - ...getPropertiesFromModification(editData.properties), - }); + reset({ ...voltageLevelModificationDtoToForm(editData), [FieldConstants.HIDE_SUBSTATION_FIELD]: false }); } }, [editData, reset]); @@ -197,7 +121,7 @@ const VoltageLevelModificationDialog = ({ equipmentId, true ) - .then((voltageLevel: VoltageLevelFormData) => { + .then((voltageLevel: VoltageLevelDto) => { if (voltageLevel) { //We convert values of low short circuit current limit and high short circuit current limit from A to KA if (voltageLevel.identifiableShortCircuit) { @@ -210,16 +134,17 @@ const VoltageLevelModificationDialog = ({ voltageLevel.identifiableShortCircuit?.ipMin ); } - setVoltageLevelInfos(voltageLevel); + setVoltageLevelToModify(voltageLevel); setDataFetchStatus(FetchStatus.SUCCEED); reset( (formValues) => ({ ...formValues, + [FieldConstants.EQUIPMENT_ID]: equipmentId, [FieldConstants.ADDITIONAL_PROPERTIES]: getConcatenatedProperties( voltageLevel as EquipmentWithProperties, getValues ), - [SUBSTATION_ID]: voltageLevel?.substationId, + [FieldConstants.SUBSTATION_ID]: voltageLevel.substationId ?? null, }), { keepDirty: true } ); @@ -227,16 +152,19 @@ const VoltageLevelModificationDialog = ({ }) .catch(() => { setDataFetchStatus(FetchStatus.FAILED); + reset((formValues) => ({ ...formValues, [FieldConstants.EQUIPMENT_ID]: equipmentId }), { + keepDirty: true, + }); if (editData?.equipmentId !== equipmentId) { - setVoltageLevelInfos(null); + setVoltageLevelToModify(undefined); } }); } else { - setVoltageLevelInfos(null); + setVoltageLevelToModify(undefined); reset(emptyFormData, { keepDefaultValues: true }); } }, - [studyUuid, currentNodeUuid, currentRootNetworkUuid, reset, getValues, editData] + [studyUuid, currentNodeUuid, currentRootNetworkUuid, reset, getValues, editData?.equipmentId, emptyFormData] ); useEffect(() => { @@ -246,37 +174,22 @@ const VoltageLevelModificationDialog = ({ }, [selectedId, onEquipmentIdChange]); const onSubmit = useCallback( - (voltageLevel: VoltageLevelFormData) => { - if (selectedId != null) { - modifyVoltageLevel({ - studyUuid: studyUuid, - nodeUuid: currentNodeUuid as UUID, - modificationUuid: editData?.uuid, - equipmentId: selectedId, - equipmentName: voltageLevel[EQUIPMENT_NAME], - nominalV: voltageLevel[NOMINAL_V], - lowVoltageLimit: voltageLevel[LOW_VOLTAGE_LIMIT], - highVoltageLimit: voltageLevel[HIGH_VOLTAGE_LIMIT], - lowShortCircuitCurrentLimit: convertOutputValue( - FieldType.LOW_SHORT_CIRCUIT_CURRENT_LIMIT, - voltageLevel[LOW_SHORT_CIRCUIT_CURRENT_LIMIT] - ), - highShortCircuitCurrentLimit: convertOutputValue( - FieldType.HIGH_SHORT_CIRCUIT_CURRENT_LIMIT, - voltageLevel[HIGH_SHORT_CIRCUIT_CURRENT_LIMIT] - ), - properties: toModificationProperties(voltageLevel), - }).catch((error: Error) => { - snackWithFallback(snackError, error, { headerId: 'VoltageLevelModificationError' }); - }); - } + (voltageLevel: VoltageLevelModificationFormData) => { + modifyVoltageLevel({ + studyUuid, + nodeUuid: currentNodeUuid as UUID, + modificationUuid: editData?.uuid, + ...voltageLevelModificationFormToDto(voltageLevel), + }).catch((error: Error) => { + snackWithFallback(snackError, error, { headerId: 'VoltageLevelModificationError' }); + }); }, - [editData, studyUuid, currentNodeUuid, selectedId, snackError] + [editData, studyUuid, currentNodeUuid, snackError] ); const clear = useCallback(() => { reset(emptyFormData); - }, [reset]); + }, [emptyFormData, reset]); const open = useOpenShortWaitFetching({ isDataFetched: @@ -288,7 +201,7 @@ const VoltageLevelModificationDialog = ({ return ( )} - {selectedId != null && ( - - )} + {selectedId != null && } ); diff --git a/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-form.tsx b/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-form.tsx index 8b3a8c1fe8..e69de29bb2 100644 --- a/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-form.tsx +++ b/src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-form.tsx @@ -1,171 +0,0 @@ -/** - * Copyright (c) 2023, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -import { - EQUIPMENT_NAME, - HIGH_SHORT_CIRCUIT_CURRENT_LIMIT, - HIGH_VOLTAGE_LIMIT, - IDENTIFIABLE_SHORT_CIRCUIT, - LOW_SHORT_CIRCUIT_CURRENT_LIMIT, - LOW_VOLTAGE_LIMIT, - NAME, - NOMINAL_V, - SUBSTATION_ID, -} from 'components/utils/field-constants'; -import { - AutocompleteInput, - FieldConstants, - filledTextField, - FloatInput, - getObjectId, - KiloAmpereAdornment, - Properties, - PropertiesForm, - TextInput, - VoltageAdornment, -} from '@gridsuite/commons-ui'; -import { Grid, TextField } from '@mui/material'; -import GridItem from '../../../commons/grid-item'; -import GridSection from '../../../commons/grid-section'; - -interface VoltageLevelFormData { - [NAME]?: string; - [SUBSTATION_ID]?: string; - [NOMINAL_V]?: number; - [LOW_VOLTAGE_LIMIT]?: number; - [HIGH_VOLTAGE_LIMIT]?: number; - [LOW_SHORT_CIRCUIT_CURRENT_LIMIT]?: number; - [HIGH_SHORT_CIRCUIT_CURRENT_LIMIT]?: number; - [IDENTIFIABLE_SHORT_CIRCUIT]?: { ipMin: number; ipMax: number }; - [FieldConstants.ADDITIONAL_PROPERTIES]?: Properties; -} - -interface VoltageLevelModificationFormProps { - voltageLevelInfos: VoltageLevelFormData | null | undefined; - equipmentId: string; -} - -const VoltageLevelModificationForm = ({ voltageLevelInfos, equipmentId }: VoltageLevelModificationFormProps) => { - const voltageLevelIdField = ( - - ); - - const voltageLevelNameField = ( - - ); - - const substationField = ( - (value === null ? '' : value)} - outputTransform={(value) => value} - size={'small'} - formProps={filledTextField} - disabled //TODO to be removed when it is possible to change the substation of a voltage level in the backend (Powsybl) - /> - ); - - const nominalVoltageField = ( - - ); - - const lowVoltageLimitField = ( - - ); - - const highVoltageLimitField = ( - - ); - - const lowShortCircuitCurrentLimitField = ( - - ); - - const highShortCircuitCurrentLimitField = ( - - ); - - return ( - <> - - {voltageLevelIdField} - {voltageLevelNameField} - {substationField} - - - - {nominalVoltageField} - {lowVoltageLimitField} - {highVoltageLimitField} - - - - {lowShortCircuitCurrentLimitField} - {highShortCircuitCurrentLimitField} - - - - ); -}; - -export default VoltageLevelModificationForm; diff --git a/src/components/dialogs/network-modifications/voltage-level/voltage-level.type.ts b/src/components/dialogs/network-modifications/voltage-level/voltage-level.type.ts index bd0b9c0e00..7b71a201a6 100644 --- a/src/components/dialogs/network-modifications/voltage-level/voltage-level.type.ts +++ b/src/components/dialogs/network-modifications/voltage-level/voltage-level.type.ts @@ -5,32 +5,9 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { SwitchKind } from '@gridsuite/commons-ui'; - export interface VoltageLevelFormInfos { equipmentId: string; equipmentName?: string | null; substationId?: string | null; topologyKind?: string | null; } - -export interface IdentifiableShortCircuitDto { - ipMin: number | null; - ipMax: number | null; -} - -export interface VoltageLevelDto { - id: string; - name: string | null; - topologyKind: string | null; - substationId: string | null; - nominalV: number; - lowVoltageLimit: number | null; - highVoltageLimit: number | null; - busbarCount: number | null; - sectionCount: number | null; - switchKinds: SwitchKind[] | null; - isSymmetrical: boolean | null; - identifiableShortCircuit: IdentifiableShortCircuitDto | null; - properties: Record | null; -} diff --git a/src/services/network-modification-types.ts b/src/services/network-modification-types.ts index 9af4472fe2..9a725cb071 100644 --- a/src/services/network-modification-types.ts +++ b/src/services/network-modification-types.ts @@ -330,11 +330,6 @@ export interface VoltageLevelCreationInfo extends VoltageLeveInfo { topologyKind?: string; } -export interface VoltageLeveModificationInfo extends VoltageLeveInfo { - lowShortCircuitCurrentLimit: number | null; - highShortCircuitCurrentLimit: number | null; -} - type VariationFilter = { id: string; name: string; diff --git a/src/services/study/network-modifications.ts b/src/services/study/network-modifications.ts index c4aca43476..2a966d2192 100644 --- a/src/services/study/network-modifications.ts +++ b/src/services/study/network-modifications.ts @@ -20,6 +20,7 @@ import { toModificationOperation, SubstationCreationDto, SubstationModificationDto, + VoltageLevelModificationDto, } from '@gridsuite/commons-ui'; import { getStudyUrlWithNodeUuid, getStudyUrlWithNodeUuidAndRootNetworkUuid } from './index'; import { EQUIPMENT_TYPES } from '../../components/utils/equipment-types'; @@ -56,7 +57,6 @@ import { Variations, VariationType, VoltageLevelCreationInfo, - VoltageLeveModificationInfo, VscCreationInfos, VSCModificationInfo, } from '../network-modification-types'; @@ -1175,16 +1175,13 @@ export function createVoltageLevel({ export function modifyVoltageLevel({ studyUuid, nodeUuid, - modificationUuid = undefined, - equipmentId, - equipmentName, - nominalV, - lowVoltageLimit, - highVoltageLimit, - lowShortCircuitCurrentLimit, - highShortCircuitCurrentLimit, - properties, -}: VoltageLeveModificationInfo) { + modificationUuid, + ...dto +}: { + studyUuid: UUID; + nodeUuid: UUID; + modificationUuid?: UUID; +} & VoltageLevelModificationDto) { let modificationUrl = getNetworkModificationUrl(studyUuid, nodeUuid); const isUpdate = !!modificationUuid; @@ -1201,17 +1198,7 @@ export function modifyVoltageLevel({ Accept: 'application/json', 'Content-Type': 'application/json', }, - body: JSON.stringify({ - type: MODIFICATION_TYPES.VOLTAGE_LEVEL_MODIFICATION.type, - equipmentId, - equipmentName: toModificationOperation(equipmentName), - nominalV: toModificationOperation(nominalV), - lowVoltageLimit: toModificationOperation(lowVoltageLimit), - highVoltageLimit: toModificationOperation(highVoltageLimit), - ipMin: toModificationOperation(lowShortCircuitCurrentLimit), - ipMax: toModificationOperation(highShortCircuitCurrentLimit), - properties, - }), + body: JSON.stringify(dto), }); } diff --git a/src/translations/en.json b/src/translations/en.json index 969be33741..86e202aba1 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -853,7 +853,6 @@ "SubstationModificationError": "Error while modifying substation", "CreateVoltageLevel": "Create a voltage level", - "ModifyVoltageLevel": "Modify a voltage level", "Substation": "Substation", "BusBarSections": "Busbar section", "BusBarSectionsReference": "Busbar reference section", @@ -877,7 +876,6 @@ "DISCONNECTOR": "Disconnector", "SameHorizAndVertPos": "At least two bus bar sections have same horizontal and vertical position", "DuplicateId": "Duplicate id", - "VoltageLevelModificationError": "Error while modifying a voltage level", "AddCouplingDeviceError": "Error while creating a coupling device", "BusBarSectionID1": "Bus bar section / bus 1", "BusBarSectionID2": "Bus bar section / bus 2", diff --git a/src/translations/fr.json b/src/translations/fr.json index eb39fc506c..873f86ed68 100644 --- a/src/translations/fr.json +++ b/src/translations/fr.json @@ -845,7 +845,6 @@ "SubstationModificationError": "Erreur lors de la modification d'un site", "CreateVoltageLevel": "Créer un poste", - "ModifyVoltageLevel": "Modifier un poste", "Substation": "Site", "BusBarSections": "Section de jeu de barres", "BusBarSectionsReference": "Section de jeu de barres de référence", @@ -869,7 +868,6 @@ "DISCONNECTOR": "Sectionneur", "SameHorizAndVertPos": "Au moins deux SJBs ont les mêmes positions horizontale et verticale", "DuplicateId": "Identifiant non unique", - "VoltageLevelModificationError": "Erreur lors de la modification d'un poste", "AddCouplingDeviceError": "Erreur lors de l'ajout d'un omnibus/couplage", "BusBarSectionID1": "SJB / Nœud 1", "BusBarSectionID2": "SJB / Nœud 2",