From 30ff81448764a078e19a200055beb440b21f55f3 Mon Sep 17 00:00:00 2001 From: Clara Ni Date: Tue, 26 Nov 2024 13:01:53 +0100 Subject: [PATCH] fixup! front: refacto useOutputData --- .../helpers/__tests__/scheduleData.spec.ts | 9 ++++----- .../timesStops/helpers/scheduleData.ts | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/front/src/modules/timesStops/helpers/__tests__/scheduleData.spec.ts b/front/src/modules/timesStops/helpers/__tests__/scheduleData.spec.ts index ff81a1afc82..0ee79a53c05 100644 --- a/front/src/modules/timesStops/helpers/__tests__/scheduleData.spec.ts +++ b/front/src/modules/timesStops/helpers/__tests__/scheduleData.spec.ts @@ -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, @@ -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, diff --git a/front/src/modules/timesStops/helpers/scheduleData.ts b/front/src/modules/timesStops/helpers/scheduleData.ts index d74002c68bd..10f7a272fd0 100644 --- a/front/src/modules/timesStops/helpers/scheduleData.ts +++ b/front/src/modules/timesStops/helpers/scheduleData.ts @@ -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, }; };