Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: fix trains not stopping at the end of path #7880

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,21 @@ const Pathfinding = ({ pathProperties, setPathProperties }: PathfindingProps) =>
'uic' in step && suggestedOp.uic === step.uic && suggestedOp.ch === step.ch
);

const stopFor = i === pathSteps.length - 1 && !step.stopFor ? '0' : step.stopFor;

return correspondingOp
? {
...step,
kp: correspondingOp.kp,
coordinates: correspondingOp.coordinates,
positionOnPath: pathfindingResult.path_items_positions[i],
stopFor,
}
: { ...step, positionOnPath: pathfindingResult.path_items_positions[i] };
: {
...step,
positionOnPath: pathfindingResult.path_items_positions[i],
stopFor,
};
});
dispatch(updatePathSteps(updatedPathSteps));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ const TypeAndPathV2 = ({ setPathProperties }: PathfindingProps) => {
kp: correspondingOp.kp,
coordinates: correspondingOp.coordinates,
positionOnPath: pathfindingResult.path_items_positions[i],
stopFor: i === opList.length - 1 ? '0' : undefined,
};
});
dispatch(updatePathSteps(pathSteps));
Expand Down
5 changes: 0 additions & 5 deletions front/src/utils/__tests__/timeManipulation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,4 @@ describe('ISO8601Duration2sec', () => {
it('should handle hours and seconds without min', () => {
expect(ISO8601Duration2sec('PT1H1S')).toEqual(3601);
});
it('should throw if the duration is not correctly formatted', () => {
expect(() => ISO8601Duration2sec('test')).toThrow(
new Error('Invalid ISO 8601 duration format')
);
});
});
16 changes: 7 additions & 9 deletions front/src/utils/timeManipulation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as d3 from 'd3';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';

import type { TimeString } from 'common/types';

dayjs.extend(duration);

export function sec2ms(sec: number) {
return sec * 1000;
}
Expand Down Expand Up @@ -78,15 +82,9 @@ export function formatDurationAsISO8601(seconds: number) {
/**
* Parse ISO8601 duration, for instance "PT11H9M8S" (11h, 9min and 8s) to seconds
*/
export function ISO8601Duration2sec(duration: string) {
const regex = /PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/;
const matches = duration.match(regex);
if (!matches) {
throw new Error('Invalid ISO 8601 duration format');
}

const [hours, minutes, seconds] = matches.slice(1).map((match) => parseInt(match || '0', 10));
return hours * 60 * 60 + minutes * 60 + seconds;
export function ISO8601Duration2sec(isoDuration: string) {
const durationDictionnary = dayjs.duration(isoDuration);
return durationDictionnary.asSeconds();
}

export function getStopTime(sec: number) {
Expand Down
Loading