Skip to content

Commit 5112724

Browse files
committed
front: refacto stopDurationInput
- use useDebounce hook instead of custom debounce function - add a useEffect to update the store when the input is changed - move the callback function inside the component stopDurationInput Signed-off-by: Clara Ni <clara.ni@outlook.fr>
1 parent c927340 commit 5112724

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

front/src/applications/stdcm/components/StdcmForm/StdcmVias.tsx

+1-15
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
8484
};
8585
}, [newIntermediateOpIndex]);
8686

87-
const updateStopDuration = (stopTime: string, pathStep: StdcmPathStep) => {
88-
const stopFor = stopTime ? Number(stopTime) : undefined;
89-
dispatch(
90-
updateStdcmPathStep({
91-
id: pathStep.id,
92-
updates: { stopFor },
93-
})
94-
);
95-
};
96-
9787
const deleteViaOnClick = (pathStepId: string) => {
9888
dispatch(deleteStdcmVia(pathStepId));
9989
};
@@ -147,11 +137,7 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
147137
stopTypes={pathStep.stopType}
148138
updatePathStepStopType={(newStopType) => updateStopType(newStopType, pathStep)}
149139
/>
150-
<StopDurationInput
151-
stopType={pathStep.stopType}
152-
stopDuration={pathStep.stopFor}
153-
updatePathStepStopTime={(e) => updateStopDuration(e, pathStep)}
154-
/>
140+
<StopDurationInput pathStep={pathStep} />
155141
</StdcmCard>
156142
</div>
157143
);

front/src/applications/stdcm/components/StdcmForm/StopDurationInput.tsx

+56-41
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,78 @@
11
import { useMemo, useEffect, useState } from 'react';
22

33
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';
55
import { useTranslation } from 'react-i18next';
66

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+
713
import { StdcmStopTypes } from '../../types';
814

915
type StopDurationInputProps = {
10-
stopType: StdcmStopTypes;
11-
stopDuration?: number;
12-
updatePathStepStopTime: (stopTime: string) => void;
16+
pathStep: Extract<StdcmPathStep, { isVia: true }>;
1317
};
1418

15-
const StopDurationInput = ({
16-
stopType,
17-
stopDuration,
18-
updatePathStepStopTime,
19-
}: StopDurationInputProps) => {
19+
const StopDurationInput = ({ pathStep }: StopDurationInputProps) => {
20+
const dispatch = useAppDispatch();
2021
const { t } = useTranslation('stdcm');
2122

22-
const [pathStepStopTime, setPathStepStopTime] = useState('');
23+
const { updateStdcmPathStep } = useOsrdConfActions() as StdcmConfSliceActions;
2324

24-
const stopWarning = stopType === StdcmStopTypes.DRIVER_SWITCH && stopDuration && stopDuration < 3;
25+
const [stopDuration, setStopDuration] = useState('');
26+
const debouncedStopDuration = useDebounce(stopDuration, 300);
2527

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]
2939
);
40+
useEffect(() => {
41+
setStopDuration(pathStep.stopFor !== undefined ? `${pathStep.stopFor}` : '');
42+
}, [pathStep.stopFor]);
3043

3144
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+
}
3459

3560
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>
6176
);
6277
};
6378

0 commit comments

Comments
 (0)