Skip to content

Commit

Permalink
front: refacto stops table
Browse files Browse the repository at this point in the history
  • Loading branch information
anisometropie committed Jul 2, 2024
1 parent fd6ba79 commit de8f44c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 86 deletions.
77 changes: 76 additions & 1 deletion front/src/modules/timesStops/TimeColumnComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React, { useEffect, useRef, useState } from 'react';

import type { CellComponent, CellProps, Column } from 'react-datasheet-grid/dist/types';
import cx from 'classnames';
import type { TFunction } from 'i18next';
import { keyColumn, type Column, checkboxColumn, createTextColumn } from 'react-datasheet-grid';
import type { CellComponent, CellProps } from 'react-datasheet-grid/dist/types';

import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import { marginRegExValidation } from 'utils/physics';

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

const TimeComponent = ({
focus,
Expand Down Expand Up @@ -62,4 +70,71 @@ const timeColumn: Partial<Column<string | null | undefined, string, string>> = {
isCellEmpty: ({ rowData }) => !rowData,
};

export const inputColumns = (
t: TFunction<'timesStops', undefined>,
pathProperties: ManageTrainSchedulePathProperties
) =>
[
{
...keyColumn('name', createTextColumn()),
title: t('name'),
disabled: true,
},
{
...keyColumn('ch', createTextColumn()),
title: 'Ch',
disabled: true,
grow: 0.1,
},
{
...keyColumn('arrival', timeColumn),
title: t('arrivalTime'),

// We should not be able to edit the arrival time of the origin
disabled: ({ rowIndex }) => rowIndex === 0,
grow: 0.6,
},
{
...keyColumn(
'stopFor',
createTextColumn({
continuousUpdates: false,
alignRight: true,
})
),
title: `${t('stopTime')}`,
grow: 0.6,
},
{
...keyColumn('onStopSignal', checkboxColumn as Partial<Column<boolean | undefined>>),
title: t('receptionOnClosedSignal'),

// We should not be able to edit the reception on close signal if stopFor is not filled
// except for the destination
grow: 0.6,
disabled: ({ rowData, rowIndex }) =>
rowIndex !== pathProperties.allWaypoints?.length - 1 && !rowData.stopFor,
},
{
...keyColumn(
'theoreticalMargin',
createTextColumn({
continuousUpdates: false,
alignRight: true,
placeholder: t('theoreticalMarginPlaceholder'),
formatBlurredInput: (value) => {
if (!value || value === 'none') return '';
if (!marginRegExValidation.test(value)) {
return `${value}${t('theoreticalMarginPlaceholder')}`;
}
return value;
},
})
),
cellClassName: ({ rowData }) => cx({ invalidCell: !rowData.isMarginValid }),
title: t('theoreticalMargin'),
disabled: ({ rowIndex }) => rowIndex === pathProperties.allWaypoints.length - 1,
},
] as Column<PathWaypointColumn>[];

export default timeColumn;
96 changes: 14 additions & 82 deletions front/src/modules/timesStops/TimesStops.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import React, { useMemo, useState, useEffect } from 'react';

import {
keyColumn,
type Column,
checkboxColumn,
createTextColumn,
DynamicDataSheetGrid,
} from 'react-datasheet-grid';
import { type Column, DynamicDataSheetGrid } from 'react-datasheet-grid';
import { useTranslation } from 'react-i18next';

import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
Expand All @@ -16,9 +10,9 @@ import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSc
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { removeElementAtIndex } from 'utils/array';
import { marginRegExValidation } from 'utils/physics';

import { marginRegExValidation } from './consts';
import timeColumn from './TimeColumnComponent';
import { inputColumns } from './TimeColumnComponent';
import type { PathWaypointColumn } from './types';
import { formatSuggestedViasToRowVias } from './utils';

Expand All @@ -30,19 +24,21 @@ type TimesStopsProps = {
startTime?: string;
};

type DeleteButtonProps = {
removeVia: () => void;
rowIndex: number;
rowData: PathWaypointColumn;
pathProperties: ManageTrainSchedulePathProperties;
pathSteps: PathStep[];
};

const createDeleteViaButton = ({
removeVia,
rowIndex,
rowData,
pathProperties,
pathSteps,
}: {
removeVia: () => void;
rowIndex: number;
rowData: PathWaypointColumn;
pathProperties: ManageTrainSchedulePathProperties;
pathSteps: PathStep[];
}) => {
}: DeleteButtonProps) => {
const isRowVia =
rowIndex !== 0 &&
rowIndex !== pathProperties.allWaypoints?.length - 1 &&
Expand Down Expand Up @@ -84,72 +80,8 @@ const TimesStops = ({ pathProperties, pathSteps = [], startTime }: TimesStopsPro
setTimesStopsSteps(suggestedOPs);
}, [t, pathProperties.allWaypoints, startTime]);

const columns: Column<PathWaypointColumn>[] = useMemo(
() => [
{
...keyColumn<PathWaypointColumn, 'name'>('name', createTextColumn()),
title: t('name'),
disabled: true,
},
{
...keyColumn<PathWaypointColumn, 'ch'>('ch', createTextColumn()),
title: 'Ch',
disabled: true,
grow: 0.1,
},
{
...keyColumn<PathWaypointColumn, 'arrival'>('arrival', timeColumn),
title: t('arrivalTime'),

// We should not be able to edit the arrival time of the origin
disabled: ({ rowIndex }) => rowIndex === 0,
grow: 0.6,
},
{
...keyColumn<PathWaypointColumn, 'stopFor'>(
'stopFor',
createTextColumn({
continuousUpdates: false,
alignRight: true,
})
),
title: `${t('stopTime')}`,
grow: 0.6,
},
{
...keyColumn<PathWaypointColumn, 'onStopSignal'>(
'onStopSignal',
checkboxColumn as Partial<Column<boolean | undefined>>
),
title: t('receptionOnClosedSignal'),

// We should not be able to edit the reception on close signal if stopFor is not filled
// except for the destination
grow: 0.6,
disabled: ({ rowData, rowIndex }) =>
rowIndex !== pathProperties.allWaypoints?.length - 1 && !rowData.stopFor,
},
{
...keyColumn<PathWaypointColumn, 'theoreticalMargin'>(
'theoreticalMargin',
createTextColumn({
continuousUpdates: false,
alignRight: true,
placeholder: t('theoreticalMarginPlaceholder'),
formatBlurredInput: (value) => {
if (!value || value === 'none') return '';
if (!marginRegExValidation.test(value)) {
return `${value}${t('theoreticalMarginPlaceholder')}`;
}
return value;
},
})
),
cellClassName: ({ rowData }) => (rowData.isMarginValid ? '' : 'invalidCell'),
title: t('theoreticalMargin'),
disabled: ({ rowIndex }) => rowIndex === pathProperties.allWaypoints.length - 1,
},
],
const columns = useMemo<Column<PathWaypointColumn>[]>(
() => inputColumns(t, pathProperties),
[t, pathProperties.allWaypoints.length]
);

Expand Down
2 changes: 0 additions & 2 deletions front/src/modules/timesStops/consts.ts

This file was deleted.

2 changes: 1 addition & 1 deletion front/src/modules/timesStops/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { TFunction } from 'i18next';

import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { PathStep } from 'reducers/osrdconf/types';
import { marginRegExValidation } from 'utils/physics';

import { marginRegExValidation } from './consts';
import type { PathWaypointColumn } from './types';

// eslint-disable-next-line import/prefer-default-export
Expand Down
2 changes: 2 additions & 0 deletions front/src/utils/physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export function msToKmh(v: number) {
export function mToKmOneDecimal(m: number) {
return Math.round(m / 100) / 10;
}

export const marginRegExValidation = /^\d+(\.\d+)?%$|^\d+(\.\d+)?min\/100km$/;

0 comments on commit de8f44c

Please sign in to comment.