8
8
import { Grid } from '@mui/material' ;
9
9
import {
10
10
ENABLED ,
11
+ EQUIPMENT ,
11
12
LOAD_TAP_CHANGING_CAPABILITIES ,
12
13
RATIO_TAP_CHANGER ,
13
14
REGULATION_MODE ,
14
15
REGULATION_SIDE ,
15
16
REGULATION_TYPE ,
16
17
TARGET_DEADBAND ,
17
18
TARGET_V ,
19
+ VOLTAGE_LEVEL ,
18
20
} from 'components/utils/field-constants' ;
19
- import { useEffect , useMemo } from 'react' ;
21
+ import { useCallback , useEffect , useMemo } from 'react' ;
20
22
import { useFormContext , useWatch } from 'react-hook-form' ;
21
23
import { useIntl } from 'react-intl' ;
22
24
import { VoltageAdornment } from '../../../../dialog-utils' ;
23
25
import { FloatInput , SelectInput , SwitchInput } from '@gridsuite/commons-ui' ;
24
26
import RatioTapChangerPaneSteps from './ratio-tap-changer-pane-steps' ;
25
27
import { RATIO_REGULATION_MODES } from 'components/network/constants' ;
26
28
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' ;
28
35
import GridItem from '../../../../commons/grid-item' ;
29
36
import GridSection from '../../../../commons/grid-section' ;
30
37
import RegulatedTerminalSection from '../regulated-terminal-section' ;
@@ -39,7 +46,7 @@ const RatioTapChangerPane = ({
39
46
editData,
40
47
isModification = false ,
41
48
} ) => {
42
- const { trigger } = useFormContext ( ) ;
49
+ const { trigger, setValue , getValues } = useFormContext ( ) ;
43
50
const intl = useIntl ( ) ;
44
51
45
52
const previousRegulation = ( ) => {
@@ -90,6 +97,81 @@ const RatioTapChangerPane = ({
90
97
return regulationTypeWatch || getComputedPreviousRatioRegulationType ( previousValues ) ;
91
98
} , [ previousValues , regulationTypeWatch ] ) ;
92
99
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
+
93
175
// we want to update the validation of these fields when they become optionals to remove the red alert
94
176
useEffect ( ( ) => {
95
177
if ( regulationModeWatch === RATIO_REGULATION_MODES . FIXED_RATIO . id ) {
@@ -107,6 +189,7 @@ const RatioTapChangerPane = ({
107
189
disabled : ! ratioTapChangerEnabledWatcher ,
108
190
} }
109
191
previousValue = { previousRegulation ( ) }
192
+ onChange = { fillRatioTapChangerRegulationAttributesWithPreviousValues }
110
193
/>
111
194
) : (
112
195
< SwitchInput
0 commit comments