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 Feb 11, 2025
1 parent 6774666 commit 7757f04
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 66 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 consist data.",
"totalLength": "The length has been updated to reflect the new consist data.",
"maxSpeed": "The max speed has been updated to reflect the new consist data."
}
},
"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 des nouvelles données de convoi.",
"totalLength": "La longueur a été mise à jour en fonction des nouvelles données de convoi.",
"maxSpeed": "La vitesse maximale a été mise à jour en fonction des nouvelles données de convoi."
}
},
"datetimeOutsideWindow": "La date et l'heure doivent être comprises entre le {{low}} et le {{high}}",
Expand Down
92 changes: 44 additions & 48 deletions front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useState } from 'react';

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

import useStatusWithMessage from 'applications/stdcm/hooks/useConsistFieldStatus';
import useStdcmTowedRollingStock from 'applications/stdcm/hooks/useStdcmTowedRollingStock';
import type { ConsistErrors } from 'applications/stdcm/types';
import {
CONSIST_MAX_SPEED_MIN,
CONSIST_TOTAL_LENGTH_MAX,
CONSIST_TOTAL_MASS_MAX,
} from 'applications/stdcm/utils/consistValidation';
import calculateConsistMaxSpeed from 'applications/stdcm/utils/calculateConsistMaxSpeed';
import type { LightRollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
import SpeedLimitByTagSelector from 'common/SpeedLimitByTagSelector/SpeedLimitByTagSelector';
Expand All @@ -16,9 +15,8 @@ import RollingStock2Img from 'modules/rollingStock/components/RollingStock2Img';
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import useFilterRollingStock from 'modules/rollingStock/hooks/useFilterRollingStock';
import useFilterTowedRollingStock from 'modules/towedRollingStock/hooks/useFilterTowedRollingStock';
import { updateTowedRollingStockID } from 'reducers/osrdconf/stdcmConf';
import { updateMaxSpeed, updateTowedRollingStockID } from 'reducers/osrdconf/stdcmConf';
import { useAppDispatch } from 'store';
import { kgToT, kmhToMs, msToKmh } from 'utils/physics';

import StdcmCard from './StdcmCard';
import useStdcmConsist from '../../hooks/useStdcmConsist';
Expand Down Expand Up @@ -54,6 +52,12 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
const { rollingStock } = useStoreDataForRollingStockSelector();
const towedRollingStock = useStdcmTowedRollingStock();

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

const {
totalMass,
onTotalMassChange,
Expand All @@ -62,8 +66,24 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
maxSpeed,
onMaxSpeedChange,
prefillConsist,
statusWithMessage,
setMaxSpeedChanged,
} = useStdcmConsist();

const createFieldStatus = (field: 'totalMass' | 'totalLength' | 'maxSpeed') =>
useStatusWithMessage(
field,
statusWithMessage,
consistErrors,
statusMessagesVisible,
rollingStock,
towedRollingStock
);

const massFieldStatus = createFieldStatus('totalMass');
const lengthFieldStatus = createFieldStatus('totalLength');
const speedFieldStatus = createFieldStatus('maxSpeed');

const { filteredRollingStockList: rollingStocks } = useFilterRollingStock({ isStdcm: true });

const { filteredTowedRollingStockList: towedRollingStocks } = useFilterTowedRollingStock({
Expand All @@ -86,11 +106,21 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
const handleRollingStockSelect = (option?: LightRollingStockWithLiveries) => {
prefillConsist(option, towedRollingStock, speedLimitByTag);
dispatch(updateRollingStockID(option?.id));
setStatusMessagesVisible({
mass: true,
length: true,
speed: true,
});
};

const onSpeedLimitByTagChange = (newTag: string | null) => {
prefillConsist(rollingStock, towedRollingStock, newTag);
dispatch(updateMaxSpeed(calculateConsistMaxSpeed(rollingStock, towedRollingStock, newTag)));
dispatchUpdateSpeedLimitByTag(newTag);
setMaxSpeedChanged(false);
};

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

return (
Expand Down Expand Up @@ -137,20 +167,8 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
value={totalMass ?? ''}
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,
}),
}
: undefined
}
statusWithMessage={massFieldStatus}
onCloseStatusMessage={() => handleCloseStatusMessage('mass')}
/>
<Input
id="length"
Expand All @@ -161,18 +179,8 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
value={totalLength ?? ''}
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,
}),
}
: undefined
}
statusWithMessage={lengthFieldStatus}
onCloseStatusMessage={() => handleCloseStatusMessage('length')}
/>
</div>
<div className="stdcm-consist__properties">
Expand All @@ -192,20 +200,8 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
value={maxSpeed ?? ''}
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)))
),
}),
}
: undefined
}
statusWithMessage={speedFieldStatus}
onCloseStatusMessage={() => handleCloseStatusMessage('speed')}
/>
</div>
</StdcmCard>
Expand Down
88 changes: 88 additions & 0 deletions front/src/applications/stdcm/hooks/useConsistFieldStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import type { InputProps } from '@osrd-project/ui-core';
import type { StatusWithMessage } from '@osrd-project/ui-core/dist/components/inputs/StatusMessage';
import { useTranslation } from 'react-i18next';

import type { RollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi';
import { kgToT, kmhToMs, msToKmh } from 'utils/physics';

import type { ConsistErrors } from '../types';
import {
CONSIST_MAX_SPEED_MIN,
CONSIST_TOTAL_LENGTH_MAX,
CONSIST_TOTAL_MASS_MAX,
} from '../utils/consistValidation';

const useConsistFieldStatus = (
type: 'totalMass' | 'totalLength' | 'maxSpeed',
statusWithMessage: {
totalMass?: InputProps['statusWithMessage'];
totalLength?: InputProps['statusWithMessage'];
maxSpeed?: InputProps['statusWithMessage'];
},
consistErrors: ConsistErrors,
statusMessagesVisible: { mass: boolean; length: boolean; speed: boolean },
rollingStock: RollingStockWithLiveries | undefined,
towedRollingStock: TowedRollingStock | undefined
): StatusWithMessage | undefined => {
const { t } = useTranslation('stdcm');

switch (type) {
case 'totalMass': {
if (consistErrors?.totalMass) {
return {
status: 'error',
tooltip: 'left',
message: t(consistErrors.totalMass, {
low: Math.ceil(kgToT((rollingStock?.mass ?? 0) + (towedRollingStock?.mass ?? 0))),
high: CONSIST_TOTAL_MASS_MAX,
}),
};
}
if (statusMessagesVisible.mass) {
return statusWithMessage?.totalMass;
}
return undefined;
}

case 'totalLength': {
if (consistErrors?.totalLength) {
return {
status: 'error',
tooltip: 'left',
message: t(consistErrors.totalLength, {
low: Math.ceil((rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0)),
high: CONSIST_TOTAL_LENGTH_MAX,
}),
};
}
if (statusMessagesVisible.length) {
return statusWithMessage?.totalLength;
}
return undefined;
}

case 'maxSpeed': {
if (consistErrors?.maxSpeed) {
return {
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)))
),
}),
};
}
if (statusMessagesVisible.speed) {
return statusWithMessage?.maxSpeed;
}
return undefined;
}

default:
return undefined;
}
};

export default useConsistFieldStatus;
60 changes: 43 additions & 17 deletions front/src/applications/stdcm/hooks/useStdcmConsist.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import { useState } from 'react';

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

import type { LightRollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi';
import { updateTotalMass, updateTotalLength, updateMaxSpeed } from 'reducers/osrdconf/stdcmConf';
import { getTotalMass, getTotalLength, getMaxSpeed } from 'reducers/osrdconf/stdcmConf/selectors';
import { useAppDispatch } from 'store';
import { kgToT, kmhToMs, msToKmh } from 'utils/physics';
import { kgToT } from 'utils/physics';

import maxSpeedFromSpeedLimitByTag from '../utils/maxSpeedFromSpeedLimitByTag';
import calculateConsistMaxSpeed from '../utils/calculateConsistMaxSpeed';

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 totalMass = useSelector(getTotalMass);
const onTotalMassChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const totalMassValue = Number(e.target.value);
Expand All @@ -44,25 +52,41 @@ 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 && totalMass !== undefined) {
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 && totalLength !== undefined) {
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));
dispatch(updateMaxSpeed(calculateConsistMaxSpeed(rollingStock, towed, maxSpeedTag)));
if (maxSpeedChanged && maxSpeed !== undefined) {
newStatus.maxSpeed = {
status: 'info',
message: t('consist.info.maxSpeed'),
tooltip: 'left',
};
}
setMaxSpeedChanged(false);

setStatusWithMessage(newStatus);
};

return {
Expand All @@ -73,6 +97,8 @@ const useStdcmConsist = () => {
maxSpeed,
onMaxSpeedChange,
prefillConsist,
statusWithMessage,
setMaxSpeedChanged,
};
};

Expand Down
Loading

0 comments on commit 7757f04

Please sign in to comment.