Skip to content

Commit cc5385a

Browse files
committed
fron: lmr consist prefill mass length max speed
Signed-off-by: Egor Berezovskiy <[email protected]>
1 parent c98d385 commit cc5385a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
5252
onTotalLengthChange,
5353
maxSpeed,
5454
onMaxSpeedChange,
55+
prefillConsist,
5556
} = useStdcmConsist();
5657

5758
const { filters, searchRollingStock, searchRollingStockById, filteredRollingStockList } =
@@ -91,6 +92,9 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
9192
};
9293

9394
const onSelectSuggestion = (option?: LightRollingStockWithLiveries) => {
95+
if (option) {
96+
prefillConsist(option);
97+
}
9498
dispatch(updateRollingStockID(option?.id));
9599
};
96100

@@ -144,6 +148,9 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
144148
suggestions={filteredTowedRollingStockList}
145149
getSuggestionLabel={(suggestion: TowedRollingStock) => suggestion.name}
146150
onSelectSuggestion={(towed) => {
151+
if (towed) {
152+
prefillConsist(rollingStock, towed);
153+
}
147154
dispatch(updateTowedRollingStockID(towed?.id));
148155
}}
149156
/>

front/src/applications/stdcm/hooks/useStdcmConsist.ts

+32
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import { useState } from 'react';
2+
13
import { useSelector } from 'react-redux';
24

5+
import type { LightRollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi';
36
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
47
import { type StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
58
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
69
import { useAppDispatch } from 'store';
10+
import { kgToT } from 'utils/physics';
711

812
const useStdcmConsist = () => {
913
const dispatch = useAppDispatch();
14+
15+
const [totalMassChanged, setTotalMassChanged] = useState(false);
16+
const [totalLengthChanged, setTotalLengthChanged] = useState(false);
17+
const [maxSpeedChanged, setMaxSpeedChanged] = useState(false);
18+
1019
const { getTotalMass, getTotalLength, getMaxSpeed } =
1120
useOsrdConfSelectors() as StdcmConfSelectors;
1221
const { updateTotalMass, updateTotalLength, updateMaxSpeed } =
@@ -15,28 +24,51 @@ const useStdcmConsist = () => {
1524
const totalMass = useSelector(getTotalMass);
1625
const onTotalMassChange = (e: React.ChangeEvent<HTMLInputElement>) => {
1726
const totalMassValue = Number(e.target.value);
27+
setTotalMassChanged(true);
1828
dispatch(updateTotalMass(totalMassValue === 0 ? undefined : totalMassValue));
1929
};
2030

2131
const totalLength = useSelector(getTotalLength);
2232
const onTotalLengthChange = (e: React.ChangeEvent<HTMLInputElement>) => {
2333
const totalLengthValue = Number(e.target.value);
34+
setTotalLengthChanged(true);
2435
dispatch(updateTotalLength(totalLengthValue === 0 ? undefined : totalLengthValue));
2536
};
2637

2738
const maxSpeed = useSelector(getMaxSpeed);
2839
const onMaxSpeedChange = (e: React.ChangeEvent<HTMLInputElement>) => {
2940
const totalMaxSpeed = Number(e.target.value);
41+
setMaxSpeedChanged(true);
3042
dispatch(updateMaxSpeed(totalMaxSpeed === 0 ? undefined : totalMaxSpeed));
3143
};
3244

45+
const prefillConsist = (
46+
rollingStock?: LightRollingStockWithLiveries,
47+
towed?: TowedRollingStock
48+
) => {
49+
if (!totalMassChanged) {
50+
dispatch(updateTotalMass(Math.floor(kgToT((rollingStock?.mass ?? 0) + (towed?.mass ?? 0)))));
51+
}
52+
53+
if (!totalLengthChanged) {
54+
dispatch(updateTotalLength(Math.floor((rollingStock?.length ?? 0) + (towed?.length ?? 0))));
55+
}
56+
57+
if (!maxSpeedChanged) {
58+
dispatch(
59+
updateMaxSpeed(rollingStock?.max_speed ? Math.floor(rollingStock.max_speed) : undefined)
60+
);
61+
}
62+
};
63+
3364
return {
3465
totalMass,
3566
onTotalMassChange,
3667
totalLength,
3768
onTotalLengthChange,
3869
maxSpeed,
3970
onMaxSpeedChange,
71+
prefillConsist,
4072
};
4173
};
4274

front/src/utils/physics.ts

+8
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ export function decimalToPercentage(value: number) {
101101
export function tToKg(value: number) {
102102
return value * 1000;
103103
}
104+
105+
/**
106+
* ex: converts 12000kg to 12t
107+
* @param value in kg
108+
*/
109+
export function kgToT(value: number) {
110+
return value / 1000;
111+
}

0 commit comments

Comments
 (0)