Skip to content

Commit 0a0307d

Browse files
authored
Add callback to populate unset ratio tap changer regulation attributes with previous values (#3282)
When editing an existing TWT and when the user enable onload, we autofill the regulation attributes with the current network values Signed-off-by: Florent MILLOT <[email protected]>
1 parent e2507aa commit 0a0307d

File tree

3 files changed

+106
-10
lines changed

3 files changed

+106
-10
lines changed

src/components/dialogs/network-modifications/two-windings-transformer/tap-changer-pane/ratio-tap-changer-pane/ratio-tap-changer-pane-utils.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,27 @@ export const getInitialTwtRatioRegulationModeId = (twt) => {
285285
return computedRegulationMode?.id || null;
286286
};
287287

288+
export const getComputedRegulationModeId = (twt) => {
289+
return getComputedRegulationMode(twt)?.id || null;
290+
};
291+
288292
export const getComputedPreviousRatioRegulationType = (previousValues) => {
289293
const previousRegulationType = getComputedRegulationType(previousValues);
290294
return previousRegulationType?.id || null;
291295
};
292296

293-
export const getComputedTapSideId = (twt) => {
297+
export const getComputedTapSide = (twt) => {
294298
const ratioTapChangerValues = twt?.ratioTapChanger;
295299
if (!ratioTapChangerValues || !twt) {
296300
return null;
297301
}
298302
if (ratioTapChangerValues?.regulatingTerminalConnectableId === twt?.id) {
299-
return ratioTapChangerValues?.regulatingTerminalVlId === twt?.voltageLevelId1 ? SIDE.SIDE1.id : SIDE.SIDE2.id;
303+
return ratioTapChangerValues?.regulatingTerminalVlId === twt?.voltageLevelId1 ? SIDE.SIDE1 : SIDE.SIDE2;
300304
} else {
301305
return null;
302306
}
303307
};
308+
309+
export const getComputedTapSideId = (twt) => {
310+
return getComputedTapSide(twt)?.id || null;
311+
};

src/components/dialogs/network-modifications/two-windings-transformer/tap-changer-pane/ratio-tap-changer-pane/ratio-tap-changer-pane.jsx

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@
88
import { Grid } from '@mui/material';
99
import {
1010
ENABLED,
11+
EQUIPMENT,
1112
LOAD_TAP_CHANGING_CAPABILITIES,
1213
RATIO_TAP_CHANGER,
1314
REGULATION_MODE,
1415
REGULATION_SIDE,
1516
REGULATION_TYPE,
1617
TARGET_DEADBAND,
1718
TARGET_V,
19+
VOLTAGE_LEVEL,
1820
} from 'components/utils/field-constants';
19-
import { useEffect, useMemo } from 'react';
21+
import { useCallback, useEffect, useMemo } from 'react';
2022
import { useFormContext, useWatch } from 'react-hook-form';
2123
import { useIntl } from 'react-intl';
2224
import { VoltageAdornment } from '../../../../dialog-utils';
2325
import { FloatInput, SelectInput, SwitchInput } from '@gridsuite/commons-ui';
2426
import RatioTapChangerPaneSteps from './ratio-tap-changer-pane-steps';
2527
import { RATIO_REGULATION_MODES } from 'components/network/constants';
2628
import CheckboxNullableInput from 'components/utils/rhf-inputs/boolean-nullable-input';
27-
import { getComputedPreviousRatioRegulationType } from './ratio-tap-changer-pane-utils';
29+
import {
30+
getComputedPreviousRatioRegulationType,
31+
getComputedRegulationModeId,
32+
getComputedRegulationTypeId,
33+
getComputedTapSideId,
34+
} from './ratio-tap-changer-pane-utils';
2835
import GridItem from '../../../../commons/grid-item';
2936
import GridSection from '../../../../commons/grid-section';
3037
import RegulatedTerminalSection from '../regulated-terminal-section';
@@ -39,7 +46,7 @@ const RatioTapChangerPane = ({
3946
editData,
4047
isModification = false,
4148
}) => {
42-
const { trigger } = useFormContext();
49+
const { trigger, setValue, getValues } = useFormContext();
4350
const intl = useIntl();
4451

4552
const previousRegulation = () => {
@@ -90,6 +97,81 @@ const RatioTapChangerPane = ({
9097
return regulationTypeWatch || getComputedPreviousRatioRegulationType(previousValues);
9198
}, [previousValues, regulationTypeWatch]);
9299

100+
const findAndSetVoltageLevelFromPrevious = useCallback(
101+
(previousValues, voltageLevelOptions) => {
102+
const prevVl = voltageLevelOptions.find(
103+
(vl) => vl.id === previousValues?.ratioTapChanger?.regulatingTerminalVlId
104+
);
105+
if (prevVl) {
106+
const newVlValue = {
107+
id: prevVl.id,
108+
label: prevVl.name ?? '',
109+
};
110+
setValue(`${id}.${VOLTAGE_LEVEL}`, newVlValue);
111+
} else {
112+
// not supposed to happen, but if it does, we want to log it and keep the form as it is
113+
console.error('Voltage level not found:', prevVl);
114+
}
115+
},
116+
[setValue, id]
117+
);
118+
119+
const setEquipmentFromPrevious = useCallback(
120+
(previousValues) => {
121+
const prevEquipmentId = previousValues?.ratioTapChanger?.regulatingTerminalConnectableId;
122+
if (prevEquipmentId) {
123+
const prevEquipmentType = previousValues?.ratioTapChanger?.regulatingTerminalConnectableType;
124+
const newEquipment = {
125+
id: prevEquipmentId,
126+
label: prevEquipmentType,
127+
type: prevEquipmentType,
128+
};
129+
setValue(`${id}.${EQUIPMENT}`, newEquipment);
130+
}
131+
},
132+
[setValue, id]
133+
);
134+
135+
// we want to fill the empty fields with the previous values when 'on load' is enabled
136+
const fillRatioTapChangerRegulationAttributesWithPreviousValues = useCallback(
137+
(newOnLoad) => {
138+
if (newOnLoad === true) {
139+
const curRatioTapChanger = getValues(id);
140+
141+
if (curRatioTapChanger[REGULATION_MODE] === null) {
142+
setValue(`${id}.${REGULATION_MODE}`, getComputedRegulationModeId(previousValues));
143+
}
144+
if (curRatioTapChanger[TARGET_V] === null) {
145+
setValue(`${id}.${TARGET_V}`, previousValues?.ratioTapChanger?.targetV);
146+
}
147+
if (curRatioTapChanger[TARGET_DEADBAND] === null) {
148+
setValue(`${id}.${TARGET_DEADBAND}`, previousValues?.ratioTapChanger?.targetDeadband);
149+
}
150+
if (curRatioTapChanger[REGULATION_TYPE] === null) {
151+
setValue(`${id}.${REGULATION_TYPE}`, getComputedRegulationTypeId(previousValues));
152+
}
153+
if (curRatioTapChanger[REGULATION_SIDE] === null) {
154+
setValue(`${id}.${REGULATION_SIDE}`, getComputedTapSideId(previousValues));
155+
}
156+
if (curRatioTapChanger[VOLTAGE_LEVEL] === null) {
157+
findAndSetVoltageLevelFromPrevious(previousValues, voltageLevelOptions);
158+
}
159+
if (curRatioTapChanger[EQUIPMENT] === null) {
160+
setEquipmentFromPrevious(previousValues);
161+
}
162+
}
163+
},
164+
[
165+
id,
166+
voltageLevelOptions,
167+
previousValues,
168+
setValue,
169+
getValues,
170+
findAndSetVoltageLevelFromPrevious,
171+
setEquipmentFromPrevious,
172+
]
173+
);
174+
93175
// we want to update the validation of these fields when they become optionals to remove the red alert
94176
useEffect(() => {
95177
if (regulationModeWatch === RATIO_REGULATION_MODES.FIXED_RATIO.id) {
@@ -107,6 +189,7 @@ const RatioTapChangerPane = ({
107189
disabled: !ratioTapChangerEnabledWatcher,
108190
}}
109191
previousValue={previousRegulation()}
192+
onChange={fillRatioTapChangerRegulationAttributesWithPreviousValues}
110193
/>
111194
) : (
112195
<SwitchInput

src/components/utils/rhf-inputs/boolean-nullable-input.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface CheckboxNullableInputProps {
1919
formProps?: CheckboxProps;
2020
previousValue?: string;
2121
nullDisabled?: boolean;
22+
onChange?: (value: boolean | null) => void;
2223
style?: { color: string };
2324
}
2425

@@ -29,24 +30,28 @@ const CheckboxNullableInput = ({
2930
formProps,
3031
previousValue,
3132
nullDisabled,
33+
onChange,
3234
style,
3335
}: Readonly<CheckboxNullableInputProps>) => {
3436
const {
35-
field: { onChange, value },
37+
field: { onChange: rhfOnChange, value },
3638
} = useController({ name });
3739

3840
const intl = useIntl();
3941
const { isNodeBuilt, isUpdate } = useCustomFormContext();
4042

4143
const handleChangeValue = useCallback(() => {
44+
let newValue;
4245
if (value) {
43-
onChange(null);
46+
newValue = null;
4447
} else if (value === null) {
45-
onChange(false);
48+
newValue = false;
4649
} else {
47-
onChange(true);
50+
newValue = true;
4851
}
49-
}, [onChange, value]);
52+
rhfOnChange(newValue);
53+
onChange?.(newValue);
54+
}, [rhfOnChange, onChange, value]);
5055

5156
// Get the current label based on value
5257
const currentLabel = typeof label === 'function' ? label(value) : label;

0 commit comments

Comments
 (0)