Skip to content

Commit

Permalink
front: fix handleinvalidpathitems logic in usepathfinding to handle a…
Browse files Browse the repository at this point in the history
…ll invalid path step types

Signed-off-by: Achraf Mohyeddine <[email protected]>
  • Loading branch information
achrafmohye committed Jan 23, 2025
1 parent 37a6c2f commit 8c32833
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
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 @@ -117,11 +118,12 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
uic = step.uic;
} else if ('trigram' in step) {
op = ops.find((o) => o.trigram === step.trigram);
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 || uic?.toString()}`;
name = `${op?.name || uic?.toString() || trigram}`;
}

return {
Expand Down
22 changes: 5 additions & 17 deletions front/src/modules/pathfinding/hooks/usePathfinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +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),
}));

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

0 comments on commit 8c32833

Please sign in to comment.