Skip to content

Commit

Permalink
front: separate StdcmPathStep and PathStep types
Browse files Browse the repository at this point in the history
  • Loading branch information
clarani committed Nov 15, 2024
1 parent cb99790 commit d242e0d
Show file tree
Hide file tree
Showing 25 changed files with 276 additions and 218 deletions.
24 changes: 7 additions & 17 deletions front/src/applications/stdcm/components/StdcmForm/StdcmConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';

import { Button } from '@osrd-project/ui-core';
import { ArrowDown, ArrowUp } from '@osrd-project/ui-icons';
import cx from 'classnames';
import { compact } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import { extractMarkersInfo } from 'applications/stdcm/utils';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import useInfraStatus from 'modules/pathfinding/hooks/useInfraStatus';
import { Map } from 'modules/trainschedule/components/ManageTrainSchedule';
Expand Down Expand Up @@ -77,15 +77,11 @@ const StdcmConfig = ({

const disabled = isPending || retainedSimulationIndex > -1;

const markerInfos = useMemo(() => extractMarkersInfo(pathSteps), [pathSteps]);

const startSimulation = () => {
const isPathfindingFailed = !!pathfinding && pathfinding.status !== 'success';
const formErrorsStatus = checkStdcmConfigErrors(
isPathfindingFailed,
origin,
destination,
compact(pathSteps),
t
);
const formErrorsStatus = checkStdcmConfigErrors(isPathfindingFailed, origin, destination, t);
if (pathfinding?.status === 'success' && !formErrorsStatus) {
launchStdcmRequest();
} else {
Expand All @@ -108,13 +104,7 @@ const StdcmConfig = ({

useEffect(() => {
const isPathfindingFailed = !!pathfinding && pathfinding.status !== 'success';
const formErrorsStatus = checkStdcmConfigErrors(
isPathfindingFailed,
origin,
destination,
[],
t
);
const formErrorsStatus = checkStdcmConfigErrors(isPathfindingFailed, origin, destination, t);
setFormErrors(formErrorsStatus);
}, [origin, destination, pathfinding]);

Expand Down Expand Up @@ -195,7 +185,7 @@ const StdcmConfig = ({
preventPointSelection
pathGeometry={pathfinding?.geometry}
showStdcmAssets
simulationPathSteps={pathSteps}
simulationPathSteps={markerInfos}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import { getTimesInfoFromDate } from 'applications/stdcm/utils';
import DestinationIcon from 'assets/pictures/mapMarkers/destination.svg';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import { useAppDispatch } from 'store';
import { extractDateAndTimefromISO, generateISODateFromDateTime } from 'utils/date';

import StdcmCard from './StdcmCard';
import StdcmOperationalPoint from './StdcmOperationalPoint';
Expand All @@ -28,9 +28,7 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => {

const { destinationArrival, destinationToleranceValues } = useMemo(
() => ({
destinationArrival: destination.arrival
? extractDateAndTimefromISO(destination.arrival)
: undefined,
destinationArrival: getTimesInfoFromDate(destination.arrival),
destinationToleranceValues: {
arrivalToleranceBefore:
destination.tolerances?.before !== undefined
Expand All @@ -45,11 +43,12 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => {
[destination]
);

const onArrivalChange = (schedule: ScheduleConstraint) => {
const onArrivalChange = ({ date, hours, minutes }: ScheduleConstraint) => {
date.setHours(hours, minutes);
dispatch(
updateStdcmPathStep({
id: destination.id,
updates: { arrival: generateISODateFromDateTime(schedule) },
updates: { arrival: date },
})
);
};
Expand Down Expand Up @@ -80,8 +79,12 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => {
disabled={disabled}
className="extremity"
>
{'uic' in destination && (
<StdcmOperationalPoint point={destination} opPointId={destination.id} disabled={disabled} />
{(!destination.location || 'uic' in destination.location) && (
<StdcmOperationalPoint
location={destination.location}
pathStepId={destination.id}
disabled={disabled}
/>
)}
<StdcmOpSchedule
onArrivalChange={onArrivalChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,29 @@ import { Input } from '@osrd-project/ui-core';
import { debounce } from 'lodash';
import { useTranslation } from 'react-i18next';

import type { PathStep } from 'reducers/osrdconf/types';
import { ISO8601Duration2sec, secToMin } from 'utils/timeManipulation';

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

type StdcmInputViaProps = {
stopType: StdcmStopTypes;
stopDuration: PathStep['stopFor'];
stopDuration?: number;
updatePathStepStopTime: (stopTime: string) => void;
};

const StdcmInputVia = ({ stopType, stopDuration, updatePathStepStopTime }: StdcmInputViaProps) => {
const { t } = useTranslation('stdcm');

const computedStopTime = useMemo(() => {
const duration = stopDuration ? `${secToMin(ISO8601Duration2sec(stopDuration))}` : '';
switch (stopType) {
case StdcmStopTypes.PASSAGE_TIME:
return '0';
case StdcmStopTypes.DRIVER_SWITCH:
return duration || '3';
default:
return duration || '0';
}
}, [stopDuration, stopType]);

const [pathStepStopTime, setPathStepStopTime] = useState(computedStopTime);
const [pathStepStopTime, setPathStepStopTime] = useState('');

const stopWarning = stopType === StdcmStopTypes.DRIVER_SWITCH && Number(computedStopTime) < 3;
const stopWarning = stopType === StdcmStopTypes.DRIVER_SWITCH && stopDuration && stopDuration < 3;

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

useEffect(() => {
setPathStepStopTime(computedStopTime);
}, [computedStopTime]);
setPathStepStopTime(stopDuration ? `${stopDuration}` : '');
}, [stopDuration]);

return (
stopType !== StdcmStopTypes.PASSAGE_TIME && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { type SearchResultItemOperationalPoint } from 'common/api/osrdEditoastAp
import useSearchOperationalPoint from 'common/Map/Search/useSearchOperationalPoint';
import { useOsrdConfActions } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmPathStep } from 'reducers/osrdconf/types';
import type { UicPathItemLocation } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { normalized } from 'utils/strings';
import { createFixedSelectOptions } from 'utils/uiCoreHelpers';

type StdcmOperationalPointProps = {
point: StdcmPathStep;
opPointId: string;
location?: UicPathItemLocation;
pathStepId: string;
disabled?: boolean;
};

Expand All @@ -24,12 +24,15 @@ function formatChCode(chCode: string) {
return chCode === '' ? 'BV' : chCode;
}

const StdcmOperationalPoint = ({ point, opPointId, disabled }: StdcmOperationalPointProps) => {
const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperationalPointProps) => {
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();

const { searchTerm, chCodeFilter, sortedSearchResults, setSearchTerm, setChCodeFilter } =
useSearchOperationalPoint({ initialSearchTerm: point.name, initialChCodeFilter: point.ch });
useSearchOperationalPoint({
initialSearchTerm: location?.name,
initialChCodeFilter: location?.secondary_code || undefined,
});

const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions;

Expand Down Expand Up @@ -71,20 +74,21 @@ const StdcmOperationalPoint = ({ point, opPointId, disabled }: StdcmOperationalP
},
[] as { label: string; id: string }[]
),
[point, sortedSearchResults]
[location, sortedSearchResults]
);

const dispatchNewPoint = (p?: SearchResultItemOperationalPoint) => {
if (p && p.ch === point.ch && 'uic' in point && p.uic === point.uic) return;
const newPoint = p
if (p && p.ch === location?.secondary_code && 'uic' in location && p.uic === location.uic)
return;
const newLocation = p
? {
name: p.name,
ch: p.ch,
uic: p.uic,
coordinates: p.geographic.coordinates,
coordinates: p.geographic.coordinates as [number, number],
}
: { name: undefined, ch: undefined, uic: -1, coordinates: undefined };
dispatch(updateStdcmPathStep({ id: point.id, updates: newPoint }));
: undefined;
dispatch(updateStdcmPathStep({ id: pathStepId, updates: { location: newLocation } }));
};

const updateSelectedPoint = (
Expand All @@ -110,8 +114,8 @@ const StdcmOperationalPoint = ({ point, opPointId, disabled }: StdcmOperationalP

const onSelectChCodeFilter = (selectedChCode?: { id: string }) => {
setChCodeFilter(selectedChCode?.id);
if (point && 'uic' in point)
updateSelectedPoint(sortedSearchResults, point.uic, selectedChCode?.id);
if (location && 'uic' in location)
updateSelectedPoint(sortedSearchResults, location.uic, selectedChCode?.id);
};

const onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -122,20 +126,20 @@ const StdcmOperationalPoint = ({ point, opPointId, disabled }: StdcmOperationalP
};

useEffect(() => {
if (point) {
setSearchTerm(point.name || '');
setChCodeFilter(point.ch || '');
if (location) {
setSearchTerm(location.name || '');
setChCodeFilter(location.secondary_code || '');
} else {
setSearchTerm('');
setChCodeFilter(undefined);
}
}, [point]);
}, [location]);

return (
<div className="location-line">
<div className="col-9 ci-input">
<ComboBox
id={`${opPointId}-ci`}
id={`${pathStepId}-ci`}
label={t('trainPath.ci')}
value={searchTerm}
onChange={onInputChange}
Expand All @@ -150,7 +154,7 @@ const StdcmOperationalPoint = ({ point, opPointId, disabled }: StdcmOperationalP
<div className="col-3 p-0">
<Select
label={t('trainPath.ch')}
id={`${opPointId}-ch`}
id={`${pathStepId}-ch`}
value={chCodeFilter ? { label: formatChCode(chCodeFilter), id: chCodeFilter } : undefined}
onChange={(e) => onSelectChCodeFilter(e)}
{...createFixedSelectOptions(sortedChOptions)}
Expand Down
17 changes: 11 additions & 6 deletions front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import { getTimesInfoFromDate } from 'applications/stdcm/utils';
import OriginIcon from 'assets/pictures/mapMarkers/start.svg';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import { useAppDispatch } from 'store';
import { extractDateAndTimefromISO, generateISODateFromDateTime } from 'utils/date';

import StdcmCard from './StdcmCard';
import StdcmOperationalPoint from './StdcmOperationalPoint';
Expand All @@ -27,7 +27,7 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => {

const { originArrival, originToleranceValues } = useMemo(
() => ({
originArrival: origin.arrival ? extractDateAndTimefromISO(origin.arrival) : undefined,
originArrival: getTimesInfoFromDate(origin.arrival),
originToleranceValues: {
arrivalToleranceBefore:
origin.tolerances?.before !== undefined ? origin.tolerances.before : DEFAULT_TOLERANCE,
Expand All @@ -38,11 +38,12 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => {
[origin]
);

const onOriginArrivalChange = (schedule: ScheduleConstraint) => {
const onOriginArrivalChange = ({ date, hours, minutes }: ScheduleConstraint) => {
date.setHours(hours, minutes);
dispatch(
updateStdcmPathStep({
id: origin.id,
updates: { arrival: generateISODateFromDateTime(schedule) },
updates: { arrival: date },
})
);
};
Expand Down Expand Up @@ -79,8 +80,12 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => {
disabled={disabled}
hasTip
>
{'uic' in origin && (
<StdcmOperationalPoint point={origin} opPointId={origin.id} disabled={disabled} />
{(!origin.location || 'uic' in origin.location) && (
<StdcmOperationalPoint
location={origin.location}
pathStepId={origin.id}
disabled={disabled}
/>
)}
<StdcmOpSchedule
onArrivalChange={onOriginArrivalChange}
Expand Down
36 changes: 19 additions & 17 deletions front/src/applications/stdcm/components/StdcmForm/StdcmVias.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import type { StdcmPathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { formatDurationAsISO8601 } from 'utils/timeManipulation';

import StdcmCard from './StdcmCard';
import StdcmDefaultCard from './StdcmDefaultCard';
Expand All @@ -31,8 +30,7 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
const intermediatePoints = useMemo(() => pathSteps.slice(1, -1), [pathSteps]);

const updateStopType = (newStopType: StdcmStopTypes, pathStep: StdcmPathStep) => {
const defaultStopTime =
newStopType === StdcmStopTypes.DRIVER_SWITCH ? formatDurationAsISO8601(3 * 60) : '';
const defaultStopTime = newStopType === StdcmStopTypes.DRIVER_SWITCH ? 3 : undefined;
dispatch(
updateStdcmPathStep({
id: pathStep.id,
Expand All @@ -42,10 +40,11 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
};

const updateStopDuration = (stopTime: string, pathStep: StdcmPathStep) => {
const stopFor = stopTime ? Number(stopTime) : undefined;
dispatch(
updateStdcmPathStep({
id: pathStep.id,
updates: { stopFor: formatDurationAsISO8601(Number(stopTime) * 60) },
updates: { stopFor },
})
);
};
Expand Down Expand Up @@ -89,20 +88,23 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
disabled={disabled}
className="via"
>
<StdcmOperationalPoint point={pathStep} opPointId={pathStep.id} disabled={disabled} />
{'uic' in pathStep && pathStep.uic !== -1 && (
<>
<StdcmStopType
stopTypes={pathStep.stopType}
updatePathStepStopType={(newStopType) => updateStopType(newStopType, pathStep)}
/>
<StdcmInputVia
stopType={pathStep.stopType}
stopDuration={pathStep.stopFor}
updatePathStepStopTime={(e) => updateStopDuration(e, pathStep)}
/>
</>
{(!pathStep.location || 'uic' in pathStep.location) && (
<StdcmOperationalPoint
location={pathStep.location}
pathStepId={pathStep.id}
disabled={disabled}
/>
)}

<StdcmStopType
stopTypes={pathStep.stopType}
updatePathStepStopType={(newStopType) => updateStopType(newStopType, pathStep)}
/>
<StdcmInputVia
stopType={pathStep.stopType}
stopDuration={pathStep.stopFor}
updatePathStepStopTime={(e) => updateStopDuration(e, pathStep)}
/>
</StdcmCard>
</div>
);
Expand Down
Loading

0 comments on commit d242e0d

Please sign in to comment.