Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
anisometropie committed Sep 17, 2024
1 parent d4003c1 commit 3355564
Show file tree
Hide file tree
Showing 22 changed files with 203 additions and 92 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+{{count}}",
"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+{{count}}",
"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', { count: daySinceDeparture })}`
: time;
return <div className="read-only-time">{fullString}</div>;
};

ReadOnlyTime.displayName = 'ReadOnlyTime';

export default ReadOnlyTime;
12 changes: 7 additions & 5 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,16 +56,17 @@ const TimeInput = ({ focus, rowData, active, setRowData }: TimeInputProps) => {
return (
<div className="time-input-container">
{input}
<span className="extra-text">
{t('d+')}
{tempTimeValue.daySinceDeparture}
<span
className={cx('extra-text', {
'extra-text-firefox': navigator.userAgent.search('Firefox') !== -1,
})}
>
{t('dayCounter', { count: tempTimeValue.daySinceDeparture })}
</span>
</div>
);
}
return input;
};

TimeInput.displayName = 'TimeInput';

export default TimeInput;
19 changes: 10 additions & 9 deletions front/src/modules/timesStops/TimesStops.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react';

import cx from 'classnames';
import { omit } from 'lodash';
import { isEqual } from 'lodash';
import { DynamicDataSheetGrid, type DataSheetGridProps } from 'react-datasheet-grid';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -92,17 +92,18 @@ const TimesStops = ({
);
updatedRows = updateDaySinceDeparture(updatedRows, startTime);
if (!updatedRows[op.fromRowIndex].isMarginValid) {
newRows[op.fromRowIndex].isMarginValid = false;
setRows(newRows);
} else {
const newVias = updatedRows
.filter((row) => row.arrival)
.map((row) => {
const arrival = durationSinceStartTime(startTime, row.arrival);
return {
...omit(row, 'departure'),
arrival,
};
});
.filter((row, index) => !isEqual(row, rows[index]))
.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
94 changes: 88 additions & 6 deletions front/src/modules/timesStops/helpers/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('updateRowTimesAndMargin', () => {
});
});
});
describe('theoritical margin is incorrect', () => {
describe('theoretical margin is incorrect', () => {
it('should set isMarginValid flag to false', () => {
const rowData = {
opId: 'd94a2af4',
Expand All @@ -54,8 +54,8 @@ describe('updateRowTimesAndMargin', () => {
expect(result.isMarginValid).toBe(false);
});
});
describe('user removed first row theoritical margin', () => {
it('should set the theoritical margin back to 0%', () => {
describe('user removed first row theoretical margin', () => {
it('should set the theoretical margin back to 0%', () => {
const rowData = {
opId: 'd94a2af4',
name: 'Gr',
Expand Down Expand Up @@ -106,9 +106,91 @@ describe('updateRowTimesAndMargin', () => {
});
});
});
describe('arrival, departure & stopFor are set, arrival gets erased', () => {
it('should keep stopFor and remove departure', () => {
const rowData = {
opId: 'd94a2af4',
name: 'Gr',
arrival: undefined,
departure: { time: '00:20:00' },
stopFor: '600',
} as PathWaypointRow;
const previousRowData = {
opId: 'd94a2af4',
name: 'Gr',
arrival: { time: '00:10:00' },
departure: { time: '00:20:00' },
stopFor: '600',
} as PathWaypointRow;
const result = updateRowTimesAndMargin(rowData, previousRowData, whateverOperation, 4);
expect(result).toEqual({
opId: 'd94a2af4',
name: 'Gr',
arrival: undefined,
departure: undefined,
stopFor: '600',
isMarginValid: true,
});
});
});
describe('arrival, departure & stopFor are set, departure gets erased', () => {
it('should keep arrival and remove stopFor', () => {
const rowData = {
opId: 'd94a2af4',
name: 'Gr',
arrival: { time: '00:10:00' },
departure: undefined,
stopFor: '600',
} as PathWaypointRow;
const previousRowData = {
opId: 'd94a2af4',
name: 'Gr',
arrival: { time: '00:10:00' },
departure: { time: '00:20:00' },
stopFor: '600',
} as PathWaypointRow;
const result = updateRowTimesAndMargin(rowData, previousRowData, whateverOperation, 4);
expect(result).toEqual({
opId: 'd94a2af4',
name: 'Gr',
arrival: { time: '00:10:00' },
departure: undefined,
stopFor: undefined,
isMarginValid: true,
onStopSignal: false
});
});
});
describe('stopFor only is set, departure gets added', () => {
it('should set arrival too', () => {
const rowData = {
opId: 'd94a2af4',
name: 'Gr',
arrival: undefined,
departure: { time: '00:20:00' },
stopFor: '600',
} as PathWaypointRow;
const previousRowData = {
opId: 'd94a2af4',
name: 'Gr',
arrival: undefined,
departure: undefined,
stopFor: '600',
} as PathWaypointRow;
const result = updateRowTimesAndMargin(rowData, previousRowData, whateverOperation, 4);
expect(result).toEqual({
opId: 'd94a2af4',
name: 'Gr',
arrival: { time: '00:10:00' },
departure: { time: '00:20:00' },
stopFor: '600',
isMarginValid: true,
});
});
});
});

describe('updateTimeAndDays', () => {
describe('updateDaySinceDeparture', () => {
describe('1 day span', () => {
it('should add the day since departure', () => {
const pathWaypointRows = [
Expand Down Expand Up @@ -479,7 +561,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 +573,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
4 changes: 2 additions & 2 deletions front/src/modules/timesStops/helpers/arrivalTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export function checkAndFormatCalculatedArrival(
operationalPointTime: number
) {
if (!scheduleData.arrival) {
return secToHoursString(operationalPointTime, true);
return secToHoursString(operationalPointTime, { withSeconds: true });
}
const arrivalValuesAreClose =
Math.abs(scheduleData.arrival - (operationalPointTime % SECONDS_IN_A_DAY)) <=
ARRIVAL_TIME_ACCEPTABLE_ERROR_MS / 1000;
const calculatedArrival = arrivalValuesAreClose ? scheduleData.arrival : operationalPointTime;

return secToHoursString(calculatedArrival, true);
return secToHoursString(calculatedArrival, { withSeconds: true });
}
10 changes: 5 additions & 5 deletions front/src/modules/timesStops/helpers/computeMargins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ function getTheoreticalMargin(selectedTrainSchedule: TrainScheduleResult, pathSt
if (selectedTrainSchedule.path[0].id === pathStepId) {
return selectedTrainSchedule.margins?.values[0];
}
const theoriticalMarginBoundaryIndex = selectedTrainSchedule.margins?.boundaries?.findIndex(
const theoreticalMarginBoundaryIndex = selectedTrainSchedule.margins?.boundaries?.findIndex(
(id) => id === pathStepId
);
if (
theoriticalMarginBoundaryIndex === undefined ||
theoriticalMarginBoundaryIndex < 0 ||
theoriticalMarginBoundaryIndex > selectedTrainSchedule.margins!.values.length - 2
theoreticalMarginBoundaryIndex === undefined ||
theoreticalMarginBoundaryIndex < 0 ||
theoreticalMarginBoundaryIndex > selectedTrainSchedule.margins!.values.length - 2
) {
return undefined;
}

return selectedTrainSchedule.margins!.values[theoriticalMarginBoundaryIndex + 1];
return selectedTrainSchedule.margins!.values[theoreticalMarginBoundaryIndex + 1];
}

function computeDuration(
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
Loading

0 comments on commit 3355564

Please sign in to comment.