-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseStdcmConsist.ts
85 lines (71 loc) · 3.06 KB
/
useStdcmConsist.ts
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
79
80
81
82
83
84
85
import { useState } from 'react';
import { min } from 'lodash';
import { useSelector } from 'react-redux';
import type { LightRollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi';
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 { kgToT, kmhToMs, msToKmh } from 'utils/physics';
import maxSpeedFromSpeedLimitByTag from '../utils/maxSpeedFromSpeedLimitByTag';
const useStdcmConsist = () => {
const dispatch = useAppDispatch();
const [totalMassChanged, setTotalMassChanged] = useState(false);
const [totalLengthChanged, setTotalLengthChanged] = useState(false);
const [maxSpeedChanged, setMaxSpeedChanged] = useState(false);
const { getTotalMass, getTotalLength, getMaxSpeed } =
useOsrdConfSelectors() as StdcmConfSelectors;
const { updateTotalMass, updateTotalLength, updateMaxSpeed } =
useOsrdConfActions() as StdcmConfSliceActions;
const totalMass = useSelector(getTotalMass);
const onTotalMassChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const totalMassValue = Number(e.target.value);
setTotalMassChanged(true);
dispatch(updateTotalMass(totalMassValue === 0 ? undefined : totalMassValue));
};
const totalLength = useSelector(getTotalLength);
const onTotalLengthChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const totalLengthValue = Number(e.target.value);
setTotalLengthChanged(true);
dispatch(updateTotalLength(totalLengthValue === 0 ? undefined : totalLengthValue));
};
const maxSpeed = useSelector(getMaxSpeed);
const onMaxSpeedChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const totalMaxSpeed = Number(e.target.value);
setMaxSpeedChanged(true);
dispatch(updateMaxSpeed(totalMaxSpeed === 0 ? undefined : totalMaxSpeed));
};
const prefillConsist = (
rollingStock?: LightRollingStockWithLiveries,
towed?: TowedRollingStock,
maxSpeedTag?: string | null
) => {
if (!totalMassChanged) {
const consistMass = Math.ceil(kgToT((rollingStock?.mass ?? 0) + (towed?.mass ?? 0)));
dispatch(updateTotalMass(consistMass > 0 ? consistMass : undefined));
}
if (!totalLengthChanged) {
const consistLength = Math.ceil((rollingStock?.length ?? 0) + (towed?.length ?? 0));
dispatch(updateTotalLength(consistLength > 0 ? consistLength : undefined));
}
if (!maxSpeedChanged) {
const maxSpeedFromTag = maxSpeedFromSpeedLimitByTag(maxSpeedTag);
const consistMaxSpeed = min([
rollingStock?.max_speed,
towed?.max_speed,
maxSpeedFromTag ? kmhToMs(maxSpeedFromTag) : undefined,
]);
dispatch(updateMaxSpeed(consistMaxSpeed ? Math.floor(msToKmh(consistMaxSpeed)) : undefined));
}
};
return {
totalMass,
onTotalMassChange,
totalLength,
onTotalLengthChange,
maxSpeed,
onMaxSpeedChange,
prefillConsist,
};
};
export default useStdcmConsist;