Skip to content

Commit 925f3f5

Browse files
committed
front: use Duration instead of string in PathStep.arrival
Signed-off-by: Simon Ser <[email protected]>
1 parent 41d78a7 commit 925f3f5

File tree

10 files changed

+31
-27
lines changed

10 files changed

+31
-27
lines changed

front/src/applications/operationalStudies/hooks/__tests__/useSetupItineraryForTrainUpdate.spec.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect } from 'vitest';
22

33
import type { PathfindingResult } from 'common/api/osrdEditoastApi';
4+
import { Duration } from 'utils/duration';
45

56
import { updatePathStepsFromOperationalPoints } from '../useSetupItineraryForTrainUpdate';
67

@@ -154,7 +155,7 @@ describe('updatePathStepsFrom', () => {
154155
uic: 87747006,
155156
secondary_code: 'P2',
156157
name: '87747006',
157-
arrival: '15:00:00',
158+
arrival: Duration.parse('PT60S'),
158159
stopFor: null,
159160
},
160161
{
@@ -198,7 +199,7 @@ describe('updatePathStepsFrom', () => {
198199
uic: 87747006,
199200
secondary_code: 'P2', // should not be BV here, it has the same uic but not the same ch
200201
name: 'Grenadille',
201-
arrival: '15:00:00',
202+
arrival: Duration.parse('PT60S'),
202203
stopFor: null,
203204
kp: '129+952',
204205
positionOnPath: 586000,
@@ -234,7 +235,7 @@ describe('updatePathStepsFrom', () => {
234235
trigram: 'GE',
235236
secondary_code: 'P2',
236237
name: '87747006',
237-
arrival: '15:00:00',
238+
arrival: Duration.parse('PT60S'),
238239
},
239240
{
240241
id: 'who-0',
@@ -272,7 +273,7 @@ describe('updatePathStepsFrom', () => {
272273
trigram: 'GE',
273274
secondary_code: 'P2',
274275
name: 'Grenadille',
275-
arrival: '15:00:00',
276+
arrival: Duration.parse('PT60S'),
276277
kp: '129+952',
277278
positionOnPath: 586000,
278279
coordinates: [5.711846462951984, 45.19643525506182],
@@ -303,7 +304,7 @@ describe('updatePathStepsFrom', () => {
303304
trigram: 'GE',
304305
secondary_code: 'P2',
305306
name: '87747006',
306-
arrival: '15:00:00',
307+
arrival: Duration.parse('PT60S'),
307308
},
308309
{
309310
id: 'who-0',
@@ -341,7 +342,7 @@ describe('updatePathStepsFrom', () => {
341342
secondary_code: 'P2',
342343
trigram: 'GE',
343344
name: '87747006',
344-
arrival: '15:00:00',
345+
arrival: Duration.parse('PT60S'),
345346
kp: undefined,
346347
positionOnPath: 586000,
347348
coordinates: [5.711846462951984, 45.19643525506182],

front/src/modules/pathfinding/utils.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { getSupportedElectrification, isThermal } from 'modules/rollingStock/hel
1212
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
1313
import type { PathStep } from 'reducers/osrdconf/types';
1414
import { addElementAtIndex } from 'utils/array';
15-
import { Duration } from 'utils/duration';
1615
import { getPointCoordinates } from 'utils/geometry';
1716

1817
import getStepLocation from './helpers/getStepLocation';
@@ -94,8 +93,7 @@ export const getPathfindingQuery = ({
9493
export const upsertPathStepsInOPs = (ops: SuggestedOP[], pathSteps: PathStep[]): SuggestedOP[] => {
9594
let updatedOPs = [...ops];
9695
pathSteps.forEach((step) => {
97-
const { stopFor, receptionSignal, theoreticalMargin } = step;
98-
const arrival = step.arrival ? Duration.parse(step.arrival) : null;
96+
const { arrival, stopFor, receptionSignal, theoreticalMargin } = step;
9997
// We check only for pathSteps added by map click
10098
if ('track' in step) {
10199
const formattedStep: SuggestedOP = {

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ export const formatSuggestedViasToRowVias = (
7979
const { arrival, receptionSignal, stopFor, theoreticalMargin } = objectToUse || {};
8080

8181
const isMarginValid = theoreticalMargin ? marginRegExValidation.test(theoreticalMargin) : true;
82-
const rawArrivalDuration = i === 0 ? Duration.zero : arrival;
83-
// TODO: turn PathStep.arrival into a Duration instead of a string
84-
const arrivalDuration =
85-
typeof rawArrivalDuration === 'string'
86-
? Duration.parse(rawArrivalDuration)
87-
: rawArrivalDuration;
82+
const arrivalDuration = i === 0 ? Duration.zero : arrival;
8883
const arrivalInSeconds = arrivalDuration ? msToS(arrivalDuration.ms) : null;
8984

9085
const formattedArrival = calculateStepTimeAndDays(startTime, arrivalDuration);

front/src/modules/trainschedule/components/ManageTrainSchedule/helpers/__tests__/formatSchedule.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest';
22

33
import type { PathStep } from 'reducers/osrdconf/types';
4+
import { Duration } from 'utils/duration';
45

56
import formatSchedule from '../formatSchedule';
67

@@ -31,7 +32,7 @@ describe('formatSchedule', () => {
3132
kp: '117+422',
3233
name: 'V',
3334
positionOnPath: 13116000,
34-
arrival: 'PT60S',
35+
arrival: Duration.parse('PT60S'),
3536
stopFor: '0',
3637
locked: false,
3738
receptionSignal: 'OPEN',
@@ -40,7 +41,7 @@ describe('formatSchedule', () => {
4041
const result = formatSchedule(pathSteps);
4142
expect(result).toEqual([
4243
{
43-
arrival: 'PT60S',
44+
arrival: 'PT1M',
4445
at: 'id332',
4546
locked: false,
4647
reception_signal: 'OPEN',

front/src/modules/trainschedule/components/ManageTrainSchedule/helpers/formatSchedule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const formatSchedule = (pathSteps: PathStep[]): TrainScheduleBase['schedule'] =>
99
if (step?.arrival || step.stopFor) {
1010
return {
1111
at: step.id,
12-
arrival: step.arrival ?? undefined,
12+
arrival: step.arrival?.toISOString() ?? undefined,
1313
locked: step.locked,
1414
reception_signal: step.receptionSignal,
1515
stop_for:

front/src/modules/trainschedule/helpers/computeBasePathStep.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TrainScheduleResult } from 'common/api/osrdEditoastApi';
22
import type { PathStep } from 'reducers/osrdconf/types';
3+
import { Duration } from 'utils/duration';
34
import { mmToM } from 'utils/physics';
45
import { ISO8601Duration2sec } from 'utils/timeManipulation';
56

@@ -51,7 +52,7 @@ const computeBasePathStep = (
5152
...step,
5253
...('track' in step ? { offset: mmToM(step.offset) } : null),
5354
name,
54-
arrival, // ISODurationString
55+
arrival: arrival ? Duration.parse(arrival) : null,
5556
stopFor: stopFor ? ISO8601Duration2sec(stopFor).toString() : stopFor,
5657
locked,
5758
receptionSignal,

front/src/reducers/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import simulationReducer, {
3232
import type { SimulationResultsState } from 'reducers/simulationResults/types';
3333
import userReducer, { userInitialState, userSlice } from 'reducers/user';
3434
import type { UserState } from 'reducers/user';
35+
import { Duration } from 'utils/duration';
3536

3637
import type { ConfSlice } from './osrdconf/osrdConfCommon';
3738

@@ -79,14 +80,23 @@ const operationalStudiesDateTransform = createTransform(
7980
}),
8081
{ whitelist: ['operationalStudiesConf'] }
8182
);
83+
const pathStepsTransform = createTransform(
84+
null,
85+
(pathSteps: ({ arrival: string } | null)[]) =>
86+
pathSteps.map((pathStep) => {
87+
if (!pathStep) return null;
88+
return { ...pathStep, arrival: pathStep.arrival ? Duration.parse(pathStep.arrival) : null };
89+
}),
90+
{ whitelist: ['pathSteps'] }
91+
);
8292

8393
// Useful to only blacklist a sub-propertie of osrdconf
8494
const buildOsrdConfPersistConfig = <T extends OperationalStudiesConfState | OsrdStdcmConfState>(
8595
slice: ConfSlice
8696
): PersistConfig<T> => ({
8797
key: slice.name,
8898
storage,
89-
transforms: [stdcmPathStepsDateTransform, operationalStudiesDateTransform],
99+
transforms: [stdcmPathStepsDateTransform, operationalStudiesDateTransform, pathStepsTransform],
90100
});
91101

92102
export const persistConfig = {

front/src/reducers/osrdconf/helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ export function upsertPathStep(statePathSteps: (PathStep | null)[], op: Suggeste
6161
'name',
6262
'kp',
6363
'stopFor',
64+
'arrival',
6465
'locked',
6566
'deleted',
6667
'receptionSignal',
6768
'theoreticalMargin',
6869
]),
6970
id: nextId(),
70-
arrival: op.arrival?.toISOString(),
7171
...(op.uic
7272
? { uic: op.uic, secondary_code: op.ch }
7373
: {

front/src/reducers/osrdconf/operationalStudiesConf/__tests__/operationalStudiesConfReducer.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ describe('simulationConfReducer', () => {
7070
uic: 123,
7171
name: '123',
7272
theoreticalMargin: '10%',
73-
ch: undefined,
74-
arrival: undefined,
73+
arrival: null,
7574
stopFor: undefined,
7675
locked: undefined,
7776
receptionSignal: undefined,
@@ -81,8 +80,7 @@ describe('simulationConfReducer', () => {
8180
uic: 234,
8281
name: '234',
8382
theoreticalMargin: undefined,
84-
ch: undefined,
85-
arrival: undefined,
83+
arrival: null,
8684
stopFor: undefined,
8785
locked: undefined,
8886
receptionSignal: undefined,

front/src/reducers/osrdconf/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import type {
1313
PathItemLocation,
1414
ReceptionSignal,
1515
} from 'common/api/osrdEditoastApi';
16-
import type { IsoDurationString } from 'common/types';
1716
import type { InfraState } from 'reducers/infra';
17+
import type { Duration } from 'utils/duration';
1818

1919
export type OsrdConfState = InfraState & {
2020
projectID?: number;
@@ -56,7 +56,7 @@ export type PathStep = PathItemLocation & {
5656
It's useful for soft deleting the point (waiting to fix / remove all references)
5757
If true, the train schedule is consider as invalid and must be edited */
5858
deleted?: boolean;
59-
arrival?: IsoDurationString | null;
59+
arrival?: Duration | null;
6060
locked?: boolean;
6161
stopFor?: string | null;
6262
theoreticalMargin?: string;

0 commit comments

Comments
 (0)