Skip to content

Commit

Permalink
fixup! front: refacto useOutputData
Browse files Browse the repository at this point in the history
  • Loading branch information
clarani committed Nov 26, 2024
1 parent 09d66f6 commit 30ff814
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { formatSchedule } from '../scheduleData';

describe('formatScheduleTime', () => {
it('should return empty objecty if schedule is undefined', () => {
const startDatetime = new Date();
const arrivalTime = new Date();

expect(formatSchedule(startDatetime)).toEqual({
expect(formatSchedule(arrivalTime)).toEqual({
stopFor: '',
shortSlipDistance: false,
onStopSignal: false,
Expand All @@ -17,15 +17,14 @@ describe('formatScheduleTime', () => {
});

it('should compute simple arrival time in the correct timezone', () => {
const startDatetime = new Date('2022-01-01T02:03:00Z');
const arrivalTime = new Date('2022-01-01T02:03:00');
const schedule = {
at: 'id325',
arrival: 'PT3600S',
stop_for: 'PT100S',
reception_signal: 'OPEN' as ReceptionSignal,
};

expect(formatSchedule(startDatetime, schedule)).toEqual({
expect(formatSchedule(arrivalTime, schedule)).toEqual({
calculatedDeparture: '02:04:40',
stopFor: '100',
shortSlipDistance: false,
Expand Down
19 changes: 13 additions & 6 deletions front/src/modules/timesStops/helpers/scheduleData.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
// eslint-disable import/prefer-default-export
import { dateToHHMMSS } from 'utils/date';
import { ISO8601Duration2sec } from 'utils/timeManipulation';

import type { ScheduleEntry } from '../types';
import { receptionSignalToSignalBooleans } from './utils';

// eslint-disable-next-line import/prefer-default-export
/** Format the stopFor, calculatedDeparture, shortSlipDistance and onStopSignal properties */
export const formatSchedule = (arrivalTime: Date, schedule?: ScheduleEntry) => {
if (!schedule) {
return {
stopFor: '',
calculatedDeparture: undefined,
shortSlipDistance: false,
onStopSignal: false,
};
}

if (!schedule.stop_for) {
return {
stopFor: '',
calculatedDeparture: undefined,
...receptionSignalToSignalBooleans(schedule.reception_signal),
};
}

const stopForSeconds = schedule.stop_for ? ISO8601Duration2sec(schedule.stop_for) : null;
const stopForSeconds = ISO8601Duration2sec(schedule.stop_for);

return {
stopFor: stopForSeconds !== null ? `${stopForSeconds}` : null,
stopFor: `${stopForSeconds}`,
calculatedDeparture: dateToHHMMSS(new Date(arrivalTime.getTime() + stopForSeconds * 1000)),
...receptionSignalToSignalBooleans(schedule.reception_signal),
calculatedDeparture: stopForSeconds
? dateToHHMMSS(new Date(arrivalTime.getTime() + stopForSeconds * 1000))
: undefined,
};
};

0 comments on commit 30ff814

Please sign in to comment.