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 10, 2025
1 parent eeb8aa2 commit 9d86d3c
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 65 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
88 changes: 41 additions & 47 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 @@ -18,7 +17,6 @@ import useFilterRollingStock from 'modules/rollingStock/hooks/useFilterRollingSt
import useFilterTowedRollingStock from 'modules/towedRollingStock/hooks/useFilterTowedRollingStock';
import { 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,23 @@ const StdcmConsist = ({ isDebugMode, consistErrors = {}, disabled = false }: Std
maxSpeed,
onMaxSpeedChange,
prefillConsist,
statusWithMessage,
} = 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,13 +105,22 @@ 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);
};

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

return (
<StdcmCard
name={t('consist.consist')}
Expand Down Expand Up @@ -137,20 +165,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 +177,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 +198,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;
88 changes: 88 additions & 0 deletions front/src/applications/stdcm/hooks/useStatusWithMessage.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;
Loading

0 comments on commit 9d86d3c

Please sign in to comment.