Skip to content

Commit

Permalink
front: handle both invalid trigrams and uics as invalid op
Browse files Browse the repository at this point in the history
Signed-off-by: Achraf Mohyeddine <[email protected]>
  • Loading branch information
achrafmohye committed Jan 29, 2025
1 parent 9ddc06c commit 6021ad9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type TrainScheduleResult,
type PathfindingResult,
type SearchResultItemOperationalPoint,
type PathfindingInputError,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
import buildOpSearchQuery from 'modules/operationalPoint/helpers/buildOpSearchQuery';
Expand Down Expand Up @@ -74,8 +75,13 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
const [postSearch] = osrdEditoastApi.endpoints.postSearch.useMutation();

useEffect(() => {
const fetchPathStepsCoordinates = async (trainSchedule: TrainScheduleResult) => {
// get track sections
const fetchPathStepsCoordinates = async (
trainSchedule: TrainScheduleResult,
invalidPathItems: Extract<
PathfindingInputError,
{ error_type: 'invalid_path_items' }
>['items'] = []
) => {
const trackSectionIds: string[] = [];
trainSchedule.path.forEach((step) => {
if ('track' in step) {
Expand All @@ -97,6 +103,8 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
const pathStepsWithCoordinates = trainSchedule.path.map((step, index) => {
let coordinates: Position | undefined;
let name: string | undefined;
let uic: number | undefined;
let trigram: string | undefined;

if ('track' in step) {
const track = tracks[step.track];
Expand All @@ -107,19 +115,22 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
let op: SearchResultItemOperationalPoint | undefined;
if ('uic' in step) {
op = ops.find((o) => o.uic === step.uic);
uic = step.uic;
} else if ('trigram' in step) {
op = ops.find((o) => o.trigram === step.trigram);
} else {
trigram = step.trigram;
} else if ('operational_point' in step) {
op = ops.find((o) => o.obj_id === step.operational_point);
}
coordinates = op?.geographic.coordinates;
name = `${op?.name}`;
name = `${op?.name || uic?.toString() || trigram}`;
}

return {
...computeBasePathStep(trainSchedule, index),
coordinates,
name,
isInvalid: invalidPathItems.some((item) => item.index === index),
};
});

Expand Down Expand Up @@ -153,7 +164,13 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
};
const pathfindingResult = await postPathfindingBlocks(params).unwrap();
if (pathfindingResult.status !== 'success') {
fetchPathStepsCoordinates(trainSchedule);
const invalidPathItems =
pathfindingResult.failed_status === 'pathfinding_input_error' &&
pathfindingResult.error_type === 'invalid_path_items'
? pathfindingResult.items
: [];

fetchPathStepsCoordinates(trainSchedule, invalidPathItems);
return null;
}
const pathPropertiesParams: PostInfraByInfraIdPathPropertiesApiArg = {
Expand Down
21 changes: 5 additions & 16 deletions front/src/modules/pathfinding/hooks/usePathfinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,12 @@ const usePathfinding = (
steps: PathStep[],
invalidPathItems: Extract<PathfindingInputError, { error_type: 'invalid_path_items' }>['items']
) => {
// TODO: we currently only handle invalid pathSteps with trigram. We will have to do it for trackOffset, opId and uic too.
const invalidTrigrams = invalidPathItems
.map((item) => {
if ('trigram' in item.path_item) {
return item.path_item.trigram;
}
return null;
})
.filter((trigram): trigram is string => trigram !== null);
if (invalidTrigrams.length > 0) {
const updatedPathSteps = steps.map((step) => {
if (step && 'trigram' in step && invalidTrigrams.includes(step.trigram)) {
return { ...step, isInvalid: true };
}
return step;
});
const updatedPathSteps = steps.map((step, index) => ({
...step,
isInvalid: invalidPathItems.some((item) => item.index === index),
}));

if (invalidPathItems.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
launchPathfinding(updatedPathSteps);
} else {
Expand Down

0 comments on commit 6021ad9

Please sign in to comment.