Skip to content

Commit 1219b0b

Browse files
committed
front: fix timetable train selection
no train was selected after deleting a train
1 parent 6958130 commit 1219b0b

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

front/src/modules/trainschedule/components/Timetable/Timetable.tsx

+38-6
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,37 @@ export default function Timetable({
130130
}
131131
};
132132

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);
135151

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+
}
140156
}
141157

158+
return [undeletedTrains, undeletedTrainsIds];
159+
}, [selectedTrainIds, trainsList]);
160+
161+
const handleTrainsDelete = async () => {
162+
const trainsCount = selectedTrainIds.length;
163+
142164
await deleteTrainSchedules({ body: { ids: selectedTrainIds } })
143165
.unwrap()
144166
.then(() => {
@@ -148,6 +170,15 @@ export default function Timetable({
148170
text: '',
149171
})
150172
);
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+
}
151182
})
152183
.catch((e: unknown) => {
153184
console.error(e);
@@ -279,6 +310,7 @@ export default function Timetable({
279310
isInSelection={selectedTrainIds.includes(train.id)}
280311
toggleTrainSelection={toggleTrainSelection}
281312
train={train}
313+
firstTwoTrainsIds={firstTwoTrainsIds}
282314
intervalPosition={valueToInterval(train.duration, trainsDurationsIntervals)}
283315
key={`timetable-train-card-${train.id}-${train.path_id}`}
284316
isSelected={infraState === 'CACHED' && selectedTrainId === train.id}

front/src/modules/trainschedule/components/Timetable/TimetableTrainCard.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Props = {
3131
isSelectable: boolean;
3232
isInSelection: boolean;
3333
train: ScheduledTrain;
34+
firstTwoTrainsIds: number[];
3435
intervalPosition?: number;
3536
isSelected: boolean;
3637
isModified?: boolean;
@@ -45,6 +46,7 @@ function TimetableTrainCard({
4546
isSelectable,
4647
isInSelection,
4748
train,
49+
firstTwoTrainsIds,
4850
intervalPosition,
4951
isSelected,
5052
isModified,
@@ -77,7 +79,7 @@ function TimetableTrainCard({
7779

7880
const deleteTrain = async () => {
7981
if (isSelected) {
80-
// we need to set selectedTrainId to undefined, otherwise just after the delete,
82+
// we need to set selectedTrainId to undefined first, otherwise just after the delete,
8183
// some unvalid rtk calls are dispatched (see rollingstock request in SimulationResults)
8284
dispatch(updateSelectedTrainId(undefined));
8385
}
@@ -90,6 +92,14 @@ function TimetableTrainCard({
9092
text: '',
9193
})
9294
);
95+
96+
// the behavior if the selectedTrain is also the projectedTrain is in getSimulationResults
97+
if (isSelected && !projectionPathIsUsed) {
98+
// we check if the train about to be deleted is the first of the timetable
99+
if (firstTwoTrainsIds[0] === train.id)
100+
dispatch(updateSelectedTrainId(firstTwoTrainsIds[1]));
101+
else dispatch(updateSelectedTrainId(firstTwoTrainsIds[0]));
102+
}
93103
})
94104
.catch((e: unknown) => {
95105
console.error(e);

0 commit comments

Comments
 (0)