Skip to content

Commit

Permalink
fixe
Browse files Browse the repository at this point in the history
  • Loading branch information
anisometropie committed Sep 13, 2024
1 parent ba274bb commit 8f648aa
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 58 deletions.
2 changes: 1 addition & 1 deletion front/public/locales/en/timesStops.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"arrivalTime": "Requested arrival Time",
"calculatedArrivalTime": "Calculated arrival time",
"calculatedDepartureTime": "Calculated departure time",
"d+": "D+",
"dayCounter": "D+",
"departureTime": "Requested departure Time",
"diffMargins": "Margins diff.",
"name": "Name",
Expand Down
2 changes: 1 addition & 1 deletion front/public/locales/fr/timesStops.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"arrivalTime": "Arrivée demandée",
"calculatedArrivalTime": "Arrivée calculée",
"calculatedDepartureTime": "Départ calculé",
"d+": "J+",
"dayCounter": "J+",
"departureTime": "Départ demandé",
"diffMargins": "Diff. marges",
"name": "Nom",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const computeBasePathSteps = (trainSchedule: TrainScheduleResult) =>
...stepWithoutSecondaryCode,
ch: 'secondary_code' in step ? step.secondary_code : undefined,
name,
arrival,
arrival, // ISODurationString
stopFor: stopFor ? ISO8601Duration2sec(stopFor).toString() : stopFor,
locked,
onStopSignal,
Expand Down
12 changes: 6 additions & 6 deletions front/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export type TimeString = string;
*/
export type IsoDateTimeString = string;

export type RangedValue = {
begin: number;
end: number;
value: string;
};

/**
* A ISO 8601 duration string
* @example "PT3600S"
*/
export type IsoDurationString = string;

export type RangedValue = {
begin: number;
end: number;
value: string;
};
20 changes: 9 additions & 11 deletions front/src/modules/timesStops/ReadOnlyTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ import { NO_BREAK_SPACE } from 'utils/strings';

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

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

const ReadOnlyTime = ({ rowData }: ReadOnlyTimeProps) => {
const { t } = useTranslation('timesStops');
const { time, daySinceDeparture, dayDisplayed } = rowData || {};
if (time) {
const fullString =
daySinceDeparture !== undefined && dayDisplayed
? `${time}${NO_BREAK_SPACE}${t('d+')}${daySinceDeparture}`
: time;
return <div className="read-only-time">{fullString}</div>;
if (!time) {
return null;
}
return null;
const { t } = useTranslation('timesStops');
const fullString =
daySinceDeparture !== undefined && dayDisplayed
? `${time}${NO_BREAK_SPACE}${t('dayCounter')}${daySinceDeparture}`
: time;
return <div className="read-only-time">{fullString}</div>;
};

ReadOnlyTime.displayName = 'ReadOnlyTime';

export default ReadOnlyTime;
11 changes: 7 additions & 4 deletions front/src/modules/timesStops/TimeInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRef, useState, useEffect } from 'react';

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

Expand Down Expand Up @@ -55,8 +56,12 @@ const TimeInput = ({ focus, rowData, active, setRowData }: TimeInputProps) => {
return (
<div className="time-input-container">
{input}
<span className="extra-text">
{t('d+')}
<span
className={cx('extra-text', {
'extra-text-firefox': navigator.userAgent.search('Firefox') !== -1,
})}
>
{t('dayCounter')}
{tempTimeValue.daySinceDeparture}
</span>
</div>
Expand All @@ -65,6 +70,4 @@ const TimeInput = ({ focus, rowData, active, setRowData }: TimeInputProps) => {
return input;
};

TimeInput.displayName = 'TimeInput';

export default TimeInput;
13 changes: 7 additions & 6 deletions front/src/modules/timesStops/TimesStops.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ const TimesStops = ({
} else {
const newVias = updatedRows
.filter((row, index) => !isEqual(row, rows[index]))
.map((row) => {
return {
...row,
...(row.arrival && { arrival: durationSinceStartTime(startTime, row.arrival) }),
} as SuggestedOP;
});
.map(
(row) =>
({
...row,
...(row.arrival && { arrival: durationSinceStartTime(startTime, row.arrival) }),
}) as SuggestedOP
);
dispatch(upsertSeveralViasFromSuggestedOP(newVias));
}
}}
Expand Down
1 change: 1 addition & 0 deletions front/src/modules/timesStops/TimesStopsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type TimesStopsInputProps = {
startTime: string;
pathSteps: PathStep[];
};

const TimesStopsinput = ({ allWaypoints, startTime, pathSteps }: TimesStopsInputProps) => {
const dispatch = useAppDispatch();
const { updatePathSteps } = useOsrdConfActions();
Expand Down
2 changes: 1 addition & 1 deletion front/src/modules/timesStops/TimesStopsOutput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const TimesStopsOutput = ({
tableType={TableType.Output}
cellClassName={({ rowData: rowData_ }) => {
const rowData = rowData_ as PathWaypointRow;
const arrivalScheduleNotRespected = rowData?.arrival?.time
const arrivalScheduleNotRespected = rowData.arrival?.time
? rowData.calculatedArrival !== rowData.arrival.time
: false;
const negativeDiffMargins = Number(rowData.diffMargins?.split(NO_BREAK_SPACE)[0]) < 0;
Expand Down
6 changes: 3 additions & 3 deletions front/src/modules/timesStops/helpers/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('updateRowTimesAndMargin', () => {
});
});

describe('updateTimeAndDays', () => {
describe('updateDaySinceDeparture', () => {
describe('1 day span', () => {
it('should add the day since departure', () => {
const pathWaypointRows = [
Expand Down Expand Up @@ -479,7 +479,7 @@ describe('durationSinceStartTime', () => {
describe('calculateStepTimeDays', () => {
it('should return correct time and daySinceDeparture', () => {
const startTime = '2023-09-01T10:00:00Z';
const isoDuration = 'PT36000S';
const isoDuration = 'PT36000S'; // 10 hours

const result = calculateStepTimeAndDays(startTime, isoDuration);

Expand All @@ -491,7 +491,7 @@ describe('calculateStepTimeDays', () => {

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

const result = calculateStepTimeAndDays(startTime, isoDuration);

Expand Down
10 changes: 7 additions & 3 deletions front/src/modules/timesStops/helpers/scheduleData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IsoDurationString } from 'common/types';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { ISO8601Duration2sec, formatDurationAsISO8601 } from 'utils/timeManipulation';

Expand All @@ -6,7 +7,6 @@ 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) {
if (!schedule) {
Expand All @@ -27,8 +27,12 @@ export function computeScheduleData(schedule: ScheduleEntry) {
export function formatScheduleData(
scheduleData: ComputedScheduleEntry
): Pick<SuggestedOP, 'arrival' | 'departure' | 'stopFor'> {
const arrival = scheduleData.arrival ? formatDurationAsISO8601(scheduleData.arrival) : null;
const departure = scheduleData.departure ? formatDurationAsISO8601(scheduleData.departure) : null;
const arrival: IsoDurationString | null = scheduleData.arrival
? formatDurationAsISO8601(scheduleData.arrival)
: null;
const departure: IsoDurationString | null = scheduleData.departure
? formatDurationAsISO8601(scheduleData.departure)
: null;
const stopFor = scheduleData.stopFor !== null ? String(scheduleData.stopFor) : '';
return {
arrival,
Expand Down
24 changes: 13 additions & 11 deletions front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { TFunction } from 'i18next';
import { round, isEqual } from 'lodash';
import { keyColumn, createTextColumn } from 'react-datasheet-grid';

import type { IsoDateTimeString, IsoDurationString } from 'common/types';
import type { IsoDateTimeString, IsoDurationString, TimeString } from 'common/types';
import { matchPathStepAndOp } from 'modules/pathfinding/utils';
import type { OperationalPointWithTimeAndSpeed } from 'modules/trainschedule/components/DriverTrainSchedule/types';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
Expand Down Expand Up @@ -70,16 +70,18 @@ export const formatSuggestedViasToRowVias = (
const { arrival, onStopSignal, stopFor, theoreticalMargin } = objectToUse || {};

const isMarginValid = theoreticalMargin ? marginRegExValidation.test(theoreticalMargin) : true;
const roundedArrivalTime = i === 0 ? 'PT0S' : arrival;
const arrivalInSeconds = roundedArrivalTime ? time2sec(roundedArrivalTime) : null;
const durationArrivalTime = i === 0 ? 'PT0S' : arrival;
const arrivalInSeconds = durationArrivalTime ? time2sec(durationArrivalTime) : null;

const formattedArrival = calculateStepTimeAndDays(startTime, roundedArrivalTime);
const formattedArrival = calculateStepTimeAndDays(startTime, durationArrivalTime);

const departureTime =
stopFor && arrivalInSeconds
? secToHoursString(arrivalInSeconds + Number(stopFor), true)
: undefined;
const formattedDeparture = departureTime ? { time: departureTime } : undefined;
const formattedDeparture: TimeExtraDays | undefined = departureTime
? { time: departureTime }
: undefined;
return {
...op,
isMarginValid,
Expand Down Expand Up @@ -186,11 +188,11 @@ export function updateRowTimesAndMargin(
* This function goes through the whole array of path waypoints
* and updates the number of days since departure.
*/
export function updateDaySinceDeparture<T extends Pick<PathWaypointRow, 'arrival' | 'stopFor'>>(
pathWaypointRows: T[],
export function updateDaySinceDeparture(
pathWaypointRows: PathWaypointRow[],
startTime?: IsoDateTimeString,
keepFirstIndexArrival?: boolean
): T[] {
): PathWaypointRow[] {
let currentDaySinceDeparture = 0;
let previousTime = startTime ? datetime2sec(new Date(startTime)) : Number.NEGATIVE_INFINITY;

Expand Down Expand Up @@ -272,9 +274,9 @@ export function calculateStepTimeAndDays(
const start = dayjs(startTime);
const duration = dayjs.duration(isoDuration);

const endTime = start.add(duration);
const daySinceDeparture = endTime.diff(start, 'day');
const time = endTime.format('HH:mm:ss');
const waypointArrivalTime = start.add(duration);
const daySinceDeparture = waypointArrivalTime.diff(start, 'day');
const time: TimeString = waypointArrivalTime.format('HH:mm:ss');

return {
time,
Expand Down
5 changes: 5 additions & 0 deletions front/src/modules/timesStops/styles/_timeInput.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
top: 0.075rem;
pointer-events: none;
}

span.extra-text-firefox {
top: 0.03rem;
left: 6.1rem;
}
}
7 changes: 4 additions & 3 deletions front/src/modules/timesStops/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { TrainScheduleBase, TrainScheduleResult } from 'common/api/osrdEditoastApi';
import type { TimeString } from 'common/types';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { ArrayElement } from 'utils/types';

export type TimeExtraDays = {
time: string | null | undefined;
time: TimeString | null | undefined;
daySinceDeparture?: number;
dayDisplayed?: boolean;
};

export type PathWaypointRow = Omit<SuggestedOP, 'arrival' | 'departure'> & {
isMarginValid: boolean;
arrival?: TimeExtraDays | undefined; // value asked by user
departure?: TimeExtraDays | undefined; // value asked by user
arrival?: TimeExtraDays; // value asked by user
departure?: TimeExtraDays; // value asked by user
};

export enum TableType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import formatSchedule from '../formatSchedule';
describe('formatSchedule', () => {
describe('same day', () => {
it('should ignore steps without arrival or stopFor', () => {
const pathSteps = [
const pathSteps: PathStep[] = [
{
id: 'id331',
deleted: false,
Expand All @@ -17,12 +17,12 @@ describe('formatSchedule', () => {
name: 'G',
positionOnPath: 0,
},
] as PathStep[];
];
const result = formatSchedule(pathSteps);
expect(result?.length).toBe(0);
});
it('should format the train schedule', () => {
const pathSteps = [
const pathSteps: PathStep[] = [
{
id: 'id332',
deleted: false,
Expand All @@ -36,7 +36,7 @@ describe('formatSchedule', () => {
locked: false,
onStopSignal: false,
},
] as PathStep[];
];
const result = formatSchedule(pathSteps);
expect(result).toEqual([
{
Expand Down
6 changes: 3 additions & 3 deletions front/src/reducers/osrdconf/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ export const updateDestinationPathStep = (
) => updatePathStepAtIndex(pathSteps, pathSteps.length - 1, destination, replaceCompletely);

/**
* modifies the array statePathSteps in place
* Modifies the array statePathSteps in place in the reducer
*/
export function upsertPathStep(statePathSteps: (PathStep | null)[], op: SuggestedOP) {
// We know that, at this point, origin and destination are defined because pathfinding has been done
const cleanPathSteps = compact(statePathSteps);

let newVia = {
let newVia: PathStep = {
...pick(op, [
'coordinates',
'positionOnPath',
Expand All @@ -174,7 +174,7 @@ export function upsertPathStep(statePathSteps: (PathStep | null)[], op: Suggeste
track: op.track,
offset: op.offsetOnTrack,
}),
} as PathStep;
};

const stepIndex = cleanPathSteps.findIndex((step) => pathStepMatchesOp(step, op));
if (stepIndex >= 0) {
Expand Down

0 comments on commit 8f648aa

Please sign in to comment.