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: use Date instead of a string in times and stops helpers #9933

Merged
merged 1 commit into from
Dec 4, 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 @@ -40,7 +40,8 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
const destination = useSelector(getDestination);
const pathSteps = useSelector(getPathSteps);
const constraintDistribution = useSelector(getConstraintDistribution);
const startTime = useSelector(getStartTime);
const rawStartTime = useSelector(getStartTime);
const startTime = useMemo(() => new Date(rawStartTime), [rawStartTime]);

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

Expand Down
2 changes: 1 addition & 1 deletion front/src/modules/timesStops/TimesStopsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const createClearViaButton = ({

type TimesStopsInputProps = {
allWaypoints?: SuggestedOP[];
startTime: string;
startTime: Date;
pathSteps: PathStep[];
};

Expand Down
28 changes: 14 additions & 14 deletions front/src/modules/timesStops/helpers/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('updateDaySinceDeparture', () => {
arrival: { time: '10:00:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T10:00:00';
const startTime = new Date('2024-08-13T10:00:00');
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('updateDaySinceDeparture', () => {
stopFor: '1800',
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T10:00:00';
const startTime = new Date('2024-08-13T10:00:00');
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('updateDaySinceDeparture', () => {
arrival: { time: '01:00:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T00:00:00';
const startTime = new Date('2024-08-13T00:00:00');
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('updateDaySinceDeparture', () => {
arrival: { time: '00:30:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:50:00';
const startTime = new Date('2024-08-13T23:50:00');
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -387,7 +387,7 @@ describe('updateDaySinceDeparture', () => {
arrival: { time: '00:00:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:50:00';
const startTime = new Date('2024-08-13T23:50:00');
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -435,7 +435,7 @@ describe('updateDaySinceDeparture', () => {
arrival: { time: '01:30:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:45:00';
const startTime = new Date('2024-08-13T23:45:00');
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -498,7 +498,7 @@ describe('updateDaySinceDeparture', () => {
arrival: { time: '00:50:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:50:00';
const startTime = new Date('2024-08-13T23:50:00');
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -563,7 +563,7 @@ describe('updateDaySinceDeparture', () => {
arrival: { time: '00:56:00' },
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:50:00';
const startTime = new Date('2024-08-13T23:50:00');
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -643,7 +643,7 @@ describe('updateDaySinceDeparture', () => {
stopFor: '3600',
},
] as TimesStopsInputRow[];
const startTime = '2024-08-13T23:50:00';
const startTime = new Date('2024-08-13T23:50:00');
const result = updateDaySinceDeparture(TimesStopsInputRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -750,7 +750,7 @@ describe('updateDaySinceDeparture', () => {
},
] as TimesStopsInputRow[];

const startTime = '2024-08-13T23:45:00';
const startTime = new Date('2024-08-13T23:45:00');
const result = updateDaySinceDeparture(pathWaypointRows, startTime, {
keepFirstIndexArrival: true,
});
Expand Down Expand Up @@ -812,7 +812,7 @@ describe('updateDaySinceDeparture', () => {

describe('durationSinceStartTime', () => {
it('should return the correct duration', () => {
const startTime = '2023-09-01T10:00:00Z';
const startTime = new Date('2023-09-01T10:00:00Z');
const stepTimeDays = {
time: '20:00:00',
daySinceDeparture: 0,
Expand All @@ -824,7 +824,7 @@ describe('durationSinceStartTime', () => {
});

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

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

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

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

const result = calculateStepTimeAndDays(startTime, isoDuration);
Expand Down
14 changes: 7 additions & 7 deletions front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { round, isEqual, isNil } from 'lodash';
import { keyColumn, createTextColumn } from 'react-datasheet-grid';

import type { ReceptionSignal } from 'common/api/osrdEditoastApi';
import type { IsoDateTimeString, IsoDurationString, TimeString } from 'common/types';
import type { IsoDurationString, TimeString } from 'common/types';
import { matchPathStepAndOp } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { PathStep } from 'reducers/osrdconf/types';
Expand Down Expand Up @@ -42,7 +42,7 @@ export const formatSuggestedViasToRowVias = (
operationalPoints: (SuggestedOP & { isWaypoint?: boolean })[],
pathSteps: PathStep[],
t: TFunction<'timesStops', undefined>,
startTime?: IsoDateTimeString,
startTime?: Date,
tableType?: TableType
): TimesStopsInputRow[] => {
const formattedOps = [...operationalPoints];
Expand Down Expand Up @@ -221,11 +221,11 @@ export function normalizeNullablesInRow(row: TimesStopsInputRow): TimesStopsInpu
*/
export function updateDaySinceDeparture(
pathWaypointRows: TimesStopsInputRow[],
startTime?: IsoDateTimeString,
startTime?: Date,
{ keepFirstIndexArrival = false } = {}
): TimesStopsInputRow[] {
let currentDaySinceDeparture = 0;
let previousTime = startTime ? datetime2sec(new Date(startTime)) : Number.NEGATIVE_INFINITY;
let previousTime = startTime ? datetime2sec(startTime) : Number.NEGATIVE_INFINITY;

return pathWaypointRows.map((pathWaypoint, index) => {
const { arrival, stopFor } = pathWaypoint;
Expand Down Expand Up @@ -282,14 +282,14 @@ export function updateDaySinceDeparture(
}

export function durationSinceStartTime(
startTime?: IsoDateTimeString,
startTime?: Date,
stepTimeDays?: TimeExtraDays
): IsoDurationString | null {
if (!startTime || !stepTimeDays?.time || stepTimeDays?.daySinceDeparture === undefined) {
return null;
}
const start = dayjs(startTime);
const step = dayjs(`${startTime.split('T')[0]}T${stepTimeDays.time}`).add(
const step = dayjs(`${start.format('YYYY-MM-DD')}T${stepTimeDays.time}`).add(
emersion marked this conversation as resolved.
Show resolved Hide resolved
stepTimeDays.daySinceDeparture,
'day'
);
Expand All @@ -299,7 +299,7 @@ export function durationSinceStartTime(
}

export function calculateStepTimeAndDays(
startTime?: IsoDateTimeString | null,
startTime?: Date | null,
isoDuration?: IsoDurationString | null
): TimeExtraDays | undefined {
if (!startTime || !isoDuration) {
Expand Down
Loading