@@ -4,7 +4,6 @@ import React, { useEffect, useState, useMemo } from 'react';
4
4
5
5
import { Select } from '@osrd-project/ui-core' ;
6
6
import { point } from '@turf/helpers' ;
7
-
8
7
import { useTranslation } from 'react-i18next' ;
9
8
import { IoFlag } from 'react-icons/io5' ;
10
9
import { RiMapPin2Fill , RiMapPin3Fill } from 'react-icons/ri' ;
@@ -17,12 +16,7 @@ import type { TrackSectionEntity } from 'applications/editor/tools/trackEdition/
17
16
import { calculateDistanceAlongTrack } from 'applications/editor/tools/utils' ;
18
17
import { useScenarioContext } from 'applications/operationalStudies/hooks/useScenarioContext' ;
19
18
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types' ;
20
- import {
21
- osrdEditoastApi ,
22
- type OperationalPoint ,
23
- type OperationalPointReference ,
24
- type TrackReference ,
25
- } from 'common/api/osrdEditoastApi' ;
19
+ import { osrdEditoastApi , type OperationalPoint } from 'common/api/osrdEditoastApi' ;
26
20
import { useOsrdConfActions , useOsrdConfSelectors } from 'common/osrdContext' ;
27
21
import { setPointIti } from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/setPointIti' ;
28
22
import { type PathStep } from 'reducers/osrdconf/types' ;
@@ -38,8 +32,6 @@ type RenderPopupProps = {
38
32
pathProperties ?: ManageTrainSchedulePathProperties ;
39
33
} ;
40
34
41
- type SelectOp = Extract < OperationalPointReference , { uic : number } > & { coordinates : number [ ] } ;
42
-
43
35
function RenderPopup ( { pathProperties } : RenderPopupProps ) {
44
36
const { getFeatureInfoClick, getInfraID, getOrigin, getDestination } = useOsrdConfSelectors ( ) ;
45
37
const osrdConfActions = useOsrdConfActions ( ) ;
@@ -58,11 +50,16 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
58
50
const { getTrackSectionsByIds } = useScenarioContext ( ) ;
59
51
60
52
const [ trackOffset , setTrackOffset ] = useState ( 0 ) ;
61
- const [ trackSelected , setTrackSelected ] = useState < TrackReference > ( ) ;
62
- const [ selectOp , setSelectedOp ] = useState < any > ( ) ;
63
- const [ trackOptions , setTrackOptions ] = useState < ( TrackReference & { coordinates : number [ ] } ) [ ] > (
64
- [ ]
65
- ) ;
53
+
54
+ const [ clickedOp , setClickedOp ] = useState <
55
+ PathStep & {
56
+ tracks : {
57
+ trackName : string ;
58
+ coordinates : number [ ] ;
59
+ isSelected : boolean ;
60
+ } [ ] ;
61
+ }
62
+ > ( ) ;
66
63
67
64
const [ getTrackEntity ] =
68
65
osrdEditoastApi . endpoints . postInfraByInfraIdObjectsAndObjectType . useLazyQuery ( ) ;
@@ -76,12 +73,12 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
76
73
)
77
74
return ;
78
75
79
- const trackId = featureInfoClick . feature . properties . id ;
76
+ const objectId = featureInfoClick . feature . properties . id ;
80
77
81
78
const result = await getTrackEntity ( {
82
79
infraId : infraId ! ,
83
80
objectType : isOperationalPoint ? 'OperationalPoint' : 'TrackSection' ,
84
- body : [ trackId ] ,
81
+ body : [ objectId ] ,
85
82
} ) . unwrap ( ) ;
86
83
87
84
if ( ! result . length ) {
@@ -90,32 +87,34 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
90
87
}
91
88
92
89
if ( isOperationalPoint ) {
90
+ const trackId = featureInfoClick . feature . properties . track_id ;
91
+ const clickedTrack = await getTrackEntity ( {
92
+ infraId : infraId ! ,
93
+ objectType : 'TrackSection' ,
94
+ body : [ trackId ] ,
95
+ } ) . unwrap ( ) ;
96
+
93
97
const { parts } = result [ 0 ] . railjson as OperationalPoint ;
94
98
const trackIds = parts . map ( ( part ) => part . track ) ;
95
-
96
99
const tracks = await getTrackSectionsByIds ( trackIds ) ;
97
100
98
101
const trackPartCoordinates = parts . map ( ( step ) => {
99
102
const track = tracks [ step . track ] ;
100
-
101
103
return {
104
+ trackName : track . extensions ?. sncf ?. track_name as string ,
102
105
coordinates : getPointCoordinates ( track . geo , track . length , step . position ) ,
103
- track_name : track . extensions ?. sncf ?. track_name as string ,
106
+ isSelected : step . track === clickedTrack [ 0 ] . obj_id ,
104
107
} ;
105
108
} ) ;
106
109
107
- setSelectedOp ( {
110
+ setClickedOp ( {
111
+ id : nextId ( ) ,
108
112
secondary_code : result [ 0 ] . railjson . extensions . sncf . ch ,
109
113
uic : result [ 0 ] . railjson . extensions . identifier . uic ,
110
- track_reference : trackSelected ,
111
- coordinates : featureInfoClick . coordinates . slice ( 0 , 2 ) ,
114
+ tracks : trackPartCoordinates ,
112
115
} ) ;
113
-
114
- setTrackOptions ( trackPartCoordinates ) ;
115
- }
116
-
117
- setTrackSelected ( undefined ) ;
118
- if ( ! isOperationalPoint ) {
116
+ } else {
117
+ setClickedOp ( undefined ) ;
119
118
// if operationnalPoint we already have coordinates
120
119
const trackEntity = editoastToEditorEntity < TrackSectionEntity > ( result [ 0 ] , 'TrackSection' ) ;
121
120
const offset = calculateDistanceAlongTrack (
@@ -144,10 +143,9 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
144
143
const coordinates = featureInfoClick . coordinates . slice ( 0 , 2 ) ;
145
144
146
145
let pathStepProperties : PathStep ;
147
- if ( isOperationalPoint && selectOp ) {
146
+ if ( isOperationalPoint && clickedOp ) {
148
147
pathStepProperties = {
149
- id : nextId ( ) ,
150
- ...selectOp ,
148
+ ...clickedOp ,
151
149
} ;
152
150
} else {
153
151
pathStepProperties = {
@@ -165,8 +163,6 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
165
163
} as PathStep ;
166
164
}
167
165
168
- console . log ( trackSelected , 'trackSelected' ) ;
169
-
170
166
return (
171
167
< Popup
172
168
longitude = { coordinates [ 0 ] }
@@ -191,14 +187,31 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
191
187
</ div >
192
188
</ div >
193
189
194
- { isOperationalPoint && trackOptions && (
190
+ { isOperationalPoint && clickedOp ?. tracks && (
195
191
< Select
196
- getOptionLabel = { ( option ) => option . track_name }
197
- getOptionValue = { ( option ) => option . track_name }
192
+ getOptionLabel = { ( option ) => option . trackName }
193
+ getOptionValue = { ( option ) => option . trackName }
198
194
id = "select-track"
199
- onChange = { ( selectedOption ) => setTrackSelected ( selectedOption ) }
200
- options = { trackOptions }
201
- value = { trackSelected }
195
+ onChange = { ( selectedOption ) => {
196
+ const updatedTracks = clickedOp . tracks . map ( ( track ) => {
197
+ if ( track . trackName === selectedOption ?. trackName ) {
198
+ return {
199
+ ...track ,
200
+ isSelected : true ,
201
+ } ;
202
+ }
203
+ if ( track . isSelected ) {
204
+ return {
205
+ ...track ,
206
+ isSelected : false ,
207
+ } ;
208
+ }
209
+ return track ;
210
+ } ) ;
211
+ setClickedOp ( { ...clickedOp , tracks : updatedTracks } ) ;
212
+ } }
213
+ options = { clickedOp . tracks }
214
+ value = { clickedOp . tracks . find ( ( track ) => track . isSelected === true ) || clickedOp . tracks [ 0 ] }
202
215
/>
203
216
) }
204
217
0 commit comments