Skip to content

Commit

Permalink
front: add stop type selector in stdcm
Browse files Browse the repository at this point in the history
  • Loading branch information
Akctarus committed Sep 18, 2024
1 parent df44230 commit d330d87
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 25 deletions.
9 changes: 8 additions & 1 deletion front/public/locales/en/stdcm.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@
"origin": "Origin",
"preciseTime": "precise time",
"stopFor": "Minimum stop time",
"stopType": {
"driverSwitch": "driver switch",
"passageTime":"passage time",
"serviceStop": "service stop"
},
"time": "Time",
"tolerance": "Tolerance",
"vias": "Intermediate OP"
"type": "Type",
"vias": "Intermediate OP",
"warningMinStopTime": "The driver switch stop time must be at least 3 minutes."
}
}
9 changes: 8 additions & 1 deletion front/public/locales/fr/stdcm.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@
"origin": "Origine",
"preciseTime": "horaire précis",
"stopFor": "Temps d'arrêt minimum",
"stopType": {
"driverSwitch": "relève conducteur",
"passageTime":"passage",
"serviceStop": "arrêt de service"
},
"time": "Heure",
"tolerance": "Tolérance",
"vias": "PR intermédiaire"
"type": "Type",
"vias": "PR intermédiaire",
"warningMinStopTime": "Le temps d'arrêt d'une relève conducteur est d'au moins 3 minutes."
}
}
70 changes: 54 additions & 16 deletions front/src/applications/stdcmV2/components/StdcmInputVia.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useMemo } from 'react';
import { useState, useMemo, useEffect } from 'react';

import { Input } from '@osrd-project/ui-core';
import { debounce } from 'lodash';
Expand All @@ -7,10 +7,14 @@ import { useTranslation } from 'react-i18next';
import type { PathStep } from 'reducers/osrdconf/types';
import { ISO8601Duration2sec } from 'utils/timeManipulation';

import { StdcmStopTypes } from '../types';

const StdcmInputVia = ({
stopType,
pathStep,
updatePathStepStopTime,
}: {
stopType: StdcmStopTypes;
pathStep: PathStep;
updatePathStepStopTime: (stopTime: string) => void;
}) => {
Expand All @@ -20,27 +24,61 @@ const StdcmInputVia = ({
pathStep.stopFor ? `${ISO8601Duration2sec(pathStep.stopFor) / 60}` : ''
);

const [showWarning, setShowWarning] = useState(false);

const debounceUpdatePathStepStopTime = useMemo(
() => debounce((value) => updatePathStepStopTime(value), 500),
[]
);

useEffect(() => {
let newStopTime = pathStepStopTime;
let warning = false;

const isDriverSwitch = stopType === StdcmStopTypes.DRIVER_SWITCH;
const isPassageTime = stopType === StdcmStopTypes.PASSAGE_TIME;
const isServiceStop = stopType === StdcmStopTypes.SERVICE_STOP;

if (isPassageTime && pathStepStopTime !== '0') {
newStopTime = '0';
} else if (isDriverSwitch) {
const stopTimeValue = Number(pathStepStopTime);
if (stopTimeValue === 0) {
newStopTime = '3';
} else if (stopTimeValue < 3) {
warning = true;
}
} else if (isServiceStop && pathStepStopTime !== '0') {
newStopTime = '';
}

if (newStopTime !== pathStepStopTime) {
setPathStepStopTime(newStopTime);
updatePathStepStopTime(newStopTime);
}

setShowWarning(warning);
}, [stopType, pathStepStopTime]);

return (
<div className="stdcm-v2-via-stop-for pl-2">
<Input
id="stdcm-v2-via-stop-time"
type="text"
label={t('trainPath.stopFor')}
onChange={(e) => {
// TODO: Find a better way to prevent user from entering decimal values
const value = e.target.value.replace(/[\D.,]/g, '');
setPathStepStopTime(value);
debounceUpdatePathStepStopTime(value);
}}
value={pathStepStopTime}
trailingContent="minutes"
/>
</div>
stopType !== StdcmStopTypes.PASSAGE_TIME && (
<div className="stdcm-v2-via-stop-for pl-2">
<Input
id="stdcm-v2-via-stop-time"
type="text"
label={t('trainPath.stopFor')}
onChange={(e) => {
// TODO: Find a better way to prevent user from entering decimal values
const value = e.target.value.replace(/[\D.,]/g, '');
setPathStepStopTime(value);
debounceUpdatePathStepStopTime(value);
}}
value={pathStepStopTime}
trailingContent="minutes"
/>
{showWarning && <div style={{ color: 'orange' }}>{t('trainPath.warningMinStopTime')}</div>}
</div>
)
);
};

Expand Down
33 changes: 33 additions & 0 deletions front/src/applications/stdcmV2/components/StdcmStopType.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Select } from '@osrd-project/ui-core';
import { useTranslation } from 'react-i18next';

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

type StdcmStopTypeProps = {
stopTypes: StdcmStopTypes;
onStdcmStopTypesChange: (arrivalType: StdcmStopTypes) => void;
};

const StdcmStopType = ({ stopTypes, onStdcmStopTypesChange }: StdcmStopTypeProps) => {
const { t } = useTranslation('stdcm');

return (
<div className="stdcm-v2-via-stop-for pl-2">
<Select
label={t('trainPath.type')}
id="type"
value={stopTypes}
options={['passageTime', 'driverSwitch', 'serviceStop']}
onChange={(e) => {
if (e) {
onStdcmStopTypesChange(e as StdcmStopTypes);
}
}}
getOptionLabel={(option) => t(`trainPath.stopType.${option}`)}
getOptionValue={(option) => option}
/>
</div>
);
};

export default StdcmStopType;
33 changes: 27 additions & 6 deletions front/src/applications/stdcmV2/components/StdcmVias.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo } from 'react';
import { useEffect, useMemo, useState } from 'react';

import { Location } from '@osrd-project/ui-icons';
import { useTranslation } from 'react-i18next';
Expand All @@ -17,6 +17,8 @@ import StdcmCard from './StdcmCard';
import StdcmDefaultCard from './StdcmDefaultCard';
import StdcmInputVia from './StdcmInputVia';
import StdcmOperationalPoint from './StdcmOperationalPoint';
import StdcmStopType from './StdcmStopType';
import { StdcmStopTypes } from '../types';
import type { StdcmConfigCardProps } from '../types';

const StdcmVias = ({ disabled = false, setCurrentSimulationInputs }: StdcmConfigCardProps) => {
Expand All @@ -27,10 +29,17 @@ const StdcmVias = ({ disabled = false, setCurrentSimulationInputs }: StdcmConfig
useOsrdConfActions() as StdcmConfSliceActions;
const pathSteps = useSelector(getPathSteps);

const [stopType, setStopType] = useState<StdcmStopTypes>(StdcmStopTypes.PASSAGE_TIME);

const intermediatePoints = useMemo(() => pathSteps.slice(1, -1), [pathSteps]);

const updatePathStepsList = (pathStep: PathStep | null, index: number) => {
const newPathSteps = replaceElementAtIndex(pathSteps, index, pathStep);
if (!pathStep) return;
const updatedPathStep = {
...pathStep,
};

const newPathSteps = replaceElementAtIndex(pathSteps, index, updatedPathStep);
dispatch(updatePathSteps({ pathSteps: newPathSteps }));
};

Expand Down Expand Up @@ -61,6 +70,14 @@ const StdcmVias = ({ disabled = false, setCurrentSimulationInputs }: StdcmConfig
}));
}, [pathSteps]);

// useEffect(() => {
// if (stopType === StdcmStopTypes.DRIVER_SWITCH) {
// setStopTypeTime('PT180S');
// } else if (stopType === StdcmStopTypes.SERVICE_STOP) {
// setStopTypeTime('PT60S');
// }
// }, [stopType]);

return (
<div className="stdcm-v2-vias-list">
{intermediatePoints.length > 0 &&
Expand Down Expand Up @@ -100,10 +117,14 @@ const StdcmVias = ({ disabled = false, setCurrentSimulationInputs }: StdcmConfig
/>
</div>
{pathStep && (
<StdcmInputVia
pathStep={pathStep}
updatePathStepStopTime={(e) => updatePathStepStopTime(e, pathStepIndex)}
/>
<>
<StdcmStopType stopTypes={stopType} onStdcmStopTypesChange={setStopType} />
<StdcmInputVia
stopType={stopType}
pathStep={pathStep}
updatePathStepStopTime={(e) => updatePathStepStopTime(e, pathStepIndex)}
/>
</>
)}
</StdcmCard>
</>
Expand Down
6 changes: 6 additions & 0 deletions front/src/applications/stdcmV2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ export type StdcmConfigErrors = {
errorType: StdcmConfigErrorTypes;
errorDetails?: { originTime: string; destinationTime: string };
};

export enum StdcmStopTypes {
PASSAGE_TIME = 'passageTime',
DRIVER_SWITCH = 'driverSwitch',
SERVICE_STOP = 'serviceStop',
}
3 changes: 2 additions & 1 deletion front/src/reducers/osrdconf/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Feature, Position } from 'geojson';

import type { PowerRestriction } from 'applications/operationalStudies/types';
import type { AllowanceValue } from 'applications/stdcm/types';
import type { ArrivalTimeTypes } from 'applications/stdcmV2/types';
import type { ArrivalTimeTypes, StdcmStopTypes } from 'applications/stdcmV2/types';
import type { Comfort, Distribution, PathItemLocation } from 'common/api/osrdEditoastApi';
import type { InfraState } from 'reducers/infra';

Expand Down Expand Up @@ -61,6 +61,7 @@ export type PathStep = PathItemLocation & {
arrivalToleranceAfter?: number;
locked?: boolean;
stopFor?: string | null;
stopType?: StdcmStopTypes;
theoreticalMargin?: string;
onStopSignal?: boolean;
kp?: string;
Expand Down

0 comments on commit d330d87

Please sign in to comment.