Skip to content
Merged
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 @@ -285,19 +285,27 @@ export const getInitialTwtRatioRegulationModeId = (twt) => {
return computedRegulationMode?.id || null;
};

export const getComputedRegulationModeId = (twt) => {
return getComputedRegulationMode(twt)?.id || null;
};

export const getComputedPreviousRatioRegulationType = (previousValues) => {
const previousRegulationType = getComputedRegulationType(previousValues);
return previousRegulationType?.id || null;
};

export const getComputedTapSideId = (twt) => {
export const getComputedTapSide = (twt) => {
const ratioTapChangerValues = twt?.ratioTapChanger;
if (!ratioTapChangerValues || !twt) {
return null;
}
if (ratioTapChangerValues?.regulatingTerminalConnectableId === twt?.id) {
return ratioTapChangerValues?.regulatingTerminalVlId === twt?.voltageLevelId1 ? SIDE.SIDE1.id : SIDE.SIDE2.id;
return ratioTapChangerValues?.regulatingTerminalVlId === twt?.voltageLevelId1 ? SIDE.SIDE1 : SIDE.SIDE2;
} else {
return null;
}
};

export const getComputedTapSideId = (twt) => {
return getComputedTapSide(twt)?.id || null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@
import { Grid } from '@mui/material';
import {
ENABLED,
EQUIPMENT,
LOAD_TAP_CHANGING_CAPABILITIES,
RATIO_TAP_CHANGER,
REGULATION_MODE,
REGULATION_SIDE,
REGULATION_TYPE,
TARGET_DEADBAND,
TARGET_V,
VOLTAGE_LEVEL,
} from 'components/utils/field-constants';
import { useEffect, useMemo } from 'react';
import { useCallback, useEffect, useMemo } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
import { useIntl } from 'react-intl';
import { VoltageAdornment } from '../../../../dialog-utils';
import { FloatInput, SelectInput, SwitchInput } from '@gridsuite/commons-ui';
import RatioTapChangerPaneSteps from './ratio-tap-changer-pane-steps';
import { RATIO_REGULATION_MODES } from 'components/network/constants';
import CheckboxNullableInput from 'components/utils/rhf-inputs/boolean-nullable-input';
import { getComputedPreviousRatioRegulationType } from './ratio-tap-changer-pane-utils';
import {
getComputedPreviousRatioRegulationType,
getComputedRegulationModeId,
getComputedRegulationTypeId,
getComputedTapSideId,
} from './ratio-tap-changer-pane-utils';
import GridItem from '../../../../commons/grid-item';
import GridSection from '../../../../commons/grid-section';
import RegulatedTerminalSection from '../regulated-terminal-section';
Expand All @@ -39,7 +46,7 @@ const RatioTapChangerPane = ({
editData,
isModification = false,
}) => {
const { trigger } = useFormContext();
const { trigger, setValue, getValues } = useFormContext();
const intl = useIntl();

const previousRegulation = () => {
Expand Down Expand Up @@ -90,6 +97,81 @@ const RatioTapChangerPane = ({
return regulationTypeWatch || getComputedPreviousRatioRegulationType(previousValues);
}, [previousValues, regulationTypeWatch]);

const findAndSetVoltageLevelFromPrevious = useCallback(
(previousValues, voltageLevelOptions) => {
const prevVl = voltageLevelOptions.find(
(vl) => vl.id === previousValues?.ratioTapChanger?.regulatingTerminalVlId
);
if (prevVl) {
const newVlValue = {
id: prevVl.id,
label: prevVl.name ?? '',
};
setValue(`${id}.${VOLTAGE_LEVEL}`, newVlValue);
} else {
// not supposed to happen, but if it does, we want to log it and keep the form as it is
console.error('Voltage level not found:', prevVl);
}
},
[setValue, id]
);

const setEquipmentFromPrevious = useCallback(
(previousValues) => {
const prevEquipmentId = previousValues?.ratioTapChanger?.regulatingTerminalConnectableId;
if (prevEquipmentId) {
const prevEquipmentType = previousValues?.ratioTapChanger?.regulatingTerminalConnectableType;
const newEquipment = {
id: prevEquipmentId,
label: prevEquipmentType,
type: prevEquipmentType,
};
setValue(`${id}.${EQUIPMENT}`, newEquipment);
}
},
[setValue, id]
);

// we want to fill the empty fields with the previous values when 'on load' is enabled
const fillRatioTapChangerRegulationAttributesWithPreviousValues = useCallback(
(newOnLoad) => {
if (newOnLoad === true) {
const curRatioTapChanger = getValues(id);

if (curRatioTapChanger[REGULATION_MODE] === null) {
setValue(`${id}.${REGULATION_MODE}`, getComputedRegulationModeId(previousValues));
}
if (curRatioTapChanger[TARGET_V] === null) {
setValue(`${id}.${TARGET_V}`, previousValues?.ratioTapChanger?.targetV);
}
if (curRatioTapChanger[TARGET_DEADBAND] === null) {
setValue(`${id}.${TARGET_DEADBAND}`, previousValues?.ratioTapChanger?.targetDeadband);
}
if (curRatioTapChanger[REGULATION_TYPE] === null) {
setValue(`${id}.${REGULATION_TYPE}`, getComputedRegulationTypeId(previousValues));
}
if (curRatioTapChanger[REGULATION_SIDE] === null) {
setValue(`${id}.${REGULATION_SIDE}`, getComputedTapSideId(previousValues));
}
if (curRatioTapChanger[VOLTAGE_LEVEL] === null) {
findAndSetVoltageLevelFromPrevious(previousValues, voltageLevelOptions);
}
if (curRatioTapChanger[EQUIPMENT] === null) {
setEquipmentFromPrevious(previousValues);
}
}
},
[
id,
voltageLevelOptions,
previousValues,
setValue,
getValues,
findAndSetVoltageLevelFromPrevious,
setEquipmentFromPrevious,
]
);

// we want to update the validation of these fields when they become optionals to remove the red alert
useEffect(() => {
if (regulationModeWatch === RATIO_REGULATION_MODES.FIXED_RATIO.id) {
Expand All @@ -107,6 +189,7 @@ const RatioTapChangerPane = ({
disabled: !ratioTapChangerEnabledWatcher,
}}
previousValue={previousRegulation()}
onChange={fillRatioTapChangerRegulationAttributesWithPreviousValues}
/>
) : (
<SwitchInput
Expand Down
15 changes: 10 additions & 5 deletions src/components/utils/rhf-inputs/boolean-nullable-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface CheckboxNullableInputProps {
formProps?: CheckboxProps;
previousValue?: string;
nullDisabled?: boolean;
onChange?: (value: boolean | null) => void;
style?: { color: string };
}

Expand All @@ -29,24 +30,28 @@ const CheckboxNullableInput = ({
formProps,
previousValue,
nullDisabled,
onChange,
style,
}: Readonly<CheckboxNullableInputProps>) => {
const {
field: { onChange, value },
field: { onChange: rhfOnChange, value },
} = useController({ name });

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

const handleChangeValue = useCallback(() => {
let newValue;
if (value) {
onChange(null);
newValue = null;
} else if (value === null) {
onChange(false);
newValue = false;
} else {
onChange(true);
newValue = true;
}
}, [onChange, value]);
rhfOnChange(newValue);
onChange?.(newValue);
}, [rhfOnChange, onChange, value]);

// Get the current label based on value
const currentLabel = typeof label === 'function' ? label(value) : label;
Expand Down
Loading