Skip to content

Commit

Permalink
fixup! front: select op with map when add train
Browse files Browse the repository at this point in the history
  • Loading branch information
theocrsb committed Dec 24, 2024
1 parent 0c38cde commit 549d3a7
Showing 1 changed file with 67 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<TrackSectionEntity>(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<TrackSectionEntity>(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;
Expand Down

0 comments on commit 549d3a7

Please sign in to comment.