diff --git a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/AddPathStepPopup.tsx b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/AddPathStepPopup.tsx index 9059079a643..0bda13bb48d 100644 --- a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/AddPathStepPopup.tsx +++ b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/AddPathStepPopup.tsx @@ -67,12 +67,12 @@ const AddPathStepPopup = ({ osrdEditoastApi.endpoints.postInfraByInfraIdObjectsAndObjectType.useLazyQuery(); useEffect(() => { - const calculateOffset = async () => { + const handleTrack = async () => { const objectId = featureInfoClick.feature.properties?.id; const result = await getInfraObjectEntity({ infraId: infraId!, - objectType: isOperationalPoint ? 'OperationalPoint' : 'TrackSection', + objectType: 'TrackSection', body: [objectId], }).unwrap(); @@ -81,55 +81,74 @@ const AddPathStepPopup = ({ return; } - if (isOperationalPoint) { - const trackId = featureInfoClick.feature.properties?.track_id; - - const clickedTrack = await getInfraObjectEntity({ - infraId: infraId!, - objectType: 'TrackSection', - body: [trackId], - }).unwrap(); - - const operationalPoint = result[0].railjson as OperationalPoint; - const trackIds = operationalPoint.parts.map((part) => part.track); - const tracks = await getTrackSectionsByIds(trackIds); - - const trackPartCoordinates = operationalPoint.parts.map((part) => { - const track = tracks[part.track]; - - return { - trackName: track.extensions?.sncf?.track_name, - coordinates: getPointCoordinates(track.geo, track.length, part.position), - isSelected: part.track === clickedTrack[0].obj_id, - }; - }); - - trackPartCoordinates.unshift({ - trackName: undefined, - coordinates: result[0].geographic.coordinates as number[], - isSelected: false, - }); - - setClickedOp({ - id: nextId(), - secondary_code: operationalPoint.extensions!.sncf!.ch, - uic: operationalPoint.extensions!.identifier!.uic, - tracks: trackPartCoordinates, - }); - } else { - setClickedOp(undefined); - // if operationnalPoint we already have coordinates - const trackEntity = editoastToEditorEntity(result[0], 'TrackSection'); - const offset = calculateDistanceAlongTrack( - trackEntity, - point(featureInfoClick.coordinates.slice(0, 2)).geometry, - 'millimeters' - ); - setTrackOffset(offset); + setClickedOp(undefined); + // if operationnalPoint we already have coordinates + const trackEntity = editoastToEditorEntity(result[0], 'TrackSection'); + const offset = calculateDistanceAlongTrack( + trackEntity, + point(featureInfoClick.coordinates.slice(0, 2)).geometry, + 'millimeters' + ); + setTrackOffset(offset); + }; + + const handleOperationalPoint = async () => { + const objectId = featureInfoClick.feature.properties?.id; + + const result = await getInfraObjectEntity({ + infraId: infraId!, + objectType: 'OperationalPoint', + body: [objectId], + }).unwrap(); + + if (!result.length) { + console.error('No operational point found'); + return; } + + const trackId = featureInfoClick.feature.properties?.track_id; + + const clickedTrack = await getInfraObjectEntity({ + infraId: infraId!, + objectType: 'TrackSection', + body: [trackId], + }).unwrap(); + + const operationalPoint = result[0].railjson as OperationalPoint; + const trackIds = operationalPoint.parts.map((part) => part.track); + const tracks = await getTrackSectionsByIds(trackIds); + + const trackPartCoordinates = operationalPoint.parts.map((part) => { + const track = tracks[part.track]; + + return { + trackName: track.extensions?.sncf?.track_name, + coordinates: getPointCoordinates(track.geo, track.length, part.position), + isSelected: part.track === clickedTrack[0].obj_id, + }; + }); + + trackPartCoordinates.unshift({ + trackName: undefined, + coordinates: result[0].geographic.coordinates as number[], + isSelected: false, + }); + + setClickedOp({ + id: nextId(), + secondary_code: operationalPoint.extensions!.sncf!.ch, + uic: operationalPoint.extensions!.identifier!.uic, + tracks: trackPartCoordinates, + }); }; - calculateOffset(); + setClickedOp(undefined); + + if (isOperationalPoint) { + handleOperationalPoint(); + } else { + handleTrack(); + } }, [featureInfoClick]); if (!featureInfoClick.feature.properties) return null;