Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
anisometropie committed Sep 3, 2024
1 parent 4fef754 commit c2ef063
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 57 deletions.
1 change: 1 addition & 0 deletions front/public/locales/en/timesStops.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"arrivalTime": "Requested arrival Time",
"calculatedArrivalTime": "Calculated arrival time",
"calculatedDepartureTime": "Calculated departure time",
"d+": "D+",
"departureTime": "Requested departure Time",
"diffMargins": "Margins diff.",
"name": "Name",
Expand Down
1 change: 1 addition & 0 deletions front/public/locales/fr/timesStops.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"arrivalTime": "Arrivée demandée",
"calculatedArrivalTime": "Arrivée calculée",
"calculatedDepartureTime": "Départ calculé",
"d+": "J+",
"departureTime": "Départ demandé",
"diffMargins": "Diff. marges",
"name": "Nom",
Expand Down
6 changes: 5 additions & 1 deletion front/src/modules/timesStops/ReadOnlyTime.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import React from 'react';

import type { CellProps } from 'react-datasheet-grid/dist/types';
import { useTranslation } from 'react-i18next';

import type { TimeExtraDays } from './types';

type ReadOnlyTimeProps = CellProps<TimeExtraDays | null | undefined, string>;

const ReadOnlyTime = ({ rowData }: ReadOnlyTimeProps) => {
const { t } = useTranslation('timesStops');
const { time, daySinceDeparture, dayDisplayed } = rowData || {};
if (time) {
const fullString =
daySinceDeparture !== undefined && dayDisplayed ? `${time} J+${daySinceDeparture}` : time;
daySinceDeparture !== undefined && dayDisplayed
? `${time} ${t('d+')}${daySinceDeparture}`
: time;
return <div className="read-only-time">{fullString}</div>;
}
return null;
Expand Down
7 changes: 6 additions & 1 deletion front/src/modules/timesStops/TimeInput.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, { useRef, useState, useEffect } from 'react';

import type { CellProps } from 'react-datasheet-grid/dist/types';
import { useTranslation } from 'react-i18next';

import type { TimeExtraDays } from './types';

type TimeInputProps = CellProps<TimeExtraDays | null | undefined, string>;

const TimeInput = ({ focus, rowData, active, setRowData }: TimeInputProps) => {
const { t } = useTranslation('timesStops');
const ref = useRef<HTMLInputElement>(null);
const [tempTimeValue, setTempTimeValue] = useState<TimeExtraDays | null | undefined>(rowData);

Expand Down Expand Up @@ -53,7 +55,10 @@ const TimeInput = ({ focus, rowData, active, setRowData }: TimeInputProps) => {
return (
<div className="time-input-container">
{input}
<span className="extra-text">J+{tempTimeValue.daySinceDeparture}</span>
<span className="extra-text">
{t('d+')}
{tempTimeValue.daySinceDeparture}
</span>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,12 @@ describe('computeScheduleData', () => {
on_stop_signal: false,
locked: false,
};
const startTime = '2024-05-14T00:00:00Z';

expect(computeScheduleData(schedule, startTime)).toEqual({
expect(computeScheduleData(schedule)).toEqual({
arrival: 3600,
departure: 3700,
stopFor: 100,
});
});
it('should compute simple arrival time in the correct timezone 2', () => {
const schedule = {
at: 'id325',
arrival: 'PT3600S',
stop_for: 'PT100S',
on_stop_signal: false,
locked: false,
};
const startTime = '2024-05-14T01:00:00Z';

expect(computeScheduleData(schedule, startTime)).toEqual({
arrival: 7200,
departure: 7300,
stopFor: 100,
});
});
});
describe('after midnight', () => {
it('should compute simple arrival time in the correct timezone', () => {
const schedule = {
at: 'id325',
arrival: 'PT3600S',
stop_for: 'PT100S',
on_stop_signal: false,
locked: false,
};
const startTime = '2024-05-14T23:50:00Z';

expect(computeScheduleData(schedule, startTime)).toEqual({
arrival: 3000,
departure: 3100,
stopFor: 100,
});
});
});
});
23 changes: 6 additions & 17 deletions front/src/modules/timesStops/helpers/scheduleData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import {
ISO8601Duration2sec,
SECONDS_IN_A_DAY,
datetime2sec,
formatDurationAsISO8601,
} from 'utils/timeManipulation';
import { ISO8601Duration2sec, formatDurationAsISO8601 } from 'utils/timeManipulation';

import type { ComputedScheduleEntry, ScheduleEntry } from '../types';

Expand All @@ -13,24 +8,18 @@ import type { ComputedScheduleEntry, ScheduleEntry } from '../types';
* @param schedule for a given operational point
* @param startTime time of departure from the very beginning of the route
*/
export function computeScheduleData(schedule: ScheduleEntry, startTime: string) {
export function computeScheduleData(schedule: ScheduleEntry) {
if (!schedule) {
return { arrival: null, departure: null, stopFor: null };
}
const startTimeSeconds = datetime2sec(new Date(startTime));
// relative value, number of seconds since startTime
const arrivalSeconds = schedule.arrival
? (startTimeSeconds + ISO8601Duration2sec(schedule.arrival)) % SECONDS_IN_A_DAY
: null;

const arrivalSeconds = schedule.arrival ? ISO8601Duration2sec(schedule.arrival) : null;
const stopForSeconds = schedule.stop_for ? ISO8601Duration2sec(schedule.stop_for) : null;
const departureSeconds =
arrivalSeconds && stopForSeconds ? arrivalSeconds + stopForSeconds : null;

const departure =
arrivalSeconds && stopForSeconds ? (arrivalSeconds + stopForSeconds) % SECONDS_IN_A_DAY : null;
console.log(schedule, startTime, arrivalSeconds, departure, stopForSeconds);
return {
arrival: arrivalSeconds,
departure,
departure: departureSeconds,
stopFor: stopForSeconds,
};
}
Expand Down
2 changes: 1 addition & 1 deletion front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import dayjs from 'dayjs';
import type { TFunction } from 'i18next';
import { round, isEqual } from 'lodash';
Expand Down Expand Up @@ -34,7 +35,6 @@ export const formatSuggestedViasToRowVias = (
startTime?: IsoDateTimeString,
tableType?: TableType
): PathWaypointRow[] => {
console.log(operationalPoints);
const formattedOps = [...operationalPoints];

// If the origin is in the ops and isn't the first operational point, we need
Expand Down
3 changes: 2 additions & 1 deletion front/src/modules/timesStops/hooks/useOutputTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ function useOutputTableData(
if (pathStepKey in pathStepsByUic) {
const pathStepId = pathStepsByUic[pathStepKey].id || '';
const schedule = scheduleByAt[pathStepId];
const scheduleData = computeScheduleData(schedule, selectedTrainSchedule.start_time);
// in SuggestedOP arrival and departure are the number of seconds since departure
const scheduleData = computeScheduleData(schedule);
const formattedScheduleData = formatScheduleData(scheduleData);
const marginsData = nextOpPoint
? computeMargins(
Expand Down

0 comments on commit c2ef063

Please sign in to comment.