@@ -16,9 +16,10 @@ import {
16
16
resetEquipments ,
17
17
resetEquipmentsByTypes ,
18
18
updateEquipments ,
19
+ type UpdateEquipmentsAction ,
19
20
} from 'redux/actions' ;
20
21
import { type AppState } from 'redux/reducer' ;
21
- import { SpreadsheetEquipmentType } from '../../../types/spreadsheet.type' ;
22
+ import { isSpreadsheetEquipmentType , SpreadsheetEquipmentType } from '../../../types/spreadsheet.type' ;
22
23
import { fetchAllEquipments } from 'services/study/network-map' ;
23
24
import type { NodeAlias } from '../../../types/node-alias.type' ;
24
25
import { isStatusBuilt } from '../../../../graph/util/model-functions' ;
@@ -27,6 +28,7 @@ import { type DeletedEquipment, isStudyNotification, type NetworkImpactsInfos }
27
28
import { NodeType } from '../../../../graph/tree-node.type' ;
28
29
import { validAlias } from '../../../hooks/use-node-aliases' ;
29
30
import { fetchNetworkElementInfos } from 'services/study/network' ;
31
+ import { EQUIPMENT_INFOS_TYPES } from '../../../../utils/equipment-types' ;
30
32
31
33
export const useSpreadsheetEquipments = (
32
34
type : SpreadsheetEquipmentType ,
@@ -45,10 +47,8 @@ export const useSpreadsheetEquipments = (
45
47
const currentRootNetworkUuid = useSelector ( ( state : AppState ) => state . currentRootNetworkUuid ) ;
46
48
const currentNode = useSelector ( ( state : AppState ) => state . currentTreeNode ) ;
47
49
const treeNodes = useSelector ( ( state : AppState ) => state . networkModificationTreeModel ?. treeNodes ) ;
48
- const [ builtAliasedNodesIds , setBuiltAliasedNodesIds ] = useState < UUID [ ] > ( ) ;
49
-
50
+ const [ builtAliasedNodesIds , setBuiltAliasedNodesIds ] = useState < UUID [ ] > ( [ ] ) ;
50
51
const [ isFetching , setIsFetching ] = useState < boolean > ( false ) ;
51
-
52
52
const { fetchNodesEquipmentData } = useFetchEquipment ( type ) ;
53
53
54
54
// effect to keep builtAliasedNodesIds up-to-date (when we add/remove an alias or build/unbuild an aliased node)
@@ -61,25 +61,20 @@ export const useSpreadsheetEquipments = (
61
61
. filter ( ( nodeAlias ) => validAlias ( nodeAlias ) )
62
62
. map ( ( nodeAlias ) => nodeAlias . id ) ;
63
63
if ( aliasedNodesIds . length > 0 ) {
64
- treeNodes ?. forEach ( ( treeNode ) => {
65
- if (
66
- aliasedNodesIds . includes ( treeNode . id ) &&
67
- ( treeNode . type === NodeType . ROOT || isStatusBuilt ( treeNode . data . globalBuildStatus ) )
68
- ) {
69
- computedIds . push ( treeNode . id ) ;
70
- }
71
- } ) ;
64
+ computedIds =
65
+ treeNodes
66
+ ?. filter (
67
+ ( treeNode ) =>
68
+ aliasedNodesIds . includes ( treeNode . id ) &&
69
+ ( treeNode . type === NodeType . ROOT || isStatusBuilt ( treeNode . data . globalBuildStatus ) )
70
+ )
71
+ . map ( ( treeNode ) => treeNode . id ) ?? [ ] ;
72
72
}
73
+ computedIds . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
73
74
// Because of treeNodes: update the state only on real values changes (to avoid multiple effects for the watchers)
74
- setBuiltAliasedNodesIds ( ( prevState ) => {
75
- const currentIds = prevState ;
76
- currentIds ?. sort ( ( a , b ) => a . localeCompare ( b ) ) ;
77
- computedIds . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
78
- if ( JSON . stringify ( currentIds ) !== JSON . stringify ( computedIds ) ) {
79
- return computedIds ;
80
- }
81
- return prevState ;
82
- } ) ;
75
+ setBuiltAliasedNodesIds ( ( prevState ) =>
76
+ JSON . stringify ( prevState ) !== JSON . stringify ( computedIds ) ? computedIds : prevState
77
+ ) ;
83
78
} , [ nodeAliases , treeNodes ] ) ;
84
79
85
80
const nodesIdToFetch = useMemo ( ( ) => {
@@ -92,17 +87,17 @@ export const useSpreadsheetEquipments = (
92
87
nodesIdToFetch . add ( currentNode . id ) ;
93
88
}
94
89
// Then we do the same for the other nodes we need the data of (the ones defined in aliases)
95
- builtAliasedNodesIds . forEach ( ( builtAliasNodeId ) => {
90
+ for ( const builtAliasNodeId of builtAliasedNodesIds ) {
96
91
if ( equipments . nodesId . find ( ( nodeId ) => nodeId === builtAliasNodeId ) === undefined ) {
97
92
nodesIdToFetch . add ( builtAliasNodeId ) ;
98
93
}
99
- } ) ;
94
+ }
100
95
return nodesIdToFetch ;
101
96
} , [ currentNode ?. id , equipments . nodesId , builtAliasedNodesIds ] ) ;
102
97
103
98
// effect to unload equipment data when we remove an alias or unbuild an aliased node
104
99
useEffect ( ( ) => {
105
- if ( ! equipments || ! builtAliasedNodesIds || ! currentNode ?. id ) {
100
+ if ( ! equipments || ! builtAliasedNodesIds . length || ! currentNode ?. id ) {
106
101
return ;
107
102
}
108
103
const currentNodeId = currentNode . id ;
@@ -121,7 +116,7 @@ export const useSpreadsheetEquipments = (
121
116
return ;
122
117
}
123
118
// updating data related to impacted elements
124
- const nodeId = currentNode ?. id as UUID ;
119
+ const nodeId = currentNode ?. id as UUID ; //TODO maybe do nothing if no current node?
125
120
126
121
// Handle updates and resets based on impacted element types
127
122
if ( impactedElementTypes . length > 0 ) {
@@ -133,7 +128,9 @@ export const useSpreadsheetEquipments = (
133
128
Object . keys ( allEquipments ) . includes ( type )
134
129
) ;
135
130
if ( impactedSpreadsheetEquipmentsTypes . length > 0 ) {
136
- dispatch ( resetEquipmentsByTypes ( impactedSpreadsheetEquipmentsTypes as SpreadsheetEquipmentType [ ] ) ) ;
131
+ dispatch (
132
+ resetEquipmentsByTypes ( impactedSpreadsheetEquipmentsTypes . filter ( isSpreadsheetEquipmentType ) )
133
+ ) ;
137
134
}
138
135
}
139
136
@@ -154,17 +151,51 @@ export const useSpreadsheetEquipments = (
154
151
) ;
155
152
} else {
156
153
// here, we can fetch only the data for the modified equipment
157
- fetchNetworkElementInfos (
158
- studyUuid ,
159
- nodeId ,
160
- currentRootNetworkUuid ,
161
- type ,
162
- 'TAB' ,
163
- equipmentToUpdateId ,
164
- false
165
- ) . then ( ( value : Identifiable ) => {
166
- highlightUpdatedEquipment ( ) ;
167
- dispatch ( updateEquipments ( { [ type ] : [ value ] } , nodeId ) ) ;
154
+ const promises = [
155
+ fetchNetworkElementInfos (
156
+ studyUuid ,
157
+ nodeId ,
158
+ currentRootNetworkUuid ,
159
+ type ,
160
+ EQUIPMENT_INFOS_TYPES . TAB . type ,
161
+ equipmentToUpdateId ,
162
+ false
163
+ ) ,
164
+ ] ;
165
+ if (
166
+ type === SpreadsheetEquipmentType . LINE ||
167
+ type === SpreadsheetEquipmentType . TWO_WINDINGS_TRANSFORMER
168
+ ) {
169
+ promises . push (
170
+ fetchNetworkElementInfos (
171
+ studyUuid ,
172
+ nodeId ,
173
+ currentRootNetworkUuid ,
174
+ SpreadsheetEquipmentType . BRANCH ,
175
+ EQUIPMENT_INFOS_TYPES . TAB . type ,
176
+ equipmentToUpdateId ,
177
+ false
178
+ )
179
+ ) ;
180
+ }
181
+ Promise . allSettled ( promises ) . then ( ( results ) => {
182
+ const updates : UpdateEquipmentsAction [ 'equipments' ] = { } ;
183
+ if ( results [ 0 ] . status === 'rejected' ) {
184
+ //TODO show snackbar error?
185
+ } else {
186
+ updates [ type ] = results [ 0 ] . value ;
187
+ }
188
+ if ( results . length > 1 ) {
189
+ if ( results [ 1 ] . status === 'rejected' ) {
190
+ //TODO show snackbar error?
191
+ } else {
192
+ updates [ SpreadsheetEquipmentType . BRANCH ] = results [ 1 ] . value ;
193
+ }
194
+ }
195
+ if ( Object . keys ( updates ) . length > 1 ) {
196
+ highlightUpdatedEquipment ( ) ;
197
+ dispatch ( updateEquipments ( updates , nodeId ) ) ;
198
+ }
168
199
} ) ;
169
200
}
170
201
}
@@ -183,11 +214,12 @@ export const useSpreadsheetEquipments = (
183
214
} ) ;
184
215
185
216
if ( equipmentsToDelete . length > 0 ) {
186
- const equipmentsToDeleteArray : EquipmentToDelete [ ] = equipmentsToDelete . map ( ( equipment ) => ( {
187
- equipmentType : equipment . equipmentType as SpreadsheetEquipmentType ,
188
- equipmentId : equipment . equipmentId ,
189
- nodeId : nodeId ,
190
- } ) ) ;
217
+ const equipmentsToDeleteArray = equipmentsToDelete
218
+ . filter ( ( e ) => isSpreadsheetEquipmentType ( e . equipmentType ) )
219
+ . map < EquipmentToDelete > ( ( equipment ) => ( {
220
+ equipmentType : equipment . equipmentType as unknown as SpreadsheetEquipmentType ,
221
+ equipmentId : equipment . equipmentId ,
222
+ } ) ) ;
191
223
dispatch ( deleteEquipments ( equipmentsToDeleteArray , nodeId ) ) ;
192
224
}
193
225
}
0 commit comments