Skip to content

Commit

Permalink
front: use Duration for OsrdStdcmConfState grid margins
Browse files Browse the repository at this point in the history
Improves type safety.

Signed-off-by: Simon Ser <[email protected]>
  • Loading branch information
emersion committed Feb 28, 2025
1 parent 5d09f8f commit f060c6a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions front/src/applications/stdcm/utils/formatStdcmConf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getStepLocation from 'modules/pathfinding/helpers/getStepLocation';
import { setFailure } from 'reducers/main';
import type { OsrdStdcmConfState, StandardAllowance } from 'reducers/osrdconf/types';
import { dateTimeFormatting } from 'utils/date';
import { Duration } from 'utils/duration';
import type { Duration } from 'utils/duration';
import { kmhToMs, tToKg } from 'utils/physics';
import { minToMs } from 'utils/timeManipulation';

Expand Down Expand Up @@ -190,8 +190,8 @@ export const checkStdcmConf = (
maxSpeed,
towedRollingStockID,
margin: standardAllowance,
gridMarginBefore: new Duration({ seconds: gridMarginBefore }),
gridMarginAfter: new Duration({ seconds: gridMarginAfter }),
gridMarginBefore,
gridMarginAfter,
workScheduleGroupId,
temporarySpeedLimitGroupId,
electricalProfileSetId,
Expand Down
13 changes: 9 additions & 4 deletions front/src/modules/stdcmAllowances/components/StdcmAllowances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { getMargins } from 'reducers/osrdconf/stdcmConf/selectors';
import type { StandardAllowance } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { Duration } from 'utils/duration';
import { convertInputStringToNumber } from 'utils/strings';

const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
Expand Down Expand Up @@ -49,11 +50,13 @@ const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
<InputSNCF
id="standardAllowanceTypeGridMarginBefore"
type="number"
value={gridMarginBefore || ''}
value={gridMarginBefore?.total('second') || ''}
unit={ALLOWANCE_UNITS_KEYS.time}
onChange={(e) =>
dispatch(
updateGridMarginBefore(Math.abs(convertInputStringToNumber(e.target.value)))
updateGridMarginBefore(
new Duration({ seconds: Math.abs(convertInputStringToNumber(e.target.value)) })
)
)
}
disabled={disabled}
Expand All @@ -67,11 +70,13 @@ const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
<InputSNCF
id="standardAllowanceTypeGridMarginAfter"
type="number"
value={gridMarginAfter || ''}
value={gridMarginAfter?.total('second') || ''}
unit={ALLOWANCE_UNITS_KEYS.time}
onChange={(e) =>
dispatch(
updateGridMarginAfter(Math.abs(convertInputStringToNumber(e.target.value)))
updateGridMarginAfter(
new Duration({ seconds: Math.abs(convertInputStringToNumber(e.target.value)) })
)
)
}
disabled={disabled}
Expand Down
8 changes: 4 additions & 4 deletions front/src/reducers/osrdconf/stdcmConf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const stdcmConfInitialState: OsrdStdcmConfState = {
],
margins: {
standardAllowance: { type: 'time_per_distance', value: 4.5 },
gridMarginBefore: 15,
gridMarginAfter: 15,
gridMarginBefore: new Duration({ seconds: 15 }),
gridMarginAfter: new Duration({ seconds: 15 }),
},
totalMass: undefined,
totalLength: undefined,
Expand Down Expand Up @@ -113,8 +113,8 @@ export const stdcmConfSlice = createSlice({
resetMargins(state: Draft<OsrdStdcmConfState>) {
state.margins = {
standardAllowance: { type: 'time_per_distance', value: 4.5 },
gridMarginBefore: 15,
gridMarginAfter: 15,
gridMarginBefore: new Duration({ seconds: 15 }),
gridMarginAfter: new Duration({ seconds: 15 }),
};
},
updateStandardAllowance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ describe('stdcmConfReducers', () => {
});

it('should handle updateGridMarginBefore', () => {
const newGridMarginBefore = 5;
const newGridMarginBefore = new Duration({ seconds: 5 });
const store = createStore(initialStateSTDCMConfig);
store.dispatch(updateGridMarginBefore(newGridMarginBefore));
const state = store.getState()[stdcmConfSlice.name];
expect(state.margins.gridMarginBefore).toStrictEqual(newGridMarginBefore);
});

it('should handle updateGridMarginAfter', () => {
const newGridMarginAfter = 5;
const newGridMarginAfter = new Duration({ seconds: 5 });
const store = createStore(initialStateSTDCMConfig);
store.dispatch(updateGridMarginAfter(newGridMarginAfter));
const state = store.getState()[stdcmConfSlice.name];
Expand Down
4 changes: 2 additions & 2 deletions front/src/reducers/osrdconf/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export type OsrdStdcmConfState = OsrdConfState & {
stdcmPathSteps: StdcmPathStep[];
margins: {
standardAllowance?: StandardAllowance;
gridMarginBefore?: number;
gridMarginAfter?: number;
gridMarginBefore?: Duration;
gridMarginAfter?: Duration;
};
totalMass?: number;
totalLength?: number;
Expand Down

0 comments on commit f060c6a

Please sign in to comment.