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 10, 2024
1 parent ea07f17 commit c0f0025
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, { useEffect, useState, useMemo } from 'react';

import { Select } from '@osrd-project/ui-core';
import { point } from '@turf/helpers';

import { useTranslation } from 'react-i18next';
import { IoFlag } from 'react-icons/io5';
import { RiMapPin2Fill, RiMapPin3Fill } from 'react-icons/ri';
Expand All @@ -17,12 +16,7 @@ import type { TrackSectionEntity } from 'applications/editor/tools/trackEdition/
import { calculateDistanceAlongTrack } from 'applications/editor/tools/utils';
import { useScenarioContext } from 'applications/operationalStudies/hooks/useScenarioContext';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import {
osrdEditoastApi,
type OperationalPoint,
type OperationalPointReference,
type TrackReference,
} from 'common/api/osrdEditoastApi';
import { osrdEditoastApi, type OperationalPoint } from 'common/api/osrdEditoastApi';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { setPointIti } from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/setPointIti';
import { type PathStep } from 'reducers/osrdconf/types';
Expand All @@ -38,8 +32,6 @@ type RenderPopupProps = {
pathProperties?: ManageTrainSchedulePathProperties;
};

type SelectOp = Extract<OperationalPointReference, { uic: number }> & { coordinates: number[] };

function RenderPopup({ pathProperties }: RenderPopupProps) {
const { getFeatureInfoClick, getInfraID, getOrigin, getDestination } = useOsrdConfSelectors();
const osrdConfActions = useOsrdConfActions();
Expand All @@ -58,11 +50,16 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
const { getTrackSectionsByIds } = useScenarioContext();

const [trackOffset, setTrackOffset] = useState(0);
const [trackSelected, setTrackSelected] = useState<TrackReference>();
const [selectOp, setSelectedOp] = useState<any>();
const [trackOptions, setTrackOptions] = useState<(TrackReference & { coordinates: number[] })[]>(
[]
);

const [clickedOp, setClickedOp] = useState<
PathStep & {
tracks: {
trackName: string;
coordinates: number[];
isSelected: boolean;
}[];
}
>();

const [getTrackEntity] =
osrdEditoastApi.endpoints.postInfraByInfraIdObjectsAndObjectType.useLazyQuery();
Expand All @@ -76,12 +73,12 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
)
return;

const trackId = featureInfoClick.feature.properties.id;
const objectId = featureInfoClick.feature.properties.id;

const result = await getTrackEntity({
infraId: infraId!,
objectType: isOperationalPoint ? 'OperationalPoint' : 'TrackSection',
body: [trackId],
body: [objectId],
}).unwrap();

if (!result.length) {
Expand All @@ -90,32 +87,34 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
}

if (isOperationalPoint) {
const trackId = featureInfoClick.feature.properties.track_id;
const clickedTrack = await getTrackEntity({
infraId: infraId!,
objectType: 'TrackSection',
body: [trackId],
}).unwrap();

const { parts } = result[0].railjson as OperationalPoint;
const trackIds = parts.map((part) => part.track);

const tracks = await getTrackSectionsByIds(trackIds);

const trackPartCoordinates = parts.map((step) => {
const track = tracks[step.track];

return {
trackName: track.extensions?.sncf?.track_name as string,
coordinates: getPointCoordinates(track.geo, track.length, step.position),
track_name: track.extensions?.sncf?.track_name as string,
isSelected: step.track === clickedTrack[0].obj_id,
};
});

setSelectedOp({
setClickedOp({
id: nextId(),
secondary_code: result[0].railjson.extensions.sncf.ch,
uic: result[0].railjson.extensions.identifier.uic,
track_reference: trackSelected,
coordinates: featureInfoClick.coordinates.slice(0, 2),
tracks: trackPartCoordinates,
});

setTrackOptions(trackPartCoordinates);
}

setTrackSelected(undefined);
if (!isOperationalPoint) {
} else {
setClickedOp(undefined);
// if operationnalPoint we already have coordinates
const trackEntity = editoastToEditorEntity<TrackSectionEntity>(result[0], 'TrackSection');
const offset = calculateDistanceAlongTrack(
Expand Down Expand Up @@ -144,10 +143,9 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
const coordinates = featureInfoClick.coordinates.slice(0, 2);

let pathStepProperties: PathStep;
if (isOperationalPoint && selectOp) {
if (isOperationalPoint && clickedOp) {
pathStepProperties = {
id: nextId(),
...selectOp,
...clickedOp,
};
} else {
pathStepProperties = {
Expand All @@ -165,8 +163,6 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
} as PathStep;
}

console.log(trackSelected, 'trackSelected');

return (
<Popup
longitude={coordinates[0]}
Expand All @@ -191,14 +187,31 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
</div>
</div>

{isOperationalPoint && trackOptions && (
{isOperationalPoint && clickedOp?.tracks && (
<Select
getOptionLabel={(option) => option.track_name}
getOptionValue={(option) => option.track_name}
getOptionLabel={(option) => option.trackName}
getOptionValue={(option) => option.trackName}
id="select-track"
onChange={(selectedOption) => setTrackSelected(selectedOption)}
options={trackOptions}
value={trackSelected}
onChange={(selectedOption) => {
const updatedTracks = clickedOp.tracks.map((track) => {
if (track.trackName === selectedOption?.trackName) {
return {
...track,
isSelected: true,
};
}
if (track.isSelected) {
return {
...track,
isSelected: false,
};
}
return track;
});
setClickedOp({ ...clickedOp, tracks: updatedTracks });
}}
options={clickedOp.tracks}
value={clickedOp.tracks.find((track) => track.isSelected === true) || clickedOp.tracks[0]}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ const Map = ({
result.feature.properties.id &&
(result.feature.geometry.type === 'LineString' || result.feature.geometry.type === 'Point')
) {
//
if (result.feature.geometry.type === 'Point') {
console.log('Click on PR in Map.tsx');
}
//
dispatch(
updateFeatureInfoClick({
displayPopup: true,
Expand Down
2 changes: 0 additions & 2 deletions front/src/reducers/osrdconf/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export const insertViaFromMap = (
)
return pathSteps;

console.log(newVia, ' : le nouveau via');

const origin = pathSteps[0];
const destination = last(pathSteps);
let newStep;
Expand Down

0 comments on commit c0f0025

Please sign in to comment.