Skip to content

Commit

Permalink
fron: lmr consist prefill mass length max speed
Browse files Browse the repository at this point in the history
Signed-off-by: Egor Berezovskiy <[email protected]>
  • Loading branch information
Wadjetz committed Dec 2, 2024
1 parent c98d385 commit 2dd6457
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
onTotalLengthChange,
maxSpeed,
onMaxSpeedChange,
prefillConsist,
} = useStdcmConsist();

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

const onSelectSuggestion = (option?: LightRollingStockWithLiveries) => {
if (option) {
prefillConsist(option);
}
dispatch(updateRollingStockID(option?.id));
};

Expand Down Expand Up @@ -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));
}}
/>
Expand Down
35 changes: 35 additions & 0 deletions front/src/applications/stdcm/hooks/useStdcmConsist.ts
Original file line number Diff line number Diff line change
@@ -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 } =
Expand All @@ -15,28 +27,51 @@ const useStdcmConsist = () => {
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
) => {
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,
totalLength,
onTotalLengthChange,
maxSpeed,
onMaxSpeedChange,
prefillConsist,
};
};

Expand Down
8 changes: 8 additions & 0 deletions front/src/utils/physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2dd6457

Please sign in to comment.