66 */
77
88import { yupResolver } from '@hookform/resolvers/yup' ;
9- import yup from 'components/utils/yup-config' ;
10- import { DELETION_SPECIFIC_DATA , EQUIPMENT_ID , TYPE } from '../../../utils/field-constants' ;
119import {
1210 CustomFormProvider ,
1311 DeepNullable ,
12+ equipmentDeletionEmptyFormData ,
13+ EquipmentDeletionDto ,
1414 EquipmentType ,
1515 snackWithFallback ,
1616 useSnackMessage ,
17+ equipmentDeletionFormSchema ,
18+ EquipmentDeletionFormData ,
19+ EquipmentDeletionForm ,
20+ equipmentDeletionDtoToForm ,
21+ equipmentDeletionFormToDto ,
1722} from '@gridsuite/commons-ui' ;
1823import { useForm } from 'react-hook-form' ;
1924import { useCallback , useEffect , useMemo } from 'react' ;
2025import { ModificationDialog } from '../../commons/modificationDialog' ;
21- import DeleteEquipmentForm from './equipment-deletion-form' ;
2226import { useOpenShortWaitFetching } from 'components/dialogs/commons/handle-modification-form' ;
2327import { FORM_LOADING_DELAY } from 'components/network/constants' ;
2428import { deleteEquipment } from '../../../../services/study/network-modifications' ;
2529import { UUID } from 'node:crypto' ;
2630import { FetchStatus } from 'services/utils.type' ;
27- import { EquipmentDeletionInfos } from './equipement-deletion-dialog.type' ;
2831import { NetworkModificationDialogProps } from '../../../graph/menus/network-modifications/network-modification-menu.type' ;
29- import { getHvdcLccDeletionSchema } from './hvdc-lcc-deletion/hvdc-lcc-deletion-utils' ;
30-
31- const formSchema = yup
32- . object ( )
33- . shape ( {
34- [ EQUIPMENT_ID ] : yup . string ( ) . nullable ( ) . required ( ) ,
35- [ TYPE ] : yup . mixed < EquipmentType > ( ) . oneOf ( Object . values ( EquipmentType ) ) . nullable ( ) . required ( ) ,
36- [ DELETION_SPECIFIC_DATA ] : getHvdcLccDeletionSchema ( ) ,
37- } )
38- . required ( ) ;
39-
40- export type EquipmentDeletionFormInfos = yup . InferType < typeof formSchema > ;
41-
42- const emptyFormData : DeepNullable < EquipmentDeletionFormInfos > = {
43- [ EQUIPMENT_ID ] : '' ,
44- [ TYPE ] : null ,
45- [ DELETION_SPECIFIC_DATA ] : null ,
46- } ;
32+ import { WithModificationId } from 'services/network-modification-types' ;
33+ import { fetchEquipmentsIds , fetchHvdcLineWithShuntCompensators } from 'services/study/network-map' ;
34+
35+ export interface EquipmentDeletionDtoWithId extends EquipmentDeletionDto , WithModificationId { }
4736
4837type EquipmentDeletionDialogProps = NetworkModificationDialogProps & {
49- editData ?: EquipmentDeletionInfos ;
38+ editData ?: EquipmentDeletionDtoWithId ;
5039 defaultIdValue ?: UUID ;
51- equipmentType : EquipmentType ;
52- editDataFetchStatus ?: FetchStatus ;
40+ equipmentType ?: EquipmentType ;
5341} ;
5442
5543/**
@@ -59,8 +47,8 @@ type EquipmentDeletionDialogProps = NetworkModificationDialogProps & {
5947 * @param currentRootNetworkUuid
6048 * @param editData the data to edit
6149 * @param isUpdate check if edition form
62- * @param defaultIdValue the default equipment id
63- * @param equipmentType
50+ * @param defaultIdValue in creation mode, we can specify the equipment ID we want to delete
51+ * @param equipmentType in creation mode, we can specify the equipment type we want to delete
6452 * @param editDataFetchStatus indicates the status of fetching EditData
6553 * @param onClose a callback when dialog has been closed
6654 * @param onValidated a callback when dialog has been validated
@@ -83,79 +71,69 @@ const EquipmentDeletionDialog = ({
8371
8472 const { snackError } = useSnackMessage ( ) ;
8573
86- const formMethods = useForm < DeepNullable < EquipmentDeletionFormInfos > > ( {
87- defaultValues : emptyFormData ,
88- resolver : yupResolver < DeepNullable < EquipmentDeletionFormInfos > > ( formSchema ) ,
74+ const formMethods = useForm < DeepNullable < EquipmentDeletionFormData > > ( {
75+ defaultValues : equipmentDeletionEmptyFormData ,
76+ resolver : yupResolver < DeepNullable < EquipmentDeletionFormData > > ( equipmentDeletionFormSchema ) ,
8977 } ) ;
9078
9179 const { reset } = formMethods ;
9280
9381 const fromEditDataToFormValues = useCallback (
94- ( editData : EquipmentDeletionInfos ) => {
82+ ( editData : EquipmentDeletionDto ) => {
83+ const formData = equipmentDeletionDtoToForm ( editData ) ;
9584 reset ( {
96- [ TYPE ] : editData . equipmentType ,
97- [ EQUIPMENT_ID ] : editData . equipmentId ,
98- [ DELETION_SPECIFIC_DATA ] : null ,
85+ ...formData ,
86+ equipmentInfos : null , // this prop will be computed (cf LCC specific part)
9987 } ) ;
10088 } ,
10189 [ reset ]
10290 ) ;
10391
104- const fromMenuDataValues = useCallback (
105- ( menuSelectId : UUID ) => {
92+ const presetTypeAndId = useCallback (
93+ ( equipmentType : EquipmentType , equipmentId : UUID ) => {
10694 reset ( {
107- [ TYPE ] : EquipmentType . HVDC_LINE ,
108- [ EQUIPMENT_ID ] : menuSelectId ,
109- [ DELETION_SPECIFIC_DATA ] : null ,
95+ equipmentID : equipmentId ,
96+ type : equipmentType ,
97+ equipmentInfos : null ,
11098 } ) ;
11199 } ,
112100 [ reset ]
113101 ) ;
114102
115- const resetFormWithEquipmentType = useCallback (
103+ const presetType = useCallback (
116104 ( equipmentType : EquipmentType ) => {
117105 reset ( {
118- [ TYPE ] : equipmentType ,
106+ type : equipmentType ,
119107 } ) ;
120108 } ,
121109 [ reset ]
122110 ) ;
123111
124112 useEffect ( ( ) => {
125113 if ( editData ) {
114+ // edition mode
126115 fromEditDataToFormValues ( editData ) ;
127- } else if ( defaultIdValue ) {
128- fromMenuDataValues ( defaultIdValue ) ;
116+ } else if ( equipmentType && defaultIdValue ) {
117+ // creation mode with both Id and Type (ex: from diagram)
118+ presetTypeAndId ( equipmentType , defaultIdValue ) ;
129119 } else if ( equipmentType ) {
130- resetFormWithEquipmentType ( equipmentType ) ;
120+ // creation mode with only Type (ex: from node modifications menu)
121+ presetType ( equipmentType ) ;
131122 }
132- } , [
133- fromEditDataToFormValues ,
134- editData ,
135- fromMenuDataValues ,
136- defaultIdValue ,
137- equipmentType ,
138- resetFormWithEquipmentType ,
139- ] ) ;
123+ } , [ defaultIdValue , editData , equipmentType , fromEditDataToFormValues , presetType , presetTypeAndId ] ) ;
140124
141125 const onSubmit = useCallback (
142- ( formData : EquipmentDeletionFormInfos ) => {
143- deleteEquipment ( {
144- studyUuid,
145- nodeUuid : currentNodeUuid ,
146- uuid : editData ?. uuid ,
147- equipmentId : formData [ EQUIPMENT_ID ] as UUID ,
148- equipmentType : formData [ TYPE ] ,
149- equipmentSpecificInfos : formData [ DELETION_SPECIFIC_DATA ] ?? undefined ,
150- } ) . catch ( ( error ) => {
126+ ( formData : EquipmentDeletionFormData ) => {
127+ const dto = equipmentDeletionFormToDto ( formData ) ;
128+ deleteEquipment ( studyUuid , currentNodeUuid , editData ?. uuid , dto ) . catch ( ( error : unknown ) => {
151129 snackWithFallback ( snackError , error , { headerId : 'UnableToDeleteEquipment' } ) ;
152130 } ) ;
153131 } ,
154132 [ currentNodeUuid , editData , snackError , studyUuid ]
155133 ) ;
156134
157135 const clear = useCallback ( ( ) => {
158- reset ( emptyFormData ) ;
136+ reset ( equipmentDeletionEmptyFormData ) ;
159137 } , [ reset ] ) ;
160138
161139 const open = useOpenShortWaitFetching ( {
@@ -169,8 +147,20 @@ const EquipmentDeletionDialog = ({
169147 [ editDataFetchStatus , isUpdate ]
170148 ) ;
171149
150+ const fetchEquipmentIds = useCallback (
151+ ( equipmentType : EquipmentType ) =>
152+ fetchEquipmentsIds ( studyUuid , currentNodeUuid , currentRootNetworkUuid , undefined , equipmentType , true ) ,
153+ [ studyUuid , currentNodeUuid , currentRootNetworkUuid ]
154+ ) ;
155+
156+ const fetchHvdcWithShuntCompensators = useCallback (
157+ ( hvdcLineId : UUID ) =>
158+ fetchHvdcLineWithShuntCompensators ( studyUuid , currentNodeUuid , currentRootNetworkUuid , hvdcLineId ) ,
159+ [ studyUuid , currentNodeUuid , currentRootNetworkUuid ]
160+ ) ;
161+
172162 return (
173- < CustomFormProvider validationSchema = { formSchema } { ...formMethods } >
163+ < CustomFormProvider validationSchema = { equipmentDeletionFormSchema } { ...formMethods } >
174164 < ModificationDialog
175165 fullWidth
176166 maxWidth = "md"
@@ -183,11 +173,10 @@ const EquipmentDeletionDialog = ({
183173 isDataFetching = { waitingForData }
184174 { ...dialogProps }
185175 >
186- < DeleteEquipmentForm
187- studyUuid = { studyUuid }
188- currentNode = { currentNode }
189- currentRootNetworkUuid = { currentRootNetworkUuid }
176+ < EquipmentDeletionForm
190177 editData = { editData }
178+ fetchEquipmentIds = { fetchEquipmentIds }
179+ fetchHvdcWithShuntCompensators = { fetchHvdcWithShuntCompensators }
191180 />
192181 </ ModificationDialog >
193182 </ CustomFormProvider >
0 commit comments