Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,27 @@

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 {
convertInputValue,
convertOutputValue,
CustomFormProvider,
emptyProperties,
DeepNullable,
EquipmentType,
EquipmentWithProperties,
FieldConstants,
FieldType,
getConcatenatedProperties,
getPropertiesFromModification,
modificationPropertiesSchema,
snackWithFallback,
toModificationProperties,
useSnackMessage,
VoltageLevelDto,

Check failure on line 21 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'VoltageLevelDto'. Did you mean 'VoltageLevel'?
VoltageLevelModificationDto,

Check failure on line 22 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'VoltageLevelModificationDto'. Did you mean 'VoltageLevelCreationDto'?
VoltageLevelModificationForm,

Check failure on line 23 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'VoltageLevelModificationForm'. Did you mean 'VoltageLevelCreationForm'?
VoltageLevelModificationFormData,

Check failure on line 24 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'VoltageLevelModificationFormData'. Did you mean 'VoltageLevelCreationFormData'?
voltageLevelModificationDtoToForm,

Check failure on line 25 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'voltageLevelModificationDtoToForm'. Did you mean 'voltageLevelCreationDtoToForm'?
voltageLevelModificationEmptyFormData,

Check failure on line 26 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'voltageLevelModificationEmptyFormData'. Did you mean 'voltageLevelCreationEmptyFormData'?
voltageLevelModificationFormSchema,

Check failure on line 27 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'voltageLevelModificationFormSchema'. Did you mean 'voltageLevelCreationFormSchema'?
voltageLevelModificationFormToDto,

Check failure on line 28 in src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

'"@gridsuite/commons-ui"' has no exported member named 'voltageLevelModificationFormToDto'. Did you mean 'voltageLevelCreationFormToDto'?
} 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';
Expand All @@ -49,21 +42,8 @@

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;
Expand All @@ -73,59 +53,6 @@
[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
Expand All @@ -139,25 +66,25 @@
const currentNodeUuid = currentNode?.id;
const { snackError } = useSnackMessage();
const [selectedId, setSelectedId] = useState<string | null>(defaultIdValue ?? null);
const [voltageLevelInfos, setVoltageLevelInfos] = useState<VoltageLevelFormData | null>(null);
const [voltageLevelToModify, setVoltageLevelToModify] = useState<VoltageLevelDto | undefined>();
const [dataFetchStatus, setDataFetchStatus] = useState(FetchStatus.IDLE);

const formMethods = useFormWithDirtyTracking({
defaultValues: emptyFormData,
resolver: yupResolver(formSchema),
const formMethods = useFormWithDirtyTracking<DeepNullable<VoltageLevelModificationFormData>>({
defaultValues: voltageLevelModificationEmptyFormData,
resolver: yupResolver<DeepNullable<VoltageLevelModificationFormData>>(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();
}
},
});
Expand All @@ -166,21 +93,10 @@

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));
}
}, [editData, reset]);

Expand All @@ -197,7 +113,7 @@
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) {
Expand All @@ -210,30 +126,34 @@
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 }
);
}
})
.catch(() => {
setDataFetchStatus(FetchStatus.FAILED);
reset((formValues) => ({ ...formValues, [FieldConstants.EQUIPMENT_ID]: equipmentId }), {
keepDirty: true,
});
if (editData?.equipmentId !== equipmentId) {
setVoltageLevelInfos(null);
setVoltageLevelToModify(undefined);
}
});
} else {
setVoltageLevelInfos(null);
reset(emptyFormData, { keepDefaultValues: true });
setVoltageLevelToModify(undefined);
reset(voltageLevelModificationEmptyFormData, { keepDefaultValues: true });
Comment on lines 145 to +156
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Restore the equipment selector after a fetch failure.

If a newly selected or default voltage level fails to load, selectedId stays set while voltageLevelToModify is cleared. Because the selector is only shown when selectedId == null, the dialog gets stuck on an empty form and the user cannot pick another equipment without closing it.

↩️ One simple recovery path
                 .catch(() => {
                     setDataFetchStatus(FetchStatus.FAILED);
                     reset((formValues) => ({ ...formValues, [FieldConstants.EQUIPMENT_ID]: equipmentId }), {
                         keepDirty: true,
                     });
                     if (editData?.equipmentId !== equipmentId) {
                         setVoltageLevelToModify(undefined);
+                        setSelectedId(null);
                     }
                 });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@src/components/dialogs/network-modifications/voltage-level/modification/voltage-level-modification-dialog.tsx`
around lines 145 - 156, When the fetch fails we clear voltageLevelToModify but
leave the selector hidden because selectedId remains set; update the catch block
to also restore the equipment selector by calling setSelectedId(null) (or
setSelectedId(editData?.equipmentId) if you want to fall back to the previous
selection) after setDataFetchStatus(FetchStatus.FAILED) and reset(...). Modify
the catch branch that calls setVoltageLevelToModify(undefined) to also invoke
setSelectedId(...) so the selector becomes visible and the user can pick another
equipment.

}
},
[studyUuid, currentNodeUuid, currentRootNetworkUuid, reset, getValues, editData]
Expand All @@ -246,36 +166,21 @@
}, [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(voltageLevelModificationEmptyFormData);
}, [reset]);

const open = useOpenShortWaitFetching({
Expand All @@ -288,7 +193,7 @@

return (
<CustomFormProvider
validationSchema={formSchema}
validationSchema={voltageLevelModificationFormSchema}
removeOptional={true}
{...formMethods}
isNodeBuilt={isNodeBuilt(currentNode)}
Expand All @@ -315,9 +220,7 @@
fillerHeight={4}
/>
)}
{selectedId != null && (
<VoltageLevelModificationForm voltageLevelInfos={voltageLevelInfos} equipmentId={selectedId} />
)}
{selectedId != null && <VoltageLevelModificationForm voltageLevelToModify={voltageLevelToModify} />}
</ModificationDialog>
</CustomFormProvider>
);
Expand Down
Loading
Loading