Skip to content

Commit

Permalink
front: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Wadjetz committed Nov 18, 2024
1 parent 4db9236 commit ea9b2ba
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 100 deletions.
9 changes: 4 additions & 5 deletions front/public/locales/en/stdcm.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
"errors": {
"totalMass": {
"negative": "Must be positive.",
"lowerThanTractionEngine": "Must not be less than engine mass.",
"lowerThanConsist": "Must not be less than consist mass."
"range": "The total weight must be between {{low}} and {{high}}t"
},
"totalLength": {
"negative": "Must be positive.",
"lowerThanTractionEngine": "Must not be less than engine length.",
"lowerThanConsist": "Must not be less than consist length."
"range": "The total length must be between {{low}} and {{high}}m"
},
"maxSpeed": {
"negative": "Must be positive."
"negative": "Must be positive.",
"range": "The max speed must be between {{low}} and {{high}}km/h"
}
}
},
Expand Down
9 changes: 4 additions & 5 deletions front/public/locales/fr/stdcm.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
"errors": {
"totalMass": {
"negative": "Doit être positif.",
"lowerThanTractionEngine": "Ne doit pas être inférieur à la masse de l'engin de traction.",
"lowerThanConsist": "Ne doit pas être inférieur à la masse du convoi."
"range": "Le tonnage total doit être compris entre {{low}} et {{high}}t"
},
"totalLength": {
"negative": "Doit être positif.",
"lowerThanTractionEngine": "Ne doit pas être inférieur à la longueur de l'engin de traction.",
"lowerThanConsist": "Ne doit pas être inférieur à la longueur du convoi."
"range": "La longueur totale doit être comprise entre {{low}} et {{high}}m"
},
"maxSpeed": {
"negative": "Doit être positif."
"negative": "Doit être positif.",
"range": "La vitesse max. doit être comprise entre {{low}} et {{high}}km/h"
}
}
},
Expand Down
45 changes: 41 additions & 4 deletions front/src/applications/stdcm/components/StdcmForm/StdcmConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import { compact } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import useStdcmTowedRollingStock from 'applications/stdcm/hooks/useStdcmTowedRollingStock';
import {
validateMaxSpeed,
validateTotalLength,
validateTotalMass,
} from 'applications/stdcm/utils/consistValidation';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import useInfraStatus from 'modules/pathfinding/hooks/useInfraStatus';
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import { Map } from 'modules/trainschedule/components/ManageTrainSchedule';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
Expand All @@ -19,7 +26,7 @@ import StdcmDestination from './StdcmDestination';
import StdcmLinkedPathSearch from './StdcmLinkedPathSearch';
import StdcmOrigin from './StdcmOrigin';
import useStaticPathfinding from '../../hooks/useStaticPathfinding';
import type { StdcmConfigErrors } from '../../types';
import type { ConsistErrors, StdcmConfigErrors } from '../../types';
import StdcmSimulationParams from '../StdcmSimulationParams';
import StdcmVias from './StdcmVias';
import { ArrivalTimeTypes, StdcmConfigErrorTypes } from '../../types';
Expand Down Expand Up @@ -47,6 +54,8 @@ const StdcmConfig = ({
}: StdcmConfigProps) => {
const { t } = useTranslation('stdcm');

const [consistErrors, setConsistErrors] = useState<ConsistErrors>({});

const { infra } = useInfraStatus();
const dispatch = useAppDispatch();
const {
Expand All @@ -63,20 +72,48 @@ const StdcmConfig = ({
getProjectID,
getScenarioID,
getStudyID,
getConsistErrors,
getTotalMass,
getTotalLength,
getMaxSpeed,
} = useOsrdConfSelectors() as StdcmConfSelectors;
const origin = useSelector(getStdcmOrigin);
const pathSteps = useSelector(getStdcmPathSteps);
const destination = useSelector(getStdcmDestination);
const projectID = useSelector(getProjectID);
const studyID = useSelector(getStudyID);
const scenarioID = useSelector(getScenarioID);
const consistErrors = useSelector(getConsistErrors);

const totalMass = useSelector(getTotalMass);
const totalLength = useSelector(getTotalLength);
const maxSpeed = useSelector(getMaxSpeed);

const pathfinding = useStaticPathfinding(infra);

const [formErrors, setFormErrors] = useState<StdcmConfigErrors>();

const { rollingStock } = useStoreDataForRollingStockSelector();
const towedRollingStock = useStdcmTowedRollingStock();

useEffect(() => {
const totalMassError = validateTotalMass({
tractionEngineMass: rollingStock?.mass,
towedMass: towedRollingStock?.mass,
totalMass,
});

const totalLengthError = validateTotalLength({
tractionEngineLength: rollingStock?.length,
towedLength: towedRollingStock?.length,
totalLength,
});

setConsistErrors({
totalMass: totalMassError,
totalLength: totalLengthError,
maxSpeed: validateMaxSpeed(maxSpeed, rollingStock?.max_speed),
});
}, [rollingStock, towedRollingStock, totalMass, totalLength, maxSpeed]);

const disabled = isPending || retainedSimulationIndex > -1;

const startSimulation = () => {
Expand Down Expand Up @@ -155,7 +192,7 @@ const StdcmConfig = ({
/>
<div className="stdcm-simulation-inputs">
<div className="stdcm-consist-container">
<StdcmConsist disabled={disabled} />
<StdcmConsist consistErrors={consistErrors} disabled={disabled} />
</div>
<div className="stdcm__separator" />
<div className="stdcm-simulation-itinerary">
Expand Down
55 changes: 21 additions & 34 deletions front/src/applications/stdcm/components/StdcmForm/StdcmConsist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { useTranslation } from 'react-i18next';

import useStdcmTowedRollingStock from 'applications/stdcm/hooks/useStdcmTowedRollingStock';
import {
validateMaxSpeed,
validateTotalLength,
validateTotalMass,
CONSIST_MAX_SPEED_MIN,
CONSIST_TOTAL_LENGTH_MAX,
CONSIST_TOTAL_MASS_MAX,
} from 'applications/stdcm/utils/consistValidation';
import type { LightRollingStockWithLiveries, TowedRollingStock } from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
Expand All @@ -20,6 +20,7 @@ import useFilterRollingStock from 'modules/rollingStock/hooks/useFilterRollingSt
import useFilterTowedRollingStock from 'modules/towedRollingStock/hooks/useFilterTowedRollingStock';
import { type StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import { useAppDispatch } from 'store';
import { kgToT } from 'utils/physics';

import StdcmCard from './StdcmCard';
import useStdcmConsist from '../../hooks/useStdcmConsist';
Expand All @@ -39,12 +40,12 @@ const ConsistCardTitle = ({
);
};

const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
const StdcmConsist = ({ consistErrors = {}, disabled = false }: StdcmConfigCardProps) => {
const { t } = useTranslation('stdcm');
const { speedLimitByTag, speedLimitsByTags, dispatchUpdateSpeedLimitByTag } =
useStoreDataForSpeedLimitByTagSelector({ isStdcm: true });

const { updateRollingStockID, updateConsistErrors, updateTowedRollingStockID } =
const { updateRollingStockID, updateTowedRollingStockID } =
useOsrdConfActions() as StdcmConfSliceActions;
const dispatch = useAppDispatch();

Expand All @@ -58,7 +59,6 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
onTotalLengthChange,
maxSpeed,
onMaxSpeedChange,
consistErrors,
} = useStdcmConsist();

const { filters, searchRollingStock, searchRollingStockById, filteredRollingStockList } =
Expand Down Expand Up @@ -123,28 +123,6 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
}
}, [rollingStock]);

useEffect(() => {
const totalMassError = validateTotalMass(
rollingStock?.mass,
towedRollingStock?.mass,
totalMass
);

const totalLengthError = validateTotalLength(
rollingStock?.length,
towedRollingStock?.length,
totalLength
);

dispatch(
updateConsistErrors({
totalMass: totalMassError,
totalLength: totalLengthError,
maxSpeed: validateMaxSpeed(maxSpeed),
})
);
}, [rollingStock, towedRollingStock, totalMass, totalLength, maxSpeed]);

return (
<StdcmCard
name={t('consist.consist')}
Expand Down Expand Up @@ -209,8 +187,11 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
statusWithMessage={
consistErrors?.totalMass
? {
status: 'error',
message: t(consistErrors.totalMass),
status: 'warning',
message: t(consistErrors.totalMass, {
low: kgToT((rollingStock?.mass ?? 0) + (towedRollingStock?.mass ?? 0)),
high: CONSIST_TOTAL_MASS_MAX,
}),
}
: undefined
}
Expand All @@ -226,8 +207,11 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
statusWithMessage={
consistErrors?.totalLength
? {
status: 'error',
message: t(consistErrors.totalLength),
status: 'warning',
message: t(consistErrors.totalLength, {
low: (rollingStock?.length ?? 0) + (towedRollingStock?.length ?? 0),
high: CONSIST_TOTAL_LENGTH_MAX,
}),
}
: undefined
}
Expand Down Expand Up @@ -256,8 +240,11 @@ const StdcmConsist = ({ disabled = false }: StdcmConfigCardProps) => {
statusWithMessage={
consistErrors?.maxSpeed
? {
status: 'error',
message: t(consistErrors.maxSpeed),
status: 'warning',
message: t(consistErrors.maxSpeed, {
low: CONSIST_MAX_SPEED_MIN,
high: Math.min(rollingStock?.max_speed ?? CONSIST_MAX_SPEED_MIN),
}),
}
: undefined
}
Expand Down
5 changes: 1 addition & 4 deletions front/src/applications/stdcm/hooks/useStdcmConsist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useAppDispatch } from 'store';

const useStdcmConsist = () => {
const dispatch = useAppDispatch();
const { getTotalMass, getTotalLength, getMaxSpeed, getConsistErrors } =
const { getTotalMass, getTotalLength, getMaxSpeed } =
useOsrdConfSelectors() as StdcmConfSelectors;
const { updateTotalMass, updateTotalLength, updateMaxSpeed } =
useOsrdConfActions() as StdcmConfSliceActions;
Expand All @@ -30,16 +30,13 @@ const useStdcmConsist = () => {
dispatch(updateMaxSpeed(totalMaxSpeed === 0 ? undefined : totalMaxSpeed));
};

const consistErrors = useSelector(getConsistErrors);

return {
totalMass,
onTotalMassChange,
totalLength,
onTotalLengthChange,
maxSpeed,
onMaxSpeedChange,
consistErrors,
};
};

Expand Down
7 changes: 7 additions & 0 deletions front/src/applications/stdcm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ type StdcmResultsOperationalPoint = {
trackName?: string;
};

export type ConsistErrors = {
totalMass?: string;
totalLength?: string;
maxSpeed?: string;
};

export type StdcmResults = {
stdcmResponse: StdcmSuccessResponse;
speedSpaceChartData: SpeedSpaceChartData | null;
Expand Down Expand Up @@ -149,6 +155,7 @@ export type StdcmSimulation = {
/** This type is used for StdcmConsist, StdcmOrigin, StdcmDestination and StdcmVias components */
export type StdcmConfigCardProps = {
disabled?: boolean;
consistErrors?: ConsistErrors;
};

export enum ArrivalTimeTypes {
Expand Down
Loading

0 comments on commit ea9b2ba

Please sign in to comment.