Skip to content

Commit 1cf4a5c

Browse files
committed
front: use Date instead of a string in times and stops helpers
Improves type safety. Signed-off-by: Simon Ser <[email protected]>
1 parent 7601a70 commit 1cf4a5c

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

front/src/applications/operationalStudies/views/ManageTrainSchedule.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
4040
const destination = useSelector(getDestination);
4141
const pathSteps = useSelector(getPathSteps);
4242
const constraintDistribution = useSelector(getConstraintDistribution);
43-
const startTime = useSelector(getStartTime);
43+
const rawStartTime = useSelector(getStartTime);
44+
const startTime = useMemo(() => new Date(rawStartTime), [rawStartTime]);
4445

4546
const [pathProperties, setPathProperties] = useState<ManageTrainSchedulePathProperties>();
4647

front/src/modules/timesStops/TimesStopsInput.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const createClearViaButton = ({
5959

6060
type TimesStopsInputProps = {
6161
allWaypoints?: SuggestedOP[];
62-
startTime: string;
62+
startTime: Date;
6363
pathSteps: PathStep[];
6464
};
6565

front/src/modules/timesStops/helpers/__tests__/utils.spec.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ describe('updateDaySinceDeparture', () => {
227227
arrival: { time: '10:00:00' },
228228
},
229229
] as TimesStopsInputRow[];
230-
const startTime = '2024-08-13T10:00:00';
230+
const startTime = new Date('2024-08-13T10:00:00');
231231
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
232232
keepFirstIndexArrival: true,
233233
});
@@ -261,7 +261,7 @@ describe('updateDaySinceDeparture', () => {
261261
stopFor: '1800',
262262
},
263263
] as TimesStopsInputRow[];
264-
const startTime = '2024-08-13T10:00:00';
264+
const startTime = new Date('2024-08-13T10:00:00');
265265
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
266266
keepFirstIndexArrival: true,
267267
});
@@ -303,7 +303,7 @@ describe('updateDaySinceDeparture', () => {
303303
arrival: { time: '01:00:00' },
304304
},
305305
] as TimesStopsInputRow[];
306-
const startTime = '2024-08-13T00:00:00';
306+
const startTime = new Date('2024-08-13T00:00:00');
307307
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
308308
keepFirstIndexArrival: true,
309309
});
@@ -346,7 +346,7 @@ describe('updateDaySinceDeparture', () => {
346346
arrival: { time: '00:30:00' },
347347
},
348348
] as TimesStopsInputRow[];
349-
const startTime = '2024-08-13T23:50:00';
349+
const startTime = new Date('2024-08-13T23:50:00');
350350
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
351351
keepFirstIndexArrival: true,
352352
});
@@ -387,7 +387,7 @@ describe('updateDaySinceDeparture', () => {
387387
arrival: { time: '00:00:00' },
388388
},
389389
] as TimesStopsInputRow[];
390-
const startTime = '2024-08-13T23:50:00';
390+
const startTime = new Date('2024-08-13T23:50:00');
391391
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
392392
keepFirstIndexArrival: true,
393393
});
@@ -435,7 +435,7 @@ describe('updateDaySinceDeparture', () => {
435435
arrival: { time: '01:30:00' },
436436
},
437437
] as TimesStopsInputRow[];
438-
const startTime = '2024-08-13T23:45:00';
438+
const startTime = new Date('2024-08-13T23:45:00');
439439
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
440440
keepFirstIndexArrival: true,
441441
});
@@ -498,7 +498,7 @@ describe('updateDaySinceDeparture', () => {
498498
arrival: { time: '00:50:00' },
499499
},
500500
] as TimesStopsInputRow[];
501-
const startTime = '2024-08-13T23:50:00';
501+
const startTime = new Date('2024-08-13T23:50:00');
502502
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
503503
keepFirstIndexArrival: true,
504504
});
@@ -563,7 +563,7 @@ describe('updateDaySinceDeparture', () => {
563563
arrival: { time: '00:56:00' },
564564
},
565565
] as TimesStopsInputRow[];
566-
const startTime = '2024-08-13T23:50:00';
566+
const startTime = new Date('2024-08-13T23:50:00');
567567
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
568568
keepFirstIndexArrival: true,
569569
});
@@ -643,7 +643,7 @@ describe('updateDaySinceDeparture', () => {
643643
stopFor: '3600',
644644
},
645645
] as TimesStopsInputRow[];
646-
const startTime = '2024-08-13T23:50:00';
646+
const startTime = new Date('2024-08-13T23:50:00');
647647
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
648648
keepFirstIndexArrival: true,
649649
});
@@ -750,7 +750,7 @@ describe('updateDaySinceDeparture', () => {
750750
},
751751
] as TimesStopsInputRow[];
752752

753-
const startTime = '2024-08-13T23:45:00';
753+
const startTime = new Date('2024-08-13T23:45:00');
754754
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
755755
keepFirstIndexArrival: true,
756756
});
@@ -812,7 +812,7 @@ describe('updateDaySinceDeparture', () => {
812812

813813
describe('durationSinceStartTime', () => {
814814
it('should return the correct duration', () => {
815-
const startTime = '2023-09-01T10:00:00Z';
815+
const startTime = new Date('2023-09-01T10:00:00Z');
816816
const stepTimeDays = {
817817
time: '20:00:00',
818818
daySinceDeparture: 0,
@@ -824,7 +824,7 @@ describe('durationSinceStartTime', () => {
824824
});
825825

826826
it('should return the correct duration. daySinceDeparture 1', () => {
827-
const startTime = '2023-09-01T10:00:00Z';
827+
const startTime = new Date('2023-09-01T10:00:00Z');
828828
const stepTimeDays = {
829829
time: '11:00:00',
830830
daySinceDeparture: 1,
@@ -838,7 +838,7 @@ describe('durationSinceStartTime', () => {
838838

839839
describe('calculateStepTimeDays', () => {
840840
it('should return correct time and daySinceDeparture', () => {
841-
const startTime = '2023-09-01T10:00:00Z';
841+
const startTime = new Date('2023-09-01T10:00:00Z');
842842
const isoDuration = 'PT36000S'; // 10 hours
843843

844844
const result = calculateStepTimeAndDays(startTime, isoDuration);
@@ -850,7 +850,7 @@ describe('calculateStepTimeDays', () => {
850850
});
851851

852852
it('should return correct time and daySinceDeparture, daySinceDeparture 1', () => {
853-
const startTime = '2023-09-01T10:00:00Z';
853+
const startTime = new Date('2023-09-01T10:00:00Z');
854854
const isoDuration = 'PT122400S'; // 1 day 10 hours
855855

856856
const result = calculateStepTimeAndDays(startTime, isoDuration);

front/src/modules/timesStops/helpers/utils.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { round, isEqual, isNil } from 'lodash';
55
import { keyColumn, createTextColumn } from 'react-datasheet-grid';
66

77
import type { ReceptionSignal } from 'common/api/osrdEditoastApi';
8-
import type { IsoDateTimeString, IsoDurationString, TimeString } from 'common/types';
8+
import type { IsoDurationString, TimeString } from 'common/types';
99
import { matchPathStepAndOp } from 'modules/pathfinding/utils';
1010
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
1111
import type { PathStep } from 'reducers/osrdconf/types';
@@ -42,7 +42,7 @@ export const formatSuggestedViasToRowVias = (
4242
operationalPoints: (SuggestedOP & { isWaypoint?: boolean })[],
4343
pathSteps: PathStep[],
4444
t: TFunction<'timesStops', undefined>,
45-
startTime?: IsoDateTimeString,
45+
startTime?: Date,
4646
tableType?: TableType
4747
): TimesStopsInputRow[] => {
4848
const formattedOps = [...operationalPoints];
@@ -221,11 +221,11 @@ export function normalizeNullablesInRow(row: TimesStopsInputRow): TimesStopsInpu
221221
*/
222222
export function updateDaySinceDeparture(
223223
pathWaypointRows: TimesStopsInputRow[],
224-
startTime?: IsoDateTimeString,
224+
startTime?: Date,
225225
{ keepFirstIndexArrival = false } = {}
226226
): TimesStopsInputRow[] {
227227
let currentDaySinceDeparture = 0;
228-
let previousTime = startTime ? datetime2sec(new Date(startTime)) : Number.NEGATIVE_INFINITY;
228+
let previousTime = startTime ? datetime2sec(startTime) : Number.NEGATIVE_INFINITY;
229229

230230
return pathWaypointRows.map((pathWaypoint, index) => {
231231
const { arrival, stopFor } = pathWaypoint;
@@ -282,14 +282,14 @@ export function updateDaySinceDeparture(
282282
}
283283

284284
export function durationSinceStartTime(
285-
startTime?: IsoDateTimeString,
285+
startTime?: Date,
286286
stepTimeDays?: TimeExtraDays
287287
): IsoDurationString | null {
288288
if (!startTime || !stepTimeDays?.time || stepTimeDays?.daySinceDeparture === undefined) {
289289
return null;
290290
}
291291
const start = dayjs(startTime);
292-
const step = dayjs(`${startTime.split('T')[0]}T${stepTimeDays.time}`).add(
292+
const step = dayjs(`${start.format('YYYY-MM-DD')}T${stepTimeDays.time}`).add(
293293
stepTimeDays.daySinceDeparture,
294294
'day'
295295
);
@@ -299,7 +299,7 @@ export function durationSinceStartTime(
299299
}
300300

301301
export function calculateStepTimeAndDays(
302-
startTime?: IsoDateTimeString | null,
302+
startTime?: Date | null,
303303
isoDuration?: IsoDurationString | null
304304
): TimeExtraDays | undefined {
305305
if (!startTime || !isoDuration) {

0 commit comments

Comments
 (0)