@@ -130,15 +130,37 @@ export default function Timetable({
130
130
}
131
131
} ;
132
132
133
- const handleTrainsDelete = async ( ) => {
134
- const trainsCount = selectedTrainIds . length ;
133
+ /**
134
+ * In case we are deleting the selectedTrain, this array prevents an unnecessary api call
135
+ * firstTwoTrainsIds will be used as props for TimetableTrainCard to avoid passing
136
+ * an eventual and unnecessary very large array to this component
137
+ */
138
+ const [ remainingTrains , firstTwoTrainsIds ] = useMemo ( ( ) => {
139
+ let undeletedTrains : ScheduledTrain [ ] = [ ] ;
140
+ let undeletedTrainsIds : number [ ] = [ ] ;
141
+
142
+ if ( trainsList . length > 0 ) {
143
+ if ( selectedTrainIds . length === 0 ) {
144
+ undeletedTrains = trainsList ;
145
+ if ( trainsList . length > 1 ) undeletedTrainsIds = [ trainsList [ 0 ] . id , trainsList [ 1 ] . id ] ;
146
+ else undeletedTrainsIds = [ trainsList [ 0 ] . id ] ;
147
+ } else {
148
+ undeletedTrains = trainsList
149
+ . filter ( ( train ) => ! selectedTrainIds . includes ( train . id ) )
150
+ . sort ( ( trainA , trainB ) => trainA . departure_time - trainB . departure_time ) ;
135
151
136
- if ( selectedTrainId && selectedTrainIds . includes ( selectedTrainId ) ) {
137
- // we need to set selectedTrainId to undefined, otherwise just after the delete,
138
- // some unvalid rtk calls are dispatched (see rollingstock request in SimulationResults)
139
- dispatch ( updateSelectedTrainId ( undefined ) ) ;
152
+ if ( undeletedTrains . length > 1 )
153
+ undeletedTrainsIds = [ undeletedTrains [ 0 ] , undeletedTrains [ 1 ] ] . map ( ( train ) => train . id ) ;
154
+ else if ( undeletedTrains . length > 0 ) undeletedTrainsIds = [ undeletedTrains [ 0 ] . id ] ;
155
+ }
140
156
}
141
157
158
+ return [ undeletedTrains , undeletedTrainsIds ] ;
159
+ } , [ selectedTrainIds , trainsList ] ) ;
160
+
161
+ const handleTrainsDelete = async ( ) => {
162
+ const trainsCount = selectedTrainIds . length ;
163
+
142
164
await deleteTrainSchedules ( { body : { ids : selectedTrainIds } } )
143
165
. unwrap ( )
144
166
. then ( ( ) => {
@@ -148,6 +170,15 @@ export default function Timetable({
148
170
text : '' ,
149
171
} )
150
172
) ;
173
+
174
+ // The behavior if the selectedTrain is also the projectedTrain is in getSimulationResults
175
+ if (
176
+ selectedTrainId &&
177
+ selectedTrainIds . includes ( selectedTrainId ) &&
178
+ selectedTrainId !== selectedProjection ?. id
179
+ ) {
180
+ dispatch ( updateSelectedTrainId ( remainingTrains [ 0 ] ?. id || undefined ) ) ;
181
+ }
151
182
} )
152
183
. catch ( ( e : unknown ) => {
153
184
console . error ( e ) ;
@@ -279,6 +310,7 @@ export default function Timetable({
279
310
isInSelection = { selectedTrainIds . includes ( train . id ) }
280
311
toggleTrainSelection = { toggleTrainSelection }
281
312
train = { train }
313
+ firstTwoTrainsIds = { firstTwoTrainsIds }
282
314
intervalPosition = { valueToInterval ( train . duration , trainsDurationsIntervals ) }
283
315
key = { `timetable-train-card-${ train . id } -${ train . path_id } ` }
284
316
isSelected = { infraState === 'CACHED' && selectedTrainId === train . id }
0 commit comments