-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStopDurationInput.tsx
78 lines (66 loc) · 2.35 KB
/
StopDurationInput.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { useMemo, useEffect, useState } from 'react';
import { Input } from '@osrd-project/ui-core';
import type { Status } from '@osrd-project/ui-core/dist/components/inputs/StatusMessage';
import { useTranslation } from 'react-i18next';
import { useOsrdConfActions } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmPathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { useDebounce } from 'utils/helpers';
import { parseNumber } from 'utils/strings';
import { StdcmStopTypes } from '../../types';
type StopDurationInputProps = {
pathStep: Extract<StdcmPathStep, { isVia: true }>;
};
const StopDurationInput = ({ pathStep }: StopDurationInputProps) => {
const dispatch = useAppDispatch();
const { t } = useTranslation('stdcm');
const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions;
const [stopDuration, setStopDuration] = useState(
pathStep.stopFor !== undefined ? `${pathStep.stopFor}` : ''
);
const debouncedStopDuration = useDebounce(stopDuration, 300);
const stopWarning = useMemo(
() =>
pathStep.stopType === StdcmStopTypes.DRIVER_SWITCH &&
pathStep.stopFor !== undefined &&
pathStep.stopFor < 3
? {
status: 'warning' as Status,
message: t('trainPath.warningMinStopTime'),
}
: undefined,
[pathStep.stopType, pathStep.stopFor]
);
useEffect(() => {
setStopDuration(pathStep.stopFor !== undefined ? `${pathStep.stopFor}` : '');
}, [pathStep.stopFor]);
useEffect(() => {
const parsedNumber = parseNumber(debouncedStopDuration);
const newStopDuration = parsedNumber !== undefined ? Math.round(parsedNumber) : undefined;
if (newStopDuration !== pathStep.stopFor) {
dispatch(
updateStdcmPathStep({
id: pathStep.id,
updates: { stopFor: newStopDuration },
})
);
}
}, [debouncedStopDuration]);
return (
<div className="stop-time">
<Input
id="stdcm-via-stop-time"
type="text"
label={t('trainPath.stopFor')}
onChange={(e) => {
setStopDuration(e.target.value);
}}
value={stopDuration}
trailingContent="minutes"
statusWithMessage={stopWarning}
/>
</div>
);
};
export default StopDurationInput;