Skip to content

Commit

Permalink
front: fix length, mass and speed reset in lmr
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Macron <[email protected]>
  • Loading branch information
Akctarus committed Jan 22, 2025
1 parent 74a2bcb commit 1522505
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 45 deletions.
5 changes: 5 additions & 0 deletions front/public/locales/en/stdcm.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"negative": "Must be positive.",
"range": "The max speed must be between {{low}} and {{high}}km/h"
}
},
"info": {
"totalMass": "The weight has been updated to reflect the new traction unit.",
"totalLength":"The length has been updated to reflect the new traction unit.",
"maxSpeed":"The max speed has been updated to reflect the new traction unit."
}
},
"datetimeOutsideWindow": "Date must be between {{low}} and {{high}}",
Expand Down
5 changes: 5 additions & 0 deletions front/public/locales/fr/stdcm.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"negative": "Doit être positif.",
"range": "La vitesse max. doit être comprise entre {{low}} et {{high}}km/h"
}
},
"info": {
"totalMass": "Le poids a été mis à jour en fonction du nouvel engin de traction.",
"totalLength":"La longueur a été mise à jour en fonction du nouvel engin de traction.",
"maxSpeed":"La vitesse maximale a été mise à jour en fonction du nouvel engin de traction."
}
},
"datetimeOutsideWindow": "La date et l'heure doivent être comprises entre le {{low}} et le {{high}}",
Expand Down
99 changes: 68 additions & 31 deletions front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect, useState } from 'react';

import { Input, ComboBox, useDefaultComboBox } from '@osrd-project/ui-core';
import { useTranslation } from 'react-i18next';

Expand Down Expand Up @@ -63,6 +65,7 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
maxSpeed,
onMaxSpeedChange,
prefillConsist,
statusWithMessage,
} = useStdcmConsist();

const { filteredRollingStockList: rollingStocks } = useFilterRollingStock({ isStdcm: true });
Expand Down Expand Up @@ -94,6 +97,24 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
dispatchUpdateSpeedLimitByTag(newTag);
};

const [statusMessagesVisible, setStatusMessagesVisible] = useState({
mass: true,
length: true,
speed: true,
});

const handleStatusMessage = (key: 'mass' | 'length' | 'speed') => {
setStatusMessagesVisible((prevState) => ({ ...prevState, [key]: false }));
};

useEffect(() => {
setStatusMessagesVisible({
mass: true,
length: true,
speed: true,
});
}, [rollingStock]);

return (
<StdcmCard
name={t('consist.consist')}
Expand Down Expand Up @@ -139,19 +160,23 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
onChange={onTotalMassChange}
disabled={disabled}
statusWithMessage={
consistErrors?.totalMass
? {
status: 'error',
tooltip: 'left',
message: t(consistErrors.totalMass, {
low: Math.ceil(
kgToT((rollingStock?.mass ?? 0) + (towedRollingStock?.mass ?? 0))
),
high: CONSIST_TOTAL_MASS_MAX,
}),
}
statusMessagesVisible.mass
? statusWithMessage?.totalMass ||
(consistErrors?.totalMass
? {
status: 'error',
tooltip: 'left',
message: t(consistErrors.totalMass, {
low: Math.ceil(
kgToT((rollingStock?.mass ?? 0) + (towedRollingStock?.mass ?? 0))
),
high: CONSIST_TOTAL_MASS_MAX,
}),
}
: undefined)
: undefined
}
onCloseStatusMessage={() => handleStatusMessage('mass')}
/>
<Input
id="length"
Expand All @@ -163,17 +188,23 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
onChange={onTotalLengthChange}
disabled={disabled}
statusWithMessage={
consistErrors?.totalLength
? {
status: 'error',
tooltip: 'left',
message: t(consistErrors.totalLength, {
low: Math.ceil((rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0)),
high: CONSIST_TOTAL_LENGTH_MAX,
}),
}
statusMessagesVisible.length
? statusWithMessage?.totalLength ||
(consistErrors?.totalLength
? {
status: 'error',
tooltip: 'left',
message: t(consistErrors.totalLength, {
low: Math.ceil(
(rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0)
),
high: CONSIST_TOTAL_LENGTH_MAX,
}),
}
: undefined)
: undefined
}
onCloseStatusMessage={() => handleStatusMessage('length')}
/>
</div>
<div className="stdcm-consist__properties">
Expand All @@ -193,19 +224,25 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
onChange={onMaxSpeedChange}
disabled={disabled}
statusWithMessage={
consistErrors?.maxSpeed
? {
status: 'error',
tooltip: 'left',
message: t(consistErrors.maxSpeed, {
low: CONSIST_MAX_SPEED_MIN,
high: Math.floor(
msToKmh(Math.min(rollingStock?.max_speed ?? kmhToMs(CONSIST_MAX_SPEED_MIN)))
),
}),
}
statusMessagesVisible.speed
? statusWithMessage?.maxSpeed ||
(consistErrors?.maxSpeed
? {
status: 'error',
tooltip: 'left',
message: t(consistErrors.maxSpeed, {
low: CONSIST_MAX_SPEED_MIN,
high: Math.floor(
msToKmh(
Math.min(rollingStock?.max_speed ?? kmhToMs(CONSIST_MAX_SPEED_MIN))
)
),
}),
}
: undefined)
: undefined
}
onCloseStatusMessage={() => handleStatusMessage('speed')}
/>
</div>
</StdcmCard>
Expand Down
60 changes: 46 additions & 14 deletions front/src/applications/stdcm/hooks/useStdcmConsist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState } from 'react';

import type { InputProps } from '@osrd-project/ui-core';
import { min } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import type { LightRollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi';
Expand All @@ -13,12 +15,19 @@ import { kgToT, kmhToMs, msToKmh } from 'utils/physics';
import maxSpeedFromSpeedLimitByTag from '../utils/maxSpeedFromSpeedLimitByTag';

const useStdcmConsist = () => {
const { t } = useTranslation('stdcm');
const dispatch = useAppDispatch();

const [totalMassChanged, setTotalMassChanged] = useState(false);
const [totalLengthChanged, setTotalLengthChanged] = useState(false);
const [maxSpeedChanged, setMaxSpeedChanged] = useState(false);

const [statusWithMessage, setStatusWithMessage] = useState<{
totalMass?: InputProps['statusWithMessage'];
totalLength?: InputProps['statusWithMessage'];
maxSpeed?: InputProps['statusWithMessage'];
}>({});

const { getTotalMass, getTotalLength, getMaxSpeed } =
useOsrdConfSelectors() as StdcmConfSelectors;
const { updateTotalMass, updateTotalLength, updateMaxSpeed } =
Expand Down Expand Up @@ -50,25 +59,47 @@ const useStdcmConsist = () => {
towed?: TowedRollingStock,
maxSpeedTag?: string | null
) => {
if (!totalMassChanged) {
const consistMass = Math.ceil(kgToT((rollingStock?.mass ?? 0) + (towed?.mass ?? 0)));
dispatch(updateTotalMass(consistMass > 0 ? consistMass : undefined));
const newStatus: typeof statusWithMessage = {};

const consistMass = Math.ceil(kgToT((rollingStock?.mass ?? 0) + (towed?.mass ?? 0)));
dispatch(updateTotalMass(consistMass > 0 ? consistMass : undefined));
if (totalMassChanged) {
newStatus.totalMass = {
status: 'info',
message: t('consist.info.totalMass'),
tooltip: 'left',
};
}
setTotalMassChanged(false);

if (!totalLengthChanged) {
const consistLength = Math.ceil((rollingStock?.length ?? 0) + (towed?.length ?? 0));
dispatch(updateTotalLength(consistLength > 0 ? consistLength : undefined));
const consistLength = Math.ceil((rollingStock?.length ?? 0) + (towed?.length ?? 0));
dispatch(updateTotalLength(consistLength > 0 ? consistLength : undefined));
if (totalLengthChanged) {
newStatus.totalLength = {
status: 'info',
message: t('consist.info.totalLength'),
tooltip: 'left',
};
}
setTotalLengthChanged(false);

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));
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));
if (maxSpeedChanged) {
newStatus.maxSpeed = {
status: 'info',
message: t('consist.info.maxSpeed'),
tooltip: 'left',
};
}
setMaxSpeedChanged(false);

setStatusWithMessage(newStatus);
};

return {
Expand All @@ -79,6 +110,7 @@ const useStdcmConsist = () => {
maxSpeed,
onMaxSpeedChange,
prefillConsist,
statusWithMessage,
};
};

Expand Down

0 comments on commit 1522505

Please sign in to comment.