Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: move grid margins props from commonConf to stdcmConf reducer #10203

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { extractMarkersInfo } from 'applications/stdcm/utils';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import useInfraStatus from 'modules/pathfinding/hooks/useInfraStatus';
import { Map } from 'modules/trainschedule/components/ManageTrainSchedule';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import { type StdcmConfSliceActions, resetMargins } from 'reducers/osrdconf/stdcmConf';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import { useAppDispatch } from 'store';

Expand Down Expand Up @@ -52,13 +52,7 @@ const StdcmConfig = ({

const { infra } = useInfraStatus();
const dispatch = useAppDispatch();
const {
updateGridMarginAfter,
updateGridMarginBefore,
updateStdcmStandardAllowance,
updateStdcmPathStep,
resetStdcmConfig,
} = useOsrdConfActions() as StdcmConfSliceActions;
const { updateStdcmPathStep, resetStdcmConfig } = useOsrdConfActions() as StdcmConfSliceActions;

const {
getStdcmOrigin,
Expand Down Expand Up @@ -120,9 +114,7 @@ const StdcmConfig = ({

useEffect(() => {
if (!isDebugMode) {
dispatch(updateGridMarginAfter(15));
dispatch(updateGridMarginBefore(15));
dispatch(updateStdcmStandardAllowance({ type: 'time_per_distance', value: 4.5 }));
dispatch(resetMargins());
}
}, [isDebugMode]);

Expand Down
9 changes: 3 additions & 6 deletions front/src/applications/stdcm/utils/formatStdcmConf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Dispatch } from 'redux';

import type { PathfindingItem, PostTimetableByIdStdcmApiArg } from 'common/api/osrdEditoastApi';
import getStepLocation from 'modules/pathfinding/helpers/getStepLocation';
import type { InfraState } from 'reducers/infra';
import { setFailure } from 'reducers/main';
import type { OsrdStdcmConfState, StandardAllowance } from 'reducers/osrdconf/types';
import { dateTimeFormatting } from 'utils/date';
Expand Down Expand Up @@ -35,7 +34,7 @@ type ValidStdcmConfig = {
export const checkStdcmConf = (
dispatch: Dispatch,
t: TFunction,
osrdconf: OsrdStdcmConfState & InfraState
osrdconf: OsrdStdcmConfState
): ValidStdcmConfig | null => {
const {
stdcmPathSteps: pathSteps,
Expand All @@ -44,9 +43,7 @@ export const checkStdcmConf = (
infraID,
rollingStockID,
towedRollingStockID,
standardStdcmAllowance,
gridMarginBefore,
gridMarginAfter,
margins: { standardAllowance, gridMarginBefore, gridMarginAfter },
searchDatetimeWindow,
workScheduleGroupId,
temporarySpeedLimitGroupId,
Expand Down Expand Up @@ -191,7 +188,7 @@ export const checkStdcmConf = (
totalLength,
maxSpeed,
towedRollingStockID,
margin: standardStdcmAllowance,
margin: standardAllowance,
gridMarginBefore,
gridMarginAfter,
workScheduleGroupId,
Expand Down
23 changes: 10 additions & 13 deletions front/src/modules/stdcmAllowances/components/StdcmAllowances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ import type { AllowanceValue } from 'applications/stdcm/types';
import InputGroupSNCF from 'common/BootstrapSNCF/InputGroupSNCF';
import type { InputGroupSNCFValue } from 'common/BootstrapSNCF/InputGroupSNCF';
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { ALLOWANCE_UNITS_KEYS } from 'modules/stdcmAllowances/allowancesConsts';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import {
updateGridMarginAfter,
updateGridMarginBefore,
updateStandardAllowance,
} from 'reducers/osrdconf/stdcmConf';
import { getMargins } from 'reducers/osrdconf/stdcmConf/selectors';
import type { StandardAllowance } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { convertInputStringToNumber } from 'utils/strings';

const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
const { t } = useTranslation('allowances');
const dispatch = useAppDispatch();
const { getGridMarginBefore, getGridMarginAfter, getStandardStdcmAllowance } =
useOsrdConfSelectors() as StdcmConfSelectors;
const { updateGridMarginAfter, updateGridMarginBefore, updateStdcmStandardAllowance } =
useOsrdConfActions() as StdcmConfSliceActions;
const gridMarginBefore = useSelector(getGridMarginBefore);
const gridMarginAfter = useSelector(getGridMarginAfter);
const stdcmStandardAllowance = useSelector(getStandardStdcmAllowance);
const { gridMarginAfter, gridMarginBefore, standardAllowance } = useSelector(getMargins);
const standardAllowanceTypes = [
{
id: 'percentage',
Expand All @@ -40,7 +37,7 @@ const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
value: newTypeValue.value === undefined ? undefined : Math.abs(newTypeValue.value),
};

dispatch(updateStdcmStandardAllowance(processedType));
dispatch(updateStandardAllowance(processedType));
};

return (
Expand Down Expand Up @@ -93,8 +90,8 @@ const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
options={standardAllowanceTypes}
onChange={onchangeType}
currentValue={{
unit: stdcmStandardAllowance?.type || 'percentage',
value: stdcmStandardAllowance?.value,
unit: standardAllowance?.type || 'percentage',
value: standardAllowance?.value,
}}
disabled={disabled}
/>
Expand Down
14 changes: 0 additions & 14 deletions front/src/reducers/osrdconf/osrdConfCommon/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,6 @@ const testCommonConfReducers = (slice: OperationalStudiesConfSlice | StdcmConfSl
});
});

it('should handle updateGridMarginBefore', () => {
const newGridMarginBefore = 5;
defaultStore.dispatch(slice.actions.updateGridMarginBefore(newGridMarginBefore));
const state = defaultStore.getState()[slice.name];
expect(state.gridMarginBefore).toStrictEqual(newGridMarginBefore);
});

it('should handle updateGridMarginAfter', () => {
const newGridMarginAfter = 5;
defaultStore.dispatch(slice.actions.updateGridMarginAfter(newGridMarginAfter));
const state = defaultStore.getState()[slice.name];
expect(state.gridMarginAfter).toStrictEqual(newGridMarginAfter);
});

it('should handle updatePathSteps', () => {
const pathSteps = testDataBuilder.buildPathSteps();
defaultStore.dispatch(slice.actions.updatePathSteps(pathSteps));
Expand Down
10 changes: 0 additions & 10 deletions front/src/reducers/osrdconf/osrdConfCommon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export const defaultCommonConf: OsrdConfState = {
rollingStockID: undefined,
powerRestriction: [],
speedLimitByTag: undefined,
gridMarginBefore: undefined,
gridMarginAfter: undefined,
...infraState,
// Corresponds to origin and destination not defined
pathSteps: [null, null],
Expand All @@ -35,8 +33,6 @@ interface CommonConfReducers<S extends OsrdConfState> extends InfraStateReducers
['updateElectricalProfileSetId']: CaseReducer<S, PayloadAction<S['electricalProfileSetId']>>;
['updateRollingStockID']: CaseReducer<S, PayloadAction<S['rollingStockID']>>;
['updateSpeedLimitByTag']: CaseReducer<S, PayloadAction<S['speedLimitByTag'] | null>>;
['updateGridMarginBefore']: CaseReducer<S, PayloadAction<S['gridMarginBefore']>>;
['updateGridMarginAfter']: CaseReducer<S, PayloadAction<S['gridMarginAfter']>>;
['updatePathSteps']: CaseReducer<S, PayloadAction<S['pathSteps']>>;
['replaceItinerary']: CaseReducer<S, PayloadAction<S['pathSteps']>>;
['deleteItinerary']: CaseReducer<S>;
Expand Down Expand Up @@ -69,12 +65,6 @@ export function buildCommonConfReducers<S extends OsrdConfState>(): CommonConfRe
updateSpeedLimitByTag(state: Draft<S>, action: PayloadAction<S['speedLimitByTag'] | null>) {
state.speedLimitByTag = action.payload === null ? undefined : action.payload;
},
updateGridMarginBefore(state: Draft<S>, action: PayloadAction<S['gridMarginBefore']>) {
state.gridMarginBefore = action.payload;
},
updateGridMarginAfter(state: Draft<S>, action: PayloadAction<S['gridMarginAfter']>) {
state.gridMarginAfter = action.payload;
},
// update path steps without changing the itinerary (only add vias on the existing pathfinding,
// add schedules, margins or power restrictions)
updatePathSteps(state: Draft<S>, action: PayloadAction<S['pathSteps']>) {
Expand Down
2 changes: 0 additions & 2 deletions front/src/reducers/osrdconf/osrdConfCommon/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ const buildCommonConfSelectors = (slice: OperationalStudiesConfSlice | StdcmConf
getSearchDatetimeWindow: makeOsrdConfSelector('searchDatetimeWindow'),
getRollingStockID: makeOsrdConfSelector('rollingStockID'),
getSpeedLimitByTag: makeOsrdConfSelector('speedLimitByTag'),
getGridMarginBefore: makeOsrdConfSelector('gridMarginBefore'),
getGridMarginAfter: makeOsrdConfSelector('gridMarginAfter'),
getPowerRestriction: makeOsrdConfSelector('powerRestriction'),
getPathSteps,
getOrigin: (state: RootState) => {
Expand Down
38 changes: 34 additions & 4 deletions front/src/reducers/osrdconf/stdcmConf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export const stdcmConfInitialState: OsrdStdcmConfState = {
tolerances: { before: DEFAULT_TOLERANCE, after: DEFAULT_TOLERANCE },
},
],
standardStdcmAllowance: undefined,
margins: {
standardAllowance: { type: 'time_per_distance', value: 4.5 },
gridMarginBefore: 15,
gridMarginAfter: 15,
},
totalMass: undefined,
totalLength: undefined,
maxSpeed: undefined,
Expand Down Expand Up @@ -103,11 +107,30 @@ export const stdcmConfSlice = createSlice({
state.stdcmPathSteps = action.payload.stdcmPathSteps;
state.speedLimitByTag = action.payload.speedLimitByTag;
},
updateStdcmStandardAllowance(
resetMargins(state: Draft<OsrdStdcmConfState>) {
state.margins = {
standardAllowance: { type: 'time_per_distance', value: 4.5 },
gridMarginBefore: 15,
gridMarginAfter: 15,
};
},
updateStandardAllowance(
state: Draft<OsrdStdcmConfState>,
action: PayloadAction<OsrdStdcmConfState['margins']['standardAllowance']>
) {
state.margins = { ...state.margins, standardAllowance: action.payload };
},
updateGridMarginBefore(
state: Draft<OsrdStdcmConfState>,
action: PayloadAction<OsrdStdcmConfState['standardStdcmAllowance']>
action: PayloadAction<OsrdStdcmConfState['margins']['gridMarginBefore']>
) {
state.standardStdcmAllowance = action.payload;
state.margins = { ...state.margins, gridMarginBefore: action.payload };
},
updateGridMarginAfter(
state: Draft<OsrdStdcmConfState>,
action: PayloadAction<OsrdStdcmConfState['margins']['gridMarginAfter']>
) {
state.margins = { ...state.margins, gridMarginAfter: action.payload };
},
updateStdcmEnvironment(
state: Draft<OsrdStdcmConfState>,
Expand Down Expand Up @@ -207,6 +230,13 @@ export const stdcmConfSlice = createSlice({

export const stdcmConfSliceActions = stdcmConfSlice.actions;

export const {
resetMargins,
updateGridMarginAfter,
updateGridMarginBefore,
updateStandardAllowance,
} = stdcmConfSliceActions;

export type StdcmConfSlice = typeof stdcmConfSlice;

export type StdcmConfSliceActions = typeof stdcmConfSliceActions;
Expand Down
6 changes: 4 additions & 2 deletions front/src/reducers/osrdconf/stdcmConf/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ const buildStdcmConfSelectors = () => {
return {
...commonConfSelectors,
getStdcmConf,
getStandardStdcmAllowance: makeOsrdConfSelector('standardStdcmAllowance'),

getMargins: makeOsrdConfSelector('margins'),
getTotalMass: makeOsrdConfSelector('totalMass'),
getTotalLength: makeOsrdConfSelector('totalLength'),
getMaxSpeed: makeOsrdConfSelector('maxSpeed'),
getTowedRollingStockID: makeOsrdConfSelector('towedRollingStockID'),

getStdcmPathSteps,
getStdcmOrigin: (state: RootState) => {
const pathSteps = getStdcmPathSteps(state);
Expand All @@ -43,7 +45,7 @@ const buildStdcmConfSelectors = () => {

const selectors = buildStdcmConfSelectors();

export const { getStdcmConf } = selectors;
export const { getStdcmConf, getMargins } = selectors;

export type StdcmConfSelectors = typeof selectors;

Expand Down
40 changes: 29 additions & 11 deletions front/src/reducers/osrdconf/stdcmConf/stdcmConfReducers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,38 @@ describe('stdcmConfReducers', () => {
expect(state).toEqual(stdcmConfInitialState);
});

it('should handle updateStdcmStandardAllowance', () => {
const initialTimeStandardAllowance = testDataBuilder.buildTimeStandardAllowance(10);
const store = createStore({
standardStdcmAllowance: initialTimeStandardAllowance,
});
describe('should handle margins update', () => {
it('should handle updateStandardAllowance', () => {
const initialTimeStandardAllowance = testDataBuilder.buildTimeStandardAllowance(10);
const store = createStore({
margins: { standardAllowance: initialTimeStandardAllowance },
});

const stateBefore = store.getState()[stdcmConfSlice.name];
expect(stateBefore.margins.standardAllowance).toBe(initialTimeStandardAllowance);

const stateBefore = store.getState()[stdcmConfSlice.name];
expect(stateBefore.standardStdcmAllowance).toBe(initialTimeStandardAllowance);
const newStandardAllowance = testDataBuilder.buildPercentageStandardAllowance(5);
store.dispatch(stdcmConfSliceActions.updateStandardAllowance(newStandardAllowance));

const stateAfter = store.getState()[stdcmConfSlice.name];
expect(stateAfter.margins.standardAllowance).toBe(newStandardAllowance);
});

const newStandardAllowance = testDataBuilder.buildPercentageStandardAllowance(5);
store.dispatch(stdcmConfSliceActions.updateStdcmStandardAllowance(newStandardAllowance));
it('should handle updateGridMarginBefore', () => {
const newGridMarginBefore = 5;
const store = createStore(initialStateSTDCMConfig);
store.dispatch(stdcmConfSliceActions.updateGridMarginBefore(newGridMarginBefore));
const state = store.getState()[stdcmConfSlice.name];
expect(state.margins.gridMarginBefore).toStrictEqual(newGridMarginBefore);
});

const stateAfter = store.getState()[stdcmConfSlice.name];
expect(stateAfter.standardStdcmAllowance).toBe(newStandardAllowance);
it('should handle updateGridMarginAfter', () => {
const newGridMarginAfter = 5;
const store = createStore(initialStateSTDCMConfig);
store.dispatch(stdcmConfSliceActions.updateGridMarginAfter(newGridMarginAfter));
const state = store.getState()[stdcmConfSlice.name];
expect(state.margins.gridMarginAfter).toStrictEqual(newGridMarginAfter);
});
});

it('should handle resetStdcmConfig', () => {
Expand Down
8 changes: 5 additions & 3 deletions front/src/reducers/osrdconf/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export type OsrdConfState = InfraState & {
rollingStockID?: number;
speedLimitByTag?: string;
powerRestriction: PowerRestriction[];
gridMarginBefore?: number;
gridMarginAfter?: number;
pathSteps: (PathStep | null)[];
};

Expand All @@ -39,7 +37,11 @@ export interface StandardAllowance {

export type OsrdStdcmConfState = OsrdConfState & {
stdcmPathSteps: StdcmPathStep[];
standardStdcmAllowance?: StandardAllowance;
margins: {
standardAllowance?: StandardAllowance;
gridMarginBefore?: number;
gridMarginAfter?: number;
};
totalMass?: number;
totalLength?: number;
maxSpeed?: number;
Expand Down
Loading