@@ -11,6 +11,7 @@ import { Box, Grid } from '@mui/material';
1111import { FormattedMessage , useIntl } from 'react-intl' ;
1212import { ReadOnlyInput } from '../../utils/rhf-inputs/read-only/read-only-input' ;
1313import {
14+ AREA ,
1415 FINAL_CURRENT_LIMITS ,
1516 SEGMENT_CURRENT_LIMITS ,
1617 SEGMENT_DISTANCE_VALUE ,
@@ -20,6 +21,8 @@ import {
2021 SEGMENT_TYPE_ID ,
2122 SEGMENT_TYPE_VALUE ,
2223 SEGMENTS ,
24+ SHAPE_FACTOR ,
25+ TEMPERATURE ,
2326 TOTAL_REACTANCE ,
2427 TOTAL_RESISTANCE ,
2528 TOTAL_SUSCEPTANCE ,
@@ -48,6 +51,7 @@ import {
4851import { emptyLineSegment , SegmentFormData } from './segment-utils' ;
4952import { ColDef } from 'ag-grid-community' ;
5053import GridSection from '../commons/grid-section' ;
54+ import { LineSegmentInfos } from '../../../services/network-modification-types' ;
5155
5256const styles = {
5357 h3 : {
@@ -64,7 +68,11 @@ const styles = {
6468 } ,
6569} as const satisfies MuiStyles ;
6670
67- export const LineTypeSegmentForm = ( ) => {
71+ export interface LineTypeSegmentFormProps {
72+ editData ?: LineSegmentInfos [ ] ;
73+ }
74+
75+ export const LineTypeSegmentForm = ( { editData } : Readonly < LineTypeSegmentFormProps > ) => {
6876 const { setValue, getValues, clearErrors } = useFormContext ( ) ;
6977 const [ lineTypesCatalog , setLineTypesCatalog ] = useState < LineTypeInfo [ ] > ( [ ] ) ;
7078 const [ openCatalogDialogIndex , setOpenCatalogDialogIndex ] = useState < number | null > ( null ) ;
@@ -117,6 +125,15 @@ export const LineTypeSegmentForm = () => {
117125 [ setValue ]
118126 ) ;
119127
128+ const updateLimits = useCallback (
129+ ( index : number , id : string , area : string | null , temperature : string | null , shapeFactor : number | null ) => {
130+ getLineTypeWithLimits ( id , area , temperature , shapeFactor ) . then ( ( lineTypeWithLimits ) => {
131+ updateSegmentLimitsValues ( index , lineTypeWithLimits . limitsForLineType ) ;
132+ } ) ;
133+ } ,
134+ [ updateSegmentLimitsValues ]
135+ ) ;
136+
120137 const updateTotals = useCallback ( ( ) => {
121138 const segments : SegmentFormData [ ] = getValues ( SEGMENTS ) ;
122139 const totalResistance = segments . reduce (
@@ -179,6 +196,42 @@ export const LineTypeSegmentForm = () => {
179196 setValue ( FINAL_CURRENT_LIMITS , Array . from ( mostContrainingLimits . values ( ) ) ) ;
180197 } , [ getValues , setValue , setCurrentLimitResult ] ) ;
181198
199+ useEffect ( ( ) => {
200+ if ( ! editData ) {
201+ return ;
202+ }
203+ for ( let index = 0 ; index < editData ?. length ; index ++ ) {
204+ const segmentCatalogId = editData [ index ] [ SEGMENT_TYPE_ID ] ;
205+ setValue ( `${ SEGMENTS } .${ index } .${ SEGMENT_TYPE_ID } ` , segmentCatalogId ) ;
206+ const lineTypeInfo : LineTypeInfo | undefined = lineTypesCatalog . find (
207+ ( segment ) => segment . id === segmentCatalogId
208+ ) ;
209+ setValue ( `${ SEGMENTS } .${ index } .${ SEGMENT_TYPE_VALUE } ` , lineTypeInfo ?. type ?? '' ) ;
210+ setValue ( `${ SEGMENTS } .${ index } .${ SEGMENT_DISTANCE_VALUE } ` , Number ( editData [ index ] [ SEGMENT_DISTANCE_VALUE ] ) ) ;
211+ setValue ( `${ SEGMENTS } .${ index } .${ AREA } ` , editData [ index ] [ AREA ] ) ;
212+ setValue ( `${ SEGMENTS } .${ index } .${ TEMPERATURE } ` , editData [ index ] [ TEMPERATURE ] ) ;
213+ setValue ( `${ SEGMENTS } .${ index } .${ SHAPE_FACTOR } ` , editData [ index ] [ SHAPE_FACTOR ] ) ;
214+ updateLimits (
215+ index ,
216+ segmentCatalogId ,
217+ editData [ index ] [ AREA ] ,
218+ editData [ index ] [ TEMPERATURE ] ,
219+ editData [ index ] [ SHAPE_FACTOR ]
220+ ) ;
221+ updateSegmentValues ( index ) ;
222+ updateTotals ( ) ;
223+ keepMostConstrainingLimits ( ) ;
224+ }
225+ } , [
226+ editData ,
227+ updateLimits ,
228+ keepMostConstrainingLimits ,
229+ lineTypesCatalog ,
230+ setValue ,
231+ updateSegmentValues ,
232+ updateTotals ,
233+ ] ) ;
234+
182235 const onSelectCatalogLine = useCallback (
183236 ( selectedLine : LineTypeInfo , selectedAreaAndTemperature2LineTypeData : AreaTemperatureShapeFactorInfo ) => {
184237 getLineTypeWithLimits (
@@ -193,6 +246,18 @@ export const LineTypeSegmentForm = () => {
193246 const selectedTypeId = lineTypeWithLimits . id ?? '' ;
194247 setValue ( `${ SEGMENTS } .${ openCatalogDialogIndex } .${ SEGMENT_TYPE_VALUE } ` , selectedType ) ;
195248 setValue ( `${ SEGMENTS } .${ openCatalogDialogIndex } .${ SEGMENT_TYPE_ID } ` , selectedTypeId ) ;
249+ setValue (
250+ `${ SEGMENTS } .${ openCatalogDialogIndex } .${ AREA } ` ,
251+ selectedAreaAndTemperature2LineTypeData ?. area
252+ ) ;
253+ setValue (
254+ `${ SEGMENTS } .${ openCatalogDialogIndex } .${ TEMPERATURE } ` ,
255+ selectedAreaAndTemperature2LineTypeData ?. temperature
256+ ) ;
257+ setValue (
258+ `${ SEGMENTS } .${ openCatalogDialogIndex } .${ SHAPE_FACTOR } ` ,
259+ selectedAreaAndTemperature2LineTypeData ?. shapeFactor
260+ ) ;
196261 clearErrors ( `${ SEGMENTS } .${ openCatalogDialogIndex } .${ SEGMENT_TYPE_VALUE } ` ) ;
197262 updateSegmentValues ( openCatalogDialogIndex ) ;
198263 updateSegmentLimitsValues ( openCatalogDialogIndex , lineTypeWithLimits . limitsForLineType ) ;
@@ -249,6 +314,17 @@ export const LineTypeSegmentForm = () => {
249314 [ getValues ]
250315 ) ;
251316
317+ const getPreselectedRowData = useCallback (
318+ ( index : number ) => {
319+ return {
320+ temperature : getValues ( `${ SEGMENTS } .${ index } .${ TEMPERATURE } ` ) ,
321+ area : getValues ( `${ SEGMENTS } .${ index } .${ AREA } ` ) ,
322+ shapeFactor : getValues ( `${ SEGMENTS } .${ index } .${ SHAPE_FACTOR } ` ) ,
323+ } ;
324+ } ,
325+ [ getValues ]
326+ ) ;
327+
252328 const segmentTypeHeader = (
253329 < Box sx = { styles . header } >
254330 < FormattedMessage id = { 'lineTypes.type' } />
@@ -415,6 +491,7 @@ export const LineTypeSegmentForm = () => {
415491 rowData = { lineTypesCatalog }
416492 onSelectLine = { onSelectCatalogLine }
417493 preselectedRowId = { getPreselectedRowIdForCatalog ( openCatalogDialogIndex ) }
494+ preselectedParams = { getPreselectedRowData ( openCatalogDialogIndex ) }
418495 />
419496 ) }
420497 </ >
0 commit comments