-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseGetProjectedTrainOperationalPoints.ts
96 lines (83 loc) · 3.7 KB
/
useGetProjectedTrainOperationalPoints.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { useEffect, useState } from 'react';
import { omit } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { upsertMapWaypointsInOperationalPoints } from 'applications/operationalStudies/helpers/upsertMapWaypointsInOperationalPoints';
import type { OperationalPoint } from 'applications/operationalStudies/types';
import { STDCM_TRAIN_ID } from 'applications/stdcm/consts';
import {
osrdEditoastApi,
type PathProperties,
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { isStation } from 'modules/pathfinding/utils';
const useGetProjectedTrainOperationalPoints = (
trainScheduleUsedForProjection?: TrainScheduleResult,
trainIdUsedForProjection?: number,
infraId?: number
) => {
const { t } = useTranslation('simulation');
const { getTimetableID } = useOsrdConfSelectors();
const timetableId = useSelector(getTimetableID);
const [operationalPoints, setOperationalPoints] = useState<OperationalPoint[]>([]);
const [filteredOperationalPoints, setFilteredOperationalPoints] =
useState<OperationalPoint[]>(operationalPoints);
const { data: pathfindingResult } = osrdEditoastApi.endpoints.getTrainScheduleByIdPath.useQuery(
{
id: trainIdUsedForProjection!,
infraId: infraId!,
},
{
skip: !trainIdUsedForProjection || !infraId || trainIdUsedForProjection === STDCM_TRAIN_ID,
}
);
const [postPathProperties] =
osrdEditoastApi.endpoints.postInfraByInfraIdPathProperties.useLazyQuery();
useEffect(() => {
const getOperationalPoints = async () => {
if (infraId && trainScheduleUsedForProjection && pathfindingResult?.status === 'success') {
const { operational_points } = await postPathProperties({
infraId,
props: ['operational_points'],
pathPropertiesInput: {
track_section_ranges: pathfindingResult.track_section_ranges,
},
}).unwrap();
const operationalPointsWithAllWaypoints = upsertMapWaypointsInOperationalPoints(
trainScheduleUsedForProjection.path,
pathfindingResult.path_item_positions,
operational_points!,
t
);
let operationalPointsWithUniqueIds = operationalPointsWithAllWaypoints.map((op, i) => ({
...op,
id: `${op.id}-${op.position}-${i}`,
}));
setOperationalPoints(operationalPointsWithUniqueIds);
// Check if there are saved manchettes in localStorage for the current timetable and path
const simplifiedPath = trainScheduleUsedForProjection.path.map((waypoint) =>
omit(waypoint, ['id', 'deleted'])
);
const stringifiedSavedWaypoints = localStorage.getItem(
`${timetableId}-${JSON.stringify(simplifiedPath)}`
);
if (stringifiedSavedWaypoints) {
operationalPointsWithUniqueIds = JSON.parse(stringifiedSavedWaypoints) as NonNullable<
PathProperties['operational_points']
>;
} else {
// If the manchette hasn't been saved, we want to display by default only
// the waypoints with CH BV/00/'' and the path steps (origin, destination, vias)
operationalPointsWithUniqueIds = operationalPointsWithUniqueIds.filter((op) =>
op.extensions?.sncf ? isStation(op.extensions.sncf.ch) || op.weight === 100 : true
);
}
setFilteredOperationalPoints(operationalPointsWithUniqueIds);
}
};
getOperationalPoints();
}, [pathfindingResult, infraId, t]);
return { operationalPoints, filteredOperationalPoints, setFilteredOperationalPoints };
};
export default useGetProjectedTrainOperationalPoints;