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 19, 2024
1 parent c262cc6 commit f323ca1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
5 changes: 2 additions & 3 deletions front/src/modules/pathfinding/helpers/getStepLocation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { OperationalPointReference, PathItemLocation } from 'common/api/osrdEditoastApi';
import type { PathStep, StdcmPathStep } from 'reducers/osrdconf/types';
import { mToMm } from 'utils/physics';

const getStepLocation = (step: PathStep | StdcmPathStep): PathItemLocation => {
const getStepLocation = (step: PathItemLocation): PathItemLocation => {
const trackReference: OperationalPointReference['track_reference'] =
!('track' in step) && step.track_reference
!('track' in step) && step.track_reference && 'track_name' in step.track_reference
? { track_id: undefined, track_name: step.track_reference.track_name }
: null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ import { calculateDistanceAlongTrack } from 'applications/editor/tools/utils';
import { useManageTrainScheduleContext } from 'applications/operationalStudies/hooks/useManageTrainScheduleContext';
import { useScenarioContext } from 'applications/operationalStudies/hooks/useScenarioContext';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import { osrdEditoastApi, type OperationalPoint } from 'common/api/osrdEditoastApi';
import {
osrdEditoastApi,
type OperationalPoint,
type OperationalPointReference,
} 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';
import { getPointCoordinates } from 'utils/geometry';
import { omit } from 'lodash';

type FeatureInfoClickType = {
displayPopup: boolean;
Expand All @@ -33,7 +38,7 @@ type RenderPopupProps = {
pathProperties?: ManageTrainSchedulePathProperties;
};

function RenderPopup({ pathProperties }: RenderPopupProps) {
const RenderPopup = ({ pathProperties }: RenderPopupProps) => {
const { getFeatureInfoClick, getInfraID, getOrigin, getDestination } = useOsrdConfSelectors();
const { launchPathfinding } = useManageTrainScheduleContext();
const osrdConfActions = useOsrdConfActions();
Expand All @@ -54,7 +59,7 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
const [trackOffset, setTrackOffset] = useState(0);

const [clickedOp, setClickedOp] = useState<
PathStep & {
Extract<PathStep, { uic: number }> & {
tracks: {
trackName?: string;
coordinates?: number[];
Expand Down Expand Up @@ -101,13 +106,13 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
const trackIds = parts.map((part) => part.track);
const tracks = await getTrackSectionsByIds(trackIds);

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

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

Expand Down Expand Up @@ -154,8 +159,16 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {

let pathStepProperties: PathStep;
if (isOperationalPoint && clickedOp) {
const selectedTrack = clickedOp.tracks.find((track) => track.isSelected)!;
const newPathStep: PathStep = {
...omit(clickedOp, ['tracks']),
coordinates: selectedTrack.coordinates,
track_reference: selectedTrack.trackName
? { track_name: selectedTrack.trackName }
: undefined,
};
pathStepProperties = {
...clickedOp,
...newPathStep,
};
} else {
pathStepProperties = {
Expand All @@ -170,7 +183,7 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
trackName: trackProperties.extensions_sncf_track_name,
trackNumber: trackProperties.extensions_sncf_track_number,
},
} as PathStep;
};
}

return (
Expand Down Expand Up @@ -268,6 +281,6 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
</div>
</Popup>
);
}
};

export default React.memo(RenderPopup);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { addElementAtIndex, replaceElementAtIndex } from 'utils/array';

export function setPointIti(
pointType: 'origin' | 'destination' | 'via',
pathStep: PathStep,
// pathStep comes from a map click either on a track section or an operational point
pathStep: Extract<PathStep, { track: string } | { uic: number }>,
actions: ConfSliceActions,
launchPathfinding: (newPathSteps: (PathStep | null)[]) => void,
pathProperties?: ManageTrainSchedulePathProperties
Expand Down
28 changes: 4 additions & 24 deletions front/src/reducers/osrdconf/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import nextId from 'react-id-generator';

import { calculateDistanceAlongTrack } from 'applications/editor/tools/utils';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import type { OperationalPointReference } from 'common/api/osrdEditoastApi';
import { pathStepMatchesOp } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { addElementAtIndex } from 'utils/array';
Expand All @@ -14,16 +13,8 @@ import type { OsrdConfState, PathStep } from './types';

export const insertViaFromMap = (
pathSteps: OsrdConfState['pathSteps'],
// newVia is the pathStep
newVia:
| PathStep
| (PathStep & {
tracks: {
trackName?: string;
coordinates?: number[];
isSelected: boolean;
}[];
}),
// newVia comes from a map click either on a track section or an operational point
newVia: Extract<PathStep, { track: string } | { uic: number }>,
pathProperties: ManageTrainSchedulePathProperties
): OsrdConfState['pathSteps'] => {
const isOp = 'secondary_code' in newVia && 'uic' in newVia;
Expand All @@ -36,8 +27,6 @@ export const insertViaFromMap = (

let newStep;

let coordinates: number[] | undefined;

if ('track' in newVia) {
newStep = {
track: newVia.track,
Expand All @@ -47,21 +36,12 @@ export const insertViaFromMap = (
};
// If the via is an OP we don't need to calculate the coordinates
} else {
const selectedTrackReference = newVia.tracks.find((track) => track.isSelected);

const trackReference: OperationalPointReference['track_reference'] =
selectedTrackReference?.trackName
? { track_name: selectedTrackReference.trackName }
: undefined;

coordinates =
'tracks' in newVia ? newVia.tracks.find((track) => track.isSelected)?.coordinates : [];
newStep = {
uic: newVia.uic,
secondary_code: newVia.secondary_code,
id: newVia.id,
coordinates,
track_reference: trackReference,
coordinates: newVia.coordinates,
track_reference: newVia.track_reference,
};
}

Expand Down

0 comments on commit f323ca1

Please sign in to comment.