|
1 | 1 | import { useMemo, useEffect, useState } from 'react';
|
2 | 2 |
|
3 | 3 | import { Input } from '@osrd-project/ui-core';
|
4 |
| -import { debounce } from 'lodash'; |
| 4 | +import type { Status } from '@osrd-project/ui-core/dist/components/inputs/StatusMessage'; |
5 | 5 | import { useTranslation } from 'react-i18next';
|
6 | 6 |
|
| 7 | +import { useOsrdConfActions } from 'common/osrdContext'; |
| 8 | +import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf'; |
| 9 | +import type { StdcmPathStep } from 'reducers/osrdconf/types'; |
| 10 | +import { useAppDispatch } from 'store'; |
| 11 | +import { useDebounce } from 'utils/helpers'; |
| 12 | + |
7 | 13 | import { StdcmStopTypes } from '../../types';
|
8 | 14 |
|
9 | 15 | type StopDurationInputProps = {
|
10 |
| - stopType: StdcmStopTypes; |
11 |
| - stopDuration?: number; |
12 |
| - updatePathStepStopTime: (stopTime: string) => void; |
| 16 | + pathStep: Extract<StdcmPathStep, { isVia: true }>; |
13 | 17 | };
|
14 | 18 |
|
15 |
| -const StopDurationInput = ({ |
16 |
| - stopType, |
17 |
| - stopDuration, |
18 |
| - updatePathStepStopTime, |
19 |
| -}: StopDurationInputProps) => { |
| 19 | +const StopDurationInput = ({ pathStep }: StopDurationInputProps) => { |
| 20 | + const dispatch = useAppDispatch(); |
20 | 21 | const { t } = useTranslation('stdcm');
|
21 | 22 |
|
22 |
| - const [pathStepStopTime, setPathStepStopTime] = useState(''); |
| 23 | + const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions; |
23 | 24 |
|
24 |
| - const stopWarning = stopType === StdcmStopTypes.DRIVER_SWITCH && stopDuration && stopDuration < 3; |
| 25 | + const [stopDuration, setStopDuration] = useState(''); |
| 26 | + const debouncedStopDuration = useDebounce(stopDuration, 300); |
25 | 27 |
|
26 |
| - const debounceUpdatePathStepStopTime = useMemo( |
27 |
| - () => debounce((value) => updatePathStepStopTime(value), 300), |
28 |
| - [] |
| 28 | + const stopWarning = useMemo( |
| 29 | + () => |
| 30 | + pathStep.stopType === StdcmStopTypes.DRIVER_SWITCH && |
| 31 | + pathStep.stopFor !== undefined && |
| 32 | + pathStep.stopFor < 3 |
| 33 | + ? { |
| 34 | + status: 'warning' as Status, |
| 35 | + message: t('trainPath.warningMinStopTime'), |
| 36 | + } |
| 37 | + : undefined, |
| 38 | + [pathStep.stopType, pathStep.stopFor] |
29 | 39 | );
|
| 40 | + useEffect(() => { |
| 41 | + setStopDuration(pathStep.stopFor !== undefined ? `${pathStep.stopFor}` : ''); |
| 42 | + }, [pathStep.stopFor]); |
30 | 43 |
|
31 | 44 | useEffect(() => {
|
32 |
| - setPathStepStopTime(stopDuration !== undefined ? `${stopDuration}` : ''); |
33 |
| - }, [stopDuration]); |
| 45 | + const newStopDuration = debouncedStopDuration ? Number(debouncedStopDuration) : undefined; |
| 46 | + if (newStopDuration !== pathStep.stopFor) { |
| 47 | + dispatch( |
| 48 | + updateStdcmPathStep({ |
| 49 | + id: pathStep.id, |
| 50 | + updates: { stopFor: newStopDuration }, |
| 51 | + }) |
| 52 | + ); |
| 53 | + } |
| 54 | + }, [debouncedStopDuration]); |
| 55 | + |
| 56 | + if (pathStep.stopType === StdcmStopTypes.PASSAGE_TIME) { |
| 57 | + return null; |
| 58 | + } |
34 | 59 |
|
35 | 60 | return (
|
36 |
| - stopType !== StdcmStopTypes.PASSAGE_TIME && ( |
37 |
| - <div className="stop-time"> |
38 |
| - <Input |
39 |
| - id="stdcm-via-stop-time" |
40 |
| - type="text" |
41 |
| - label={t('trainPath.stopFor')} |
42 |
| - onChange={(e) => { |
43 |
| - // TODO: Find a better way to prevent user from entering decimal values |
44 |
| - const value = e.target.value.replace(/[\D.,]/g, ''); |
45 |
| - setPathStepStopTime(value); |
46 |
| - debounceUpdatePathStepStopTime(value); |
47 |
| - }} |
48 |
| - value={pathStepStopTime} |
49 |
| - trailingContent="minutes" |
50 |
| - statusWithMessage={ |
51 |
| - stopWarning |
52 |
| - ? { |
53 |
| - status: 'warning', |
54 |
| - message: t('trainPath.warningMinStopTime'), |
55 |
| - } |
56 |
| - : undefined |
57 |
| - } |
58 |
| - /> |
59 |
| - </div> |
60 |
| - ) |
| 61 | + <div className="stop-time"> |
| 62 | + <Input |
| 63 | + id="stdcm-via-stop-time" |
| 64 | + type="text" |
| 65 | + label={t('trainPath.stopFor')} |
| 66 | + onChange={(e) => { |
| 67 | + // TODO: Find a better way to prevent user from entering decimal values |
| 68 | + const value = e.target.value.replace(/[\D.,]/g, ''); |
| 69 | + setStopDuration(value); |
| 70 | + }} |
| 71 | + value={stopDuration} |
| 72 | + trailingContent="minutes" |
| 73 | + statusWithMessage={stopWarning} |
| 74 | + /> |
| 75 | + </div> |
61 | 76 | );
|
62 | 77 | };
|
63 | 78 |
|
|
0 commit comments