Skip to content

Commit 5d1ca72

Browse files
committed
front: handle delete paced trains
Signed-off-by: SharglutDev <[email protected]>
1 parent 05440ca commit 5d1ca72

File tree

7 files changed

+135
-16
lines changed

7 files changed

+135
-16
lines changed

front/public/locales/en/operationalStudies/scenario.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@
7474
"simulation_failed": "Simulation failed"
7575
},
7676
"invalidTrains": "Some trains are invalid",
77+
"itemsSelectionDeletedCount_one": "The item has been deleted.",
78+
"itemsSelectionDeletedCount_other": "The {{count}} items have been deleted.",
7779
"noItem": "No item",
7880
"noSpeedLimitTags": "Without code",
7981
"noSpeedLimitTagsShort": "None",
8082
"noTrain": "No train",
8183
"pacedTrain": "Service",
84+
"pacedTrainDeleted": "{{name}} service has been deleted",
8285
"punctuality": "Punctuality",
8386
"rollingStockFilterPlaceholder": "Family, detail, serie",
8487
"scheduledPointsHonoredFilter": "Scheduled points honored",
@@ -95,7 +98,6 @@
9598
"trainAdded": "Train added",
9699
"trainDeleted": "{{ name }} train has been deleted.",
97100
"trainSchedule": "Unique train",
98-
99101
"trainsSelectionDeletedCount_one": "The train has been deleted.",
100102
"trainsSelectionDeletedCount_other": "The {{count}} trains have been deleted.",
101103
"trainType": "Services, trains",

front/public/locales/fr/operationalStudies/scenario.json

+3
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@
7474
"simulation_failed": "Simulation impossible"
7575
},
7676
"invalidTrains": "Certains trains sont invalides",
77+
"itemsSelectionDeletedCount_one": "L'item a bien été supprimé.",
78+
"itemsSelectionDeletedCount_other": "Les {{count}} items ont bien été supprimés.",
7779
"noItem": "Aucun item",
7880
"noSpeedLimitTags": "Sans code",
7981
"noSpeedLimitTagsShort": "Aucun",
8082
"noTrain": "Aucun train",
8183
"pacedTrain": "Mission",
84+
"pacedTrainDeleted": "La mission {{name}} a bien été supprimée",
8285
"punctuality": "Ponctualité",
8386
"rollingStockFilterPlaceholder": "Famille, détail, série",
8487
"scheduledPointsHonoredFilter": "Points horaires honorés",

front/src/applications/operationalStudies/components/Scenario/ScenarioContent.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const ScenarioContent = ({
5959
upsertTrainSchedules,
6060
upsertTimetableItems,
6161
removeTrains,
62+
removeTimetableItems,
6263
updateTrainDepartureTime,
6364
} = useScenarioData(scenario, infra);
6465
const macroEditorState = useRef<MacroEditorState>();
@@ -110,6 +111,7 @@ const ScenarioContent = ({
110111
upsertTrainSchedules(upsertedTrainSchedules);
111112
},
112113
addDeletedTrainIds: (trainIds: TimetableItemId[]) => {
114+
// TODO Paced trains : update this to handle delete paced trains in https://github.com/OpenRailAssociation/osrd/issues/10612
113115
removeTrains(trainIds);
114116
},
115117
});
@@ -156,6 +158,7 @@ const ScenarioContent = ({
156158
upsertTrainSchedules={upsertTrainSchedules}
157159
upsertTimetableItems={upsertTimetableItems}
158160
removeTrains={removeTrains}
161+
removeTimetableItems={removeTimetableItems}
159162
setItemIdToEdit={setItemIdToEdit}
160163
itemIdToEdit={itemIdToEdit}
161164
trainSchedules={trainSchedules}

front/src/applications/operationalStudies/hooks/useScenarioData.ts

+23
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ const useScenarioData = (scenario: ScenarioResponse, infra: InfraWithState) => {
261261
[timetableItems]
262262
);
263263

264+
// TODO Paced trains : remove this function in https://github.com/OpenRailAssociation/osrd/issues/10791
264265
const removeTrains = useCallback((_trainIdsToRemove: TimetableItemId[]) => {
265266
setTrainSchedules((prev) => {
266267
const trainSchedulesById = mapBy(prev, 'id');
@@ -287,6 +288,26 @@ const useScenarioData = (scenario: ScenarioResponse, infra: InfraWithState) => {
287288
});
288289
}, []);
289290

291+
const removeTimetableItems = useCallback((_timetableItemsToRemove: TimetableItemId[]) => {
292+
setTimetableItems((prev) => {
293+
const timetableItemsById = mapBy(prev, 'id');
294+
_timetableItemsToRemove.forEach((timetableItemId) => {
295+
timetableItemsById.delete(timetableItemId);
296+
});
297+
return Array.from(timetableItemsById.values());
298+
});
299+
300+
setTimetableItemSummariesById((prev) => {
301+
const newTimetableItemsSummariesById = new Map(prev);
302+
_timetableItemsToRemove.forEach((timetableItemId) => {
303+
newTimetableItemsSummariesById.delete(timetableItemId);
304+
});
305+
return newTimetableItemsSummariesById;
306+
});
307+
308+
// TODO Paced train : Add logic for projected timetable items
309+
}, []);
310+
290311
// TODO Paced train : change this function to handle paced trains in the drag issue
291312
/** Update only depature time of a train */
292313
const updateTrainDepartureTime = useCallback(
@@ -382,6 +403,7 @@ const useScenarioData = (scenario: ScenarioResponse, infra: InfraWithState) => {
382403
: undefined,
383404
conflicts,
384405
removeTrains,
406+
removeTimetableItems,
385407
upsertTrainSchedules,
386408
upsertTimetableItems,
387409
updateTrainDepartureTime,
@@ -398,6 +420,7 @@ const useScenarioData = (scenario: ScenarioResponse, infra: InfraWithState) => {
398420
trainSchedulesResults.length,
399421
conflicts,
400422
removeTrains,
423+
removeTimetableItems,
401424
upsertTrainSchedules,
402425
upsertTimetableItems,
403426
updateTrainDepartureTime,

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

+45-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import cx from 'classnames';
66
import dayjs from 'dayjs';
77
import { useTranslation } from 'react-i18next';
88

9-
import type { PacedTrainId, TrainId } from 'reducers/osrdconf/types';
9+
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
10+
import { setFailure, setSuccess } from 'reducers/main';
11+
import type { PacedTrainId, TimetableItemId, TrainId } from 'reducers/osrdconf/types';
12+
import { useAppDispatch } from 'store';
13+
import { castErrorToFailure } from 'utils/error';
1014
import { ms2min } from 'utils/timeManipulation';
15+
import { formatPacedTrainIdToEditoastTrainId } from 'utils/trainId';
1116

1217
import TimetableItemActions from '../TimetableItemActions';
1318
import useOccurrences from './hooks/useOccurrences';
@@ -18,30 +23,68 @@ type PacedTrainItemProps = {
1823
isInSelection: boolean;
1924
handleSelectPacedTrain: (pacedTrainId: PacedTrainId) => void;
2025
pacedTrain: PacedTrainWithDetails;
26+
// isSelected: boolean;
2127
isOnEdit: boolean;
2228
isProjectionPathUsed: boolean;
2329
selectedTimeTableItemId: TrainId | undefined;
2430
selectPacedTrainToEdit: (pacedTrain: PacedTrainWithDetails) => void;
31+
removePaceTrains: (pacedTrainIdsToRemove: TimetableItemId[]) => void;
32+
// dtoImport: () => void;
2533
};
2634

2735
const PacedTrainItem = ({
2836
isInSelection,
2937
handleSelectPacedTrain,
3038
pacedTrain,
39+
// isSelected,
3140
isOnEdit,
3241
isProjectionPathUsed,
3342
selectPacedTrainToEdit,
3443
selectedTimeTableItemId,
44+
removePaceTrains,
45+
// dtoImport,
3546
}: PacedTrainItemProps) => {
3647
const { t } = useTranslation(['operationalStudies/scenario']);
48+
const dispatch = useAppDispatch();
3749

3850
const [isOccurrencesListOpen, setIsOccurrencesListOpen] = useState(false);
3951
const { occurrences, occurrencesCount } = useOccurrences(pacedTrain);
4052

53+
const [deletePacedTrains] = osrdEditoastApi.endpoints.deletePacedTrain.useMutation();
54+
4155
const toggleOccurrencesList = () => setIsOccurrencesListOpen((open) => !open);
4256
const selectPathProjection = async () => {};
4357
const duplicatePacedTrain = async () => {};
44-
const deletePacedTrain = async () => {};
58+
59+
const deletePacedTrain = async () => {
60+
// if (isSelected) {
61+
// // we need to set selectedTrainId to undefined, otherwise just after the delete,
62+
// // some unvalid rtk calls are dispatched (see rollingstock request in SimulationResults)
63+
// dispatch(updateSelectedTrainId(undefined));
64+
// }
65+
66+
// TODO Paced train : Adapt this to handle paced trains in issue https://github.com/OpenRailAssociation/osrd/issues/10615
67+
deletePacedTrains({
68+
body: { ids: [formatPacedTrainIdToEditoastTrainId(pacedTrain.id)] },
69+
})
70+
.unwrap()
71+
.then(() => {
72+
removePaceTrains([pacedTrain.id]);
73+
// dtoImport();
74+
dispatch(
75+
setSuccess({
76+
title: t('timetable.pacedTrainDeleted', { name: pacedTrain.trainName }),
77+
text: '',
78+
})
79+
);
80+
})
81+
.catch((e) => {
82+
dispatch(setFailure(castErrorToFailure(e)));
83+
// if (isSelected) {
84+
// dispatch(updateSelectedTrainId(train.id));
85+
// }
86+
});
87+
};
4588

4689
return (
4790
<div

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type TimetableProps = {
4343
upsertTimetableItems: (timetableItems: TimetableItemWithTimetableId[]) => void;
4444
setItemIdToEdit: (trainId?: TimetableItemId) => void;
4545
removeTrains: (trainIds: TimetableItemId[]) => void;
46+
removeTimetableItems: (timetableItemsToRemove: TimetableItemId[]) => void;
4647
itemIdToEdit?: TimetableItemId;
4748
trainSchedules?: TrainScheduleResultWithTrainId[];
4849
timetableItems?: TimetableItemWithTimetableId[];
@@ -60,6 +61,7 @@ const Timetable = ({
6061
upsertTrainSchedules,
6162
upsertTimetableItems,
6263
removeTrains,
64+
removeTimetableItems,
6365
setItemIdToEdit,
6466
itemIdToEdit,
6567
trainSchedules = [],
@@ -83,7 +85,11 @@ const Timetable = ({
8385
};
8486

8587
const removeAndUnselectTrains = useCallback((trainIds: TimetableItemId[]) => {
86-
removeTrains(trainIds);
88+
if (showPacedTrains) {
89+
removeTimetableItems(trainIds);
90+
} else {
91+
removeTrains(trainIds);
92+
}
8793
setSelectedTimetableItemIds([]);
8894
dtoImport();
8995
}, []);
@@ -215,9 +221,14 @@ const Timetable = ({
215221
isInSelection={selectedTimetableItemIds.includes(timetableItem.id)}
216222
selectPacedTrainToEdit={selectTimetableItemToEdit}
217223
handleSelectPacedTrain={handleSelectTimetableItem}
224+
// TODO Paced trains : handle this in https://github.com/OpenRailAssociation/osrd/issues/11054
225+
// isSelected={infraState === 'CACHED' && selectedTrainId === timetableItem.id}
218226
isOnEdit={timetableItem.id === itemIdToEdit}
219227
isProjectionPathUsed={false}
220228
selectedTimeTableItemId={selectedTrainId}
229+
removePaceTrains={removeAndUnselectTrains}
230+
// TODO Paced trains : update this to handle delete paced trains in https://github.com/OpenRailAssociation/osrd/issues/10612
231+
// dtoImport={dtoImport}
221232
/>
222233
)}
223234
</div>

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

+46-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import { getSelectedTrainId } from 'reducers/simulationResults/selectors';
2222
import { getShowPacedTrains } from 'reducers/user/userSelectors';
2323
import { useAppDispatch } from 'store';
2424
import { castErrorToFailure } from 'utils/error';
25-
import { formatTrainScheduleIdToEditoastTrainId, isTrainSchedule } from 'utils/trainId';
25+
import {
26+
formatPacedTrainIdToEditoastTrainId,
27+
formatTrainScheduleIdToEditoastTrainId,
28+
isTrainSchedule,
29+
} from 'utils/trainId';
2630

2731
import FilterPanel from './FilterPanel';
2832
import type { TimetableFilters, TimetableItemWithDetails } from './types';
@@ -101,6 +105,7 @@ const TimetableToolbar = ({
101105
);
102106

103107
const [deleteTrainSchedules] = osrdEditoastApi.endpoints.deleteTrainSchedule.useMutation();
108+
const [deletePacedTrains] = osrdEditoastApi.endpoints.deletePacedTrain.useMutation();
104109

105110
const toggleFilterPanel = () => {
106111
setIsFilterPanelOpen(!isFilterPanelOpen);
@@ -118,31 +123,37 @@ const TimetableToolbar = ({
118123
const handleTrainsDelete = async () => {
119124
const itemsCount = selectedTimetableItemIds.length;
120125

121-
// TODO Paced train : Adapt this to handle delete paced trains in issue https://github.com/OpenRailAssociation/osrd/issues/10615
126+
// TODO Paced train : Adapt this to handle paced trains selection in issue https://github.com/OpenRailAssociation/osrd/issues/11054
122127
if (selectedTrainId && selectedTimetableItemIds.includes(selectedTrainId as TrainScheduleId)) {
123128
// we need to set selectedTrainId to undefined, otherwise just after the delete,
124129
// some unvalid rtk calls are dispatched (see rollingstock request in SimulationResults)
125130
dispatch(updateSelectedTrainId(undefined));
126131
}
127132

128-
// TODO Paced train : Adapt this to handle delete paced trains in issue https://github.com/OpenRailAssociation/osrd/issues/10615
129133
const editoastSelectedTrainScheduleIds = selectedTrainScheduleIds.map((id) =>
130134
formatTrainScheduleIdToEditoastTrainId(id)
131135
);
136+
const editoastSelectedPacedTrainIds = selectedPacedTrainIds.map((id) =>
137+
formatPacedTrainIdToEditoastTrainId(id)
138+
);
132139

133-
await deleteTrainSchedules({ body: { ids: editoastSelectedTrainScheduleIds } })
134-
.unwrap()
135-
.then(() => {
136-
removeTrains(selectedTrainScheduleIds);
140+
if (showPacedTrains) {
141+
try {
142+
if (editoastSelectedPacedTrainIds.length > 0) {
143+
await deleteTrainSchedules({ body: { ids: editoastSelectedTrainScheduleIds } }).unwrap();
144+
}
145+
if (editoastSelectedPacedTrainIds.length > 0) {
146+
await deletePacedTrains({ body: { ids: editoastSelectedPacedTrainIds } }).unwrap();
147+
}
148+
removeTrains(selectedTimetableItemIds);
137149
dispatch(
138150
setSuccess({
139-
title: t('timetable.trainsSelectionDeletedCount', { count: itemsCount }),
151+
title: t('timetable.itemsSelectionDeletedCount', { count: itemsCount }),
140152
text: '',
141153
})
142154
);
143-
})
144-
.catch((e) => {
145-
// TODO Paced train : Adapt this to handle delete paced trains in issue https://github.com/OpenRailAssociation/osrd/issues/10615
155+
} catch (e) {
156+
// TODO Paced train : Adapt this to handle paced trains selection in issue https://github.com/OpenRailAssociation/osrd/issues/11054
146157
if (
147158
selectedTrainId &&
148159
selectedTimetableItemIds.includes(selectedTrainId as TrainScheduleId)
@@ -151,7 +162,30 @@ const TimetableToolbar = ({
151162
} else {
152163
dispatch(setFailure(castErrorToFailure(e)));
153164
}
154-
});
165+
}
166+
// TODO Paced trains : remove the else in https://github.com/OpenRailAssociation/osrd/issues/10791
167+
} else {
168+
await deleteTrainSchedules({ body: { ids: editoastSelectedTrainScheduleIds } })
169+
.unwrap()
170+
.then(() => {
171+
dispatch(
172+
setSuccess({
173+
title: t('timetable.trainsSelectionDeletedCount', { count: itemsCount }),
174+
text: '',
175+
})
176+
);
177+
})
178+
.catch((e) => {
179+
if (
180+
selectedTrainId &&
181+
selectedTimetableItemIds.includes(selectedTrainId as TrainScheduleId)
182+
) {
183+
dispatch(updateSelectedTrainId(selectedTrainId));
184+
} else {
185+
dispatch(setFailure(castErrorToFailure(e)));
186+
}
187+
});
188+
}
155189
};
156190

157191
// TODO Paced train : Adapt this to handle export paced trains in issue https://github.com/OpenRailAssociation/osrd/issues/10614

0 commit comments

Comments
 (0)