From 2dd64577fea63f503e34be423bc60bbc8eccf3b1 Mon Sep 17 00:00:00 2001 From: Egor Berezovskiy Date: Mon, 2 Dec 2024 11:10:11 +0100 Subject: [PATCH] fron: lmr consist prefill mass length max speed Signed-off-by: Egor Berezovskiy --- .../components/StdcmForm/StdcmConsist.tsx | 7 ++++ .../stdcm/hooks/useStdcmConsist.ts | 35 +++++++++++++++++++ front/src/utils/physics.ts | 8 +++++ 3 files changed, 50 insertions(+) diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx index 5f606154cb3..6c4b23d8b59 100644 --- a/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx +++ b/front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx @@ -52,6 +52,7 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => { onTotalLengthChange, maxSpeed, onMaxSpeedChange, + prefillConsist, } = useStdcmConsist(); const { filters, searchRollingStock, searchRollingStockById, filteredRollingStockList } = @@ -91,6 +92,9 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => { }; const onSelectSuggestion = (option?: LightRollingStockWithLiveries) => { + if (option) { + prefillConsist(option); + } dispatch(updateRollingStockID(option?.id)); }; @@ -144,6 +148,9 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => { suggestions={filteredTowedRollingStockList} getSuggestionLabel={(suggestion: TowedRollingStock) => suggestion.name} onSelectSuggestion={(towed) => { + if (towed) { + prefillConsist(rollingStock, towed); + } dispatch(updateTowedRollingStockID(towed?.id)); }} /> diff --git a/front/src/applications/stdcm/hooks/useStdcmConsist.ts b/front/src/applications/stdcm/hooks/useStdcmConsist.ts index 86124a64c92..eb66f834827 100644 --- a/front/src/applications/stdcm/hooks/useStdcmConsist.ts +++ b/front/src/applications/stdcm/hooks/useStdcmConsist.ts @@ -1,12 +1,24 @@ +import { useState } from 'react'; + import { useSelector } from 'react-redux'; +import type { + LightRollingStockWithLiveries, + TowedRollingStock, +} from 'common/api/generatedEditoastApi'; 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 } from 'utils/physics'; 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 } = @@ -15,21 +27,43 @@ const useStdcmConsist = () => { const totalMass = useSelector(getTotalMass); const onTotalMassChange = (e: React.ChangeEvent) => { const totalMassValue = Number(e.target.value); + setTotalMassChanged(true); dispatch(updateTotalMass(totalMassValue === 0 ? undefined : totalMassValue)); }; const totalLength = useSelector(getTotalLength); const onTotalLengthChange = (e: React.ChangeEvent) => { const totalLengthValue = Number(e.target.value); + setTotalLengthChanged(true); dispatch(updateTotalLength(totalLengthValue === 0 ? undefined : totalLengthValue)); }; const maxSpeed = useSelector(getMaxSpeed); const onMaxSpeedChange = (e: React.ChangeEvent) => { const totalMaxSpeed = Number(e.target.value); + setMaxSpeedChanged(true); dispatch(updateMaxSpeed(totalMaxSpeed === 0 ? undefined : totalMaxSpeed)); }; + const prefillConsist = ( + rollingStock?: LightRollingStockWithLiveries, + towed?: TowedRollingStock + ) => { + if (!totalMassChanged) { + dispatch(updateTotalMass(Math.floor(kgToT((rollingStock?.mass ?? 0) + (towed?.mass ?? 0))))); + } + + if (!totalLengthChanged) { + dispatch(updateTotalLength(Math.floor((rollingStock?.length ?? 0) + (towed?.length ?? 0)))); + } + + if (!maxSpeedChanged) { + dispatch( + updateMaxSpeed(rollingStock?.max_speed ? Math.floor(rollingStock.max_speed) : undefined) + ); + } + }; + return { totalMass, onTotalMassChange, @@ -37,6 +71,7 @@ const useStdcmConsist = () => { onTotalLengthChange, maxSpeed, onMaxSpeedChange, + prefillConsist, }; }; diff --git a/front/src/utils/physics.ts b/front/src/utils/physics.ts index 02ed90165be..d923eb7140f 100644 --- a/front/src/utils/physics.ts +++ b/front/src/utils/physics.ts @@ -101,3 +101,11 @@ export function decimalToPercentage(value: number) { export function tToKg(value: number) { return value * 1000; } + +/** + * ex: converts 12000kg to 12t + * @param value in kg + */ +export function kgToT(value: number) { + return value / 1000; +}