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 10, 2024
1 parent c0781ef commit 1e07d20
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import type { Margin } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { PathStep } from 'reducers/osrdconf/types';

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

pathSteps.forEach((step, index) => {
if (index === 0) {
margins.values.push(step.theoreticalMargin || 'none');
} else if (step.theoreticalMargin !== pathSteps[index - 1].theoreticalMargin) {
margins.boundaries.push(step.id);
margins.values.push(step.theoreticalMargin || 'none');
values.push(step.theoreticalMargin || 'none');
} else if (index === pathSteps.length - 1) {
if (values.length === 1 && values[0] !== 'none') {
boundaries.push(step.id);
values.push('none');
}
}

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

if (values.length === 1) {
return undefined;
}
return { boundaries, values };
};

export default formatMargin;

0 comments on commit 1e07d20

Please sign in to comment.