Skip to content

Commit

Permalink
front: fix displayed op on manchette
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Macron <theo.macron0315@gmail.com>
  • Loading branch information
Akctarus committed Jan 20, 2025
1 parent c4c4c58 commit 3f15094
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('upsertMapWaypointsInOperationalPoints', () => {
position: 7746000,
},
position: 9246000,
weight: null,
weight: 100,
},
{
id: 'Mid_West_station',
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('upsertMapWaypointsInOperationalPoints', () => {
position: 6481000,
},
position: 0,
weight: null,
weight: 100,
},
{
id: 'Mid_West_station',
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('upsertMapWaypointsInOperationalPoints', () => {
position: 679000,
},
position: 4198000,
weight: null,
weight: 100,
},
{
id: '3',
Expand All @@ -157,7 +157,7 @@ describe('upsertMapWaypointsInOperationalPoints', () => {
position: 883000,
},
position: 4402000,
weight: null,
weight: 100,
},
]);
});
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('upsertMapWaypointsInOperationalPoints', () => {
position: 6481000,
},
position: 0,
weight: null,
weight: 100,
},
{
id: '2',
Expand All @@ -201,7 +201,7 @@ describe('upsertMapWaypointsInOperationalPoints', () => {
position: 4733000,
},
position: 1748000,
weight: null,
weight: 100,
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { PathfindingResultSuccess, TrainScheduleResult } from 'common/api/o

import type { OperationalPoint } from '../types';

const HIGHEST_PRIORITY_WEIGHT = 100;

/**
* Check if the train path used waypoints added by map click and add them to the operational points
*/
Expand All @@ -18,35 +20,56 @@ export const upsertMapWaypointsInOperationalPoints = (

return path.reduce(
(operationalPointsWithAllWaypoints, step, i) => {
if (!('track' in step)) return operationalPointsWithAllWaypoints;

const positionOnPath = pathItemsPositions[i];
const indexToInsert = operationalPointsWithAllWaypoints.findIndex(
(op) => op.position >= positionOnPath
);

const formattedStep: OperationalPoint = {
id: step.id,
extensions: {
identifier: {
name: t('requestedPoint', { count: waypointCounter }),
uic: 0,
},
},
part: { track: step.track, position: step.offset },
position: positionOnPath,
weight: null,
};

waypointCounter += 1;

// If we can't find any op position greater than the current step position, we add it at the end
if (indexToInsert === -1) {
operationalPointsWithAllWaypoints.push(formattedStep);
} else {
operationalPointsWithAllWaypoints.splice(indexToInsert, 0, formattedStep);
if ('uic' in step) {
const matchedIndex = operationalPointsWithAllWaypoints.findIndex(
(op) =>
'uic' in step &&
'secondary_code' in step &&
step.uic === op.extensions?.identifier?.uic &&
step.secondary_code === op.extensions?.sncf?.ch
);

if (matchedIndex !== -1) {
// Replace the operational point at its original index with updated weight
operationalPointsWithAllWaypoints[matchedIndex] = {
...operationalPointsWithAllWaypoints[matchedIndex],
weight: HIGHEST_PRIORITY_WEIGHT,
};
}

return operationalPointsWithAllWaypoints;
}

if ('track' in step) {
const positionOnPath = pathItemsPositions[i];
const indexToInsert = operationalPointsWithAllWaypoints.findIndex(
(op) => op.position >= positionOnPath
);

const formattedStep: OperationalPoint = {
id: step.id,
extensions: {
identifier: {
name: t('requestedPoint', { count: waypointCounter }),
uic: 0,
},
},
part: { track: step.track, position: step.offset },
position: positionOnPath,
weight: HIGHEST_PRIORITY_WEIGHT,
};

waypointCounter += 1;

// If we can't find any op position greater than the current step position, we add it at the end
if (indexToInsert === -1) {
operationalPointsWithAllWaypoints.push(formattedStep);
} else {
operationalPointsWithAllWaypoints.splice(indexToInsert, 0, formattedStep);
}

return operationalPointsWithAllWaypoints;
}
return operationalPointsWithAllWaypoints;
},
[...operationalPoints]
Expand Down
3 changes: 0 additions & 3 deletions front/src/modules/pathfinding/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,4 @@ export const isVia = (
{ withKP = false } = {}
) => vias.some((via) => pathStepMatchesOp(via, op, withKP));

export const isStation = (chCode: string): boolean =>
chCode === 'BV' || chCode === '00' || chCode === '';

export const isPathStepInvalid = (step: PathStep | null): boolean => step?.isInvalid || false;
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const ManchetteWithSpaceTimeChartWrapper = ({
position: waypoint.position,
name: waypoint.extensions?.identifier?.name,
secondaryCode: waypoint.extensions?.sncf?.ch,
weight: waypoint.weight ?? 0,
}));
}, [waypointsPanelData, operationalPoints]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { isStation } from 'modules/pathfinding/utils';

const useGetProjectedTrainOperationalPoints = (
trainScheduleUsedForProjection?: TrainScheduleResult,
Expand Down Expand Up @@ -76,12 +75,6 @@ const useGetProjectedTrainOperationalPoints = (
operationalPointsWithUniqueIds = JSON.parse(stringifiedSavedWaypoints) as NonNullable<
PathProperties['operational_points']
>;
} else {
// If the manchette hasn't been saved, we want to display by default only
// the waypoints with CH BV/00/'' and the ones added by map click
operationalPointsWithUniqueIds = operationalPointsWithUniqueIds.filter((op) =>
op.extensions?.sncf ? isStation(op.extensions.sncf.ch) : true
);
}
setFilteredOperationalPoints(operationalPointsWithUniqueIds);
}
Expand Down

0 comments on commit 3f15094

Please sign in to comment.