1
1
import { useEffect , useState } from 'react' ;
2
2
3
+ import { useTranslation } from 'react-i18next' ;
4
+
3
5
import { STDCM_TRAIN_ID } from 'applications/stdcm/consts' ;
4
6
import {
5
7
osrdEditoastApi ,
6
8
type PathProperties ,
7
9
type PostV2InfraByInfraIdPathPropertiesApiArg ,
8
10
} from 'common/api/osrdEditoastApi' ;
11
+ import { addElementAtIndex } from 'utils/array' ;
9
12
10
13
const useGetProjectedTrainOperationalPoints = (
11
14
trainIdUsedForProjection ?: number ,
12
15
infraId ?: number
13
16
) => {
17
+ const { t } = useTranslation ( 'simulation' ) ;
14
18
const [ operationalPoints , setOperationalPoints ] = useState <
15
19
NonNullable < PathProperties [ 'operational_points' ] >
16
20
> ( [ ] ) ;
17
21
22
+ const { data : trainScheduleUsedForProjection } =
23
+ osrdEditoastApi . endpoints . getV2TrainScheduleById . useQuery (
24
+ {
25
+ id : trainIdUsedForProjection as number ,
26
+ } ,
27
+ {
28
+ skip : ! trainIdUsedForProjection ,
29
+ }
30
+ ) ;
31
+
18
32
const { data : pathfindingResult } = osrdEditoastApi . endpoints . getV2TrainScheduleByIdPath . useQuery (
19
33
{
20
34
id : trainIdUsedForProjection as number ,
@@ -30,7 +44,12 @@ const useGetProjectedTrainOperationalPoints = (
30
44
31
45
useEffect ( ( ) => {
32
46
const getOperationalPoints = async ( ) => {
33
- if ( infraId && pathfindingResult && pathfindingResult . status === 'success' ) {
47
+ if (
48
+ infraId &&
49
+ trainScheduleUsedForProjection &&
50
+ pathfindingResult &&
51
+ pathfindingResult . status === 'success'
52
+ ) {
34
53
const pathPropertiesParams : PostV2InfraByInfraIdPathPropertiesApiArg = {
35
54
infraId,
36
55
props : [ 'operational_points' ] ,
@@ -40,9 +59,40 @@ const useGetProjectedTrainOperationalPoints = (
40
59
} ;
41
60
const { operational_points } = await postPathProperties ( pathPropertiesParams ) . unwrap ( ) ;
42
61
43
- setOperationalPoints (
44
- operational_points as NonNullable < PathProperties [ 'operational_points' ] >
45
- ) ;
62
+ let operationalPointsWithAllWaypoints = operational_points as NonNullable <
63
+ PathProperties [ 'operational_points' ]
64
+ > ;
65
+ // Check if there are vias added by map click and insert them in the operational points
66
+ if ( trainScheduleUsedForProjection . path . some ( ( step ) => 'track' in step ) ) {
67
+ trainScheduleUsedForProjection . path . forEach ( ( step , i ) => {
68
+ if ( 'track' in step ) {
69
+ const positionOnPath = pathfindingResult . path_item_positions [ i ] ;
70
+ const indexToInsert = operationalPointsWithAllWaypoints . findIndex (
71
+ ( op ) => op . position >= positionOnPath
72
+ ) ;
73
+
74
+ const formattedStep : NonNullable < PathProperties [ 'operational_points' ] > [ number ] = {
75
+ id : step . id ,
76
+ extensions : {
77
+ identifier : {
78
+ name : t ( 'requestedPoint' , { count : indexToInsert } ) ,
79
+ uic : 0 ,
80
+ } ,
81
+ } ,
82
+ part : { track : step . track , position : step . offset } ,
83
+ position : positionOnPath ,
84
+ } ;
85
+
86
+ operationalPointsWithAllWaypoints = addElementAtIndex (
87
+ operationalPointsWithAllWaypoints ,
88
+ indexToInsert ,
89
+ formattedStep
90
+ ) ;
91
+ }
92
+ } ) ;
93
+ }
94
+
95
+ setOperationalPoints ( operationalPointsWithAllWaypoints ) ;
46
96
}
47
97
} ;
48
98
getOperationalPoints ( ) ;
0 commit comments