diff --git a/src/components/dialogs/connectivity/connectivity-form.jsx b/src/components/dialogs/connectivity/connectivity-form.jsx index 3623bd0fbe..0a89593b43 100644 --- a/src/components/dialogs/connectivity/connectivity-form.jsx +++ b/src/components/dialogs/connectivity/connectivity-form.jsx @@ -7,7 +7,7 @@ import ExploreOffOutlinedIcon from '@mui/icons-material/ExploreOffOutlined'; import ExploreOutlinedIcon from '@mui/icons-material/ExploreOutlined'; -import { IconButton, Tooltip } from '@mui/material'; +import { IconButton, TextField, Tooltip } from '@mui/material'; import Grid from '@mui/material/Grid'; import { BUS_OR_BUSBAR_SECTION, @@ -20,7 +20,7 @@ import { VOLTAGE_LEVEL, } from 'components/utils/field-constants'; import { areIdsEqual, getObjectId } from 'components/utils/utils'; -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import { useFormContext, useWatch } from 'react-hook-form'; import { useIntl } from 'react-intl'; import PositionDiagramPane from '../../diagrams/singleLineDiagram/position-diagram-pane'; @@ -42,6 +42,7 @@ import { fetchBusesForVoltageLevel, } from '../../../services/study/network'; import CheckboxNullableInput from '../../utils/rhf-inputs/boolean-nullable-input.jsx'; +import { filledTextField } from '../dialogUtils.jsx'; /** * Hook to handle a 'connectivity value' (voltage level, bus or bus bar section) @@ -55,8 +56,8 @@ import CheckboxNullableInput from '../../utils/rhf-inputs/boolean-nullable-input * @param studyUuid the study we are currently working on * @param currentNode the currently selected tree node * @param onVoltageLevelChangeCallback callback to be called when the voltage level changes - * @param isEquipmentModification - * @param previousValues + * @param isEquipmentModification connectivity form is used in a modification form or not + * @param previousValues previous values of connectivity form's fields * @returns JSX.Element */ export const ConnectivityForm = ({ @@ -179,22 +180,27 @@ export const ConnectivityForm = ({ /> ); - const previousConnected = useMemo(() => { - if (previousValues?.terminalConnected) { - return intl.formatMessage({ id: 'On' }); - } else if ( - previousValues?.terminalConnected === false || - (previousValues && previousValues?.terminalConnected === undefined) - ) { - return intl.formatMessage({ id: 'Off' }); - } - }, [intl, previousValues]); + const readyOnlyVoltageLevelField = ( + + ); const connectedField = isEquipmentModification ? ( ) : ( @@ -228,6 +234,18 @@ export const ConnectivityForm = ({ /> ); + const readyOnlyBusOrBusbarSectionField = ( + + ); + const newConnectionNameField = ( - {newVoltageLevelField} + {!isEquipmentModification + ? newVoltageLevelField + : readyOnlyVoltageLevelField} - {newBusOrBusbarSectionField} + {!isEquipmentModification + ? newBusOrBusbarSectionField + : readyOnlyBusOrBusbarSectionField} {withDirectionsInfos && ( diff --git a/src/components/dialogs/network-modifications/battery/creation/battery-creation-form.jsx b/src/components/dialogs/network-modifications/battery/creation/battery-creation-form.jsx index f05cee719c..dc615b3805 100644 --- a/src/components/dialogs/network-modifications/battery/creation/battery-creation-form.jsx +++ b/src/components/dialogs/network-modifications/battery/creation/battery-creation-form.jsx @@ -5,13 +5,13 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { TextInput } from '@gridsuite/commons-ui'; +import { FloatInput, TextInput } from '@gridsuite/commons-ui'; import { + ACTIVE_POWER_SET_POINT, EQUIPMENT_ID, EQUIPMENT_NAME, MAXIMUM_ACTIVE_POWER, MINIMUM_ACTIVE_POWER, - ACTIVE_POWER_SET_POINT, REACTIVE_POWER_SET_POINT, } from 'components/utils/field-constants'; import { @@ -22,28 +22,18 @@ import { ReactivePowerAdornment, } from '../../../dialogUtils'; import Grid from '@mui/material/Grid'; -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { ConnectivityForm } from '../../../connectivity/connectivity-form'; -import { FloatInput } from '@gridsuite/commons-ui'; import ReactiveLimitsForm from '../../../reactive-limits/reactive-limits-form'; import FrequencyRegulation from '../../../set-points/frequency-regulation'; -import { fetchVoltageLevelsListInfos } from '../../../../../services/study/network'; import PropertiesForm from '../../common/properties/properties-form'; -const BatteryCreationForm = ({ studyUuid, currentNode }) => { - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - const currentNodeUuid = currentNode?.id; +import useVoltageLevelsListInfos from '../../../../../hooks/use-voltage-levels-list-infos'; - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); +const BatteryCreationForm = ({ studyUuid, currentNode }) => { + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNode.id + ); const batteryIdField = ( { - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - const currentNodeUuid = currentNode?.id; + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNode?.id + ); - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); const batteryIdField = ( { - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); const currentNodeUuid = currentNode?.id; - - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNodeUuid + ); const generatorIdField = ( { - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); const currentNodeUuid = currentNode?.id; const intl = useIntl(); - - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNodeUuid + ); const energySourceLabelId = getEnergySourceLabel( generatorToModify?.energySource diff --git a/src/components/dialogs/network-modifications/line/characteristics-pane/line-characteristics-pane.jsx b/src/components/dialogs/network-modifications/line/characteristics-pane/line-characteristics-pane.jsx index e9b77a2503..0b657838ad 100644 --- a/src/components/dialogs/network-modifications/line/characteristics-pane/line-characteristics-pane.jsx +++ b/src/components/dialogs/network-modifications/line/characteristics-pane/line-characteristics-pane.jsx @@ -15,20 +15,20 @@ import { import { FloatInput } from '@gridsuite/commons-ui'; import { ConnectivityForm } from '../../../connectivity/connectivity-form'; import { + B1, + B2, CHARACTERISTICS, CONNECTIVITY_1, CONNECTIVITY_2, - R, G1, G2, - B1, - B2, + R, X, } from 'components/utils/field-constants'; -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { unitToMicroUnit } from 'utils/unit-converter'; -import { fetchVoltageLevelsListInfos } from '../../../../../services/study/network'; import PropertiesForm from '../../common/properties/properties-form'; +import useVoltageLevelsListInfos from '../../../../../hooks/use-voltage-levels-list-infos'; const styles = { h3: { @@ -47,18 +47,10 @@ const LineCharacteristicsPane = ({ isModification = false, }) => { const currentNodeUuid = currentNode.id; - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNodeUuid + ); const seriesResistanceField = ( { const currentNodeUuid = currentNode?.id; - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); const [linesIds, setLinesIds] = useState([]); - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNodeUuid + ); useEffect(() => { fetchEquipmentsIds( diff --git a/src/components/dialogs/network-modifications/load/creation/load-creation-form.jsx b/src/components/dialogs/network-modifications/load/creation/load-creation-form.jsx index c6e74cd167..f3af71139b 100644 --- a/src/components/dialogs/network-modifications/load/creation/load-creation-form.jsx +++ b/src/components/dialogs/network-modifications/load/creation/load-creation-form.jsx @@ -13,7 +13,7 @@ import { P0, Q0, } from 'components/utils/field-constants'; -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { ActivePowerAdornment, filledTextField, @@ -22,29 +22,16 @@ import { ReactivePowerAdornment, } from '../../../dialogUtils'; import { LOAD_TYPES } from 'components/network/constants'; -import { FloatInput } from '@gridsuite/commons-ui'; -import { SelectInput } from '@gridsuite/commons-ui'; -import { TextInput } from '@gridsuite/commons-ui'; +import { FloatInput, SelectInput, TextInput } from '@gridsuite/commons-ui'; import { ConnectivityForm } from '../../../connectivity/connectivity-form'; - -import { fetchVoltageLevelsListInfos } from '../../../../../services/study/network'; import PropertiesForm from '../../common/properties/properties-form'; +import useVoltageLevelsListInfos from '../../../../../hooks/use-voltage-levels-list-infos'; const LoadCreationForm = ({ currentNode, studyUuid }) => { - const currentNodeUuid = currentNode?.id; - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNode?.id + ); const loadIdField = ( { - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - const currentNodeUuid = currentNode?.id; const intl = useIntl(); - - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNode?.id + ); const loadIdField = ( { - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - const currentNodeUuid = currentNode?.id; - - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNode?.id + ); const shuntCompensatorIdField = ( { - const currentNodeUuid = currentNode.id; - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNode?.id + ); const connectivity1Field = ( { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a, b) => a.id.localeCompare(b.id)) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); - useEffect(() => { if (editData) { fromEditDataToFormValues( diff --git a/src/components/dialogs/network-modifications/vsc/converter-station/converter-station-pane.tsx b/src/components/dialogs/network-modifications/vsc/converter-station/converter-station-pane.tsx index 3fee36fdcd..956672c2e4 100644 --- a/src/components/dialogs/network-modifications/vsc/converter-station/converter-station-pane.tsx +++ b/src/components/dialogs/network-modifications/vsc/converter-station/converter-station-pane.tsx @@ -5,7 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import React, { FunctionComponent, useEffect, useState } from 'react'; +import React, { FunctionComponent, useEffect } from 'react'; import { FloatInput, SwitchInput, TextInput } from '@gridsuite/commons-ui'; import { CONNECTIVITY, @@ -24,7 +24,6 @@ import { ReactivePowerAdornment, VoltageAdornment, } from '../../../dialogUtils'; -import { fetchVoltageLevelsListInfos } from '../../../../../services/study/network'; import { CurrentTreeNode } from '../../../../../redux/reducer.type'; import { UUID } from 'crypto'; import { ConnectivityForm } from '../../../connectivity/connectivity-form'; @@ -37,6 +36,7 @@ import { UpdateReactiveCapabilityCurveTable, } from './converter-station-utils'; import CheckboxNullableInput from '../../../../utils/rhf-inputs/boolean-nullable-input'; +import useVoltageLevelsListInfos from '../../../../../hooks/use-voltage-levels-list-infos'; interface VscConverterStationPaneProps { id: string; @@ -57,9 +57,6 @@ const ConverterStationPane: FunctionComponent = ({ previousValues, updatePreviousReactiveCapabilityCurveTableConverterStation, }) => { - const [voltageLevelOptions, setVoltageLevelOptions] = useState([]); - const currentNodeUuid = currentNode?.id; - const { trigger } = useFormContext(); const voltageRegulationOnWatch = useWatch({ @@ -72,19 +69,10 @@ const ConverterStationPane: FunctionComponent = ({ } }); - useEffect(() => { - if (studyUuid && currentNodeUuid) { - fetchVoltageLevelsListInfos(studyUuid, currentNodeUuid).then( - (values) => { - setVoltageLevelOptions( - values.sort((a: { id: string }, b: { id: string }) => - a.id.localeCompare(b.id) - ) - ); - } - ); - } - }, [studyUuid, currentNodeUuid]); + const voltageLevelOptions = useVoltageLevelsListInfos( + studyUuid, + currentNode?.id + ); const generatorIdField = isModification ? ( { + if (studyUuid && nodeUuid) { + fetchVoltageLevelsListInfos(studyUuid, nodeUuid).then((values) => { + setVoltageLevelsListInfos( + values.sort((a: { id: string }, b: { id: string }) => + a.id.localeCompare(b.id) + ) + ); + }); + } + }, [studyUuid, nodeUuid]); + return voltageLevelsListInfos; +}