Skip to content

Commit

Permalink
front: change margin logic
Browse files Browse the repository at this point in the history
  • Loading branch information
theocrsb committed Jul 8, 2024
1 parent 4dfa713 commit 1c8ece3
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
import type { Margin } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { PathStep } from 'reducers/osrdconf/types';

const formatMargin = (pathSteps: PathStep[]): Margin => {
const formatMargin = (pathSteps: PathStep[]): Margin | undefined => {
const margins: Margin = {
boundaries: [],
values: [],
};

// variable to check if the via has a margin
let viaWithMargin = false;

pathSteps.forEach((step, index) => {
// for the first step, we add the margin or 'none' if it doesn't exist
if (index === 0) {
margins.values.push(step.theoreticalMargin || 'none');
} else if (step.theoreticalMargin !== pathSteps[index - 1].theoreticalMargin) {
}

// for the other steps, we add the margin if it's different from the previous one
else if (
step.theoreticalMargin &&
step.theoreticalMargin !== pathSteps[index - 1].theoreticalMargin &&
index !== pathSteps.length - 1
) {
viaWithMargin = true;
margins.boundaries.push(step.id);
margins.values.push(step.theoreticalMargin || 'none');
margins.values.push(step.theoreticalMargin);
}

// for the last step, if the origin has a margin and there isn't any via with one,
// we add 'none' add the end of the values because we need N+1 values compared to boundaries
else if (index === pathSteps.length - 1 && pathSteps[0].theoreticalMargin && !viaWithMargin) {
margins.boundaries.push(step.id);
margins.values.push('none');
}
});

// check if all margins are 'none'
if (!margins.values.some((value) => value !== 'none')) {
return undefined;
}
return margins;
};

Expand Down

0 comments on commit 1c8ece3

Please sign in to comment.