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
- type PostInfraByInfraIdPathPropertiesApiArg ,
9
+ type TrainScheduleResult ,
8
10
} from 'common/api/osrdEditoastApi' ;
11
+ import { addElementAtIndex } from 'utils/array' ;
9
12
10
13
const useGetProjectedTrainOperationalPoints = (
14
+ trainScheduleUsedForProjection ?: TrainScheduleResult ,
11
15
trainIdUsedForProjection ?: number ,
12
16
infraId ?: number
13
17
) => {
18
+ const { t } = useTranslation ( 'simulation' ) ;
14
19
const [ operationalPoints , setOperationalPoints ] = useState <
15
20
NonNullable < PathProperties [ 'operational_points' ] >
16
21
> ( [ ] ) ;
17
22
18
23
const { data : pathfindingResult } = osrdEditoastApi . endpoints . getTrainScheduleByIdPath . useQuery (
19
24
{
20
- id : trainIdUsedForProjection as number ,
21
- infraId : infraId as number ,
25
+ id : trainIdUsedForProjection ! ,
26
+ infraId : infraId ! ,
22
27
} ,
23
28
{
24
29
skip : ! trainIdUsedForProjection || ! infraId || trainIdUsedForProjection === STDCM_TRAIN_ID ,
@@ -30,23 +35,60 @@ const useGetProjectedTrainOperationalPoints = (
30
35
31
36
useEffect ( ( ) => {
32
37
const getOperationalPoints = async ( ) => {
33
- if ( infraId && pathfindingResult && pathfindingResult . status === 'success' ) {
34
- const pathPropertiesParams : PostInfraByInfraIdPathPropertiesApiArg = {
38
+ if ( infraId && trainScheduleUsedForProjection && pathfindingResult ? .status === 'success' ) {
39
+ const { operational_points } = await postPathProperties ( {
35
40
infraId,
36
41
props : [ 'operational_points' ] ,
37
42
pathPropertiesInput : {
38
43
track_section_ranges : pathfindingResult . track_section_ranges ,
39
44
} ,
40
- } ;
41
- const { operational_points } = await postPathProperties ( pathPropertiesParams ) . unwrap ( ) ;
45
+ } ) . unwrap ( ) ;
46
+
47
+ let operationalPointsWithAllWaypoints = operational_points ! ;
48
+
49
+ // Check if there are vias added by map click and insert them in the operational points
50
+ let waypointCounter = 1 ;
51
+
52
+ trainScheduleUsedForProjection . path . forEach ( ( step , i ) => {
53
+ if ( ! ( 'track' in step ) ) return ;
54
+
55
+ const positionOnPath = pathfindingResult . path_item_positions [ i ] ;
56
+ const indexToInsert = operationalPointsWithAllWaypoints . findIndex (
57
+ ( op ) => op . position >= positionOnPath
58
+ ) ;
59
+
60
+ const formattedStep : NonNullable < PathProperties [ 'operational_points' ] > [ number ] = {
61
+ id : step . id ,
62
+ extensions : {
63
+ identifier : {
64
+ name : t ( 'requestedPoint' , { count : waypointCounter } ) ,
65
+ uic : 0 ,
66
+ } ,
67
+ } ,
68
+ part : { track : step . track , position : step . offset } ,
69
+ position : positionOnPath ,
70
+ } ;
71
+
72
+ waypointCounter += 1 ;
73
+
74
+ // If we can't find any op position greater than the current step position, we add it at the end
75
+ if ( indexToInsert === - 1 ) {
76
+ operationalPointsWithAllWaypoints . push ( formattedStep ) ;
77
+ return ;
78
+ }
79
+
80
+ operationalPointsWithAllWaypoints = addElementAtIndex (
81
+ operationalPointsWithAllWaypoints ,
82
+ indexToInsert ,
83
+ formattedStep
84
+ ) ;
85
+ } ) ;
42
86
43
- setOperationalPoints (
44
- operational_points as NonNullable < PathProperties [ 'operational_points' ] >
45
- ) ;
87
+ setOperationalPoints ( operationalPointsWithAllWaypoints ) ;
46
88
}
47
89
} ;
48
90
getOperationalPoints ( ) ;
49
- } , [ pathfindingResult , infraId ] ) ;
91
+ } , [ pathfindingResult , infraId , t ] ) ;
50
92
51
93
return operationalPoints ;
52
94
} ;
0 commit comments