Skip to content

Commit 177e8e8

Browse files
committed
front: move gridMargins from commonConf to stdcmConf
Also rename stdcmStandardAllowance and group all the margins values together in the store Signed-off-by: Clara Ni <[email protected]>
1 parent 31b135a commit 177e8e8

File tree

10 files changed

+82
-74
lines changed

10 files changed

+82
-74
lines changed

front/src/applications/stdcm/components/StdcmForm/StdcmConfig.tsx

+8-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { extractMarkersInfo } from 'applications/stdcm/utils';
99
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
1010
import useInfraStatus from 'modules/pathfinding/hooks/useInfraStatus';
1111
import { Map } from 'modules/trainschedule/components/ManageTrainSchedule';
12-
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
12+
import {
13+
updateGridMarginAfter,
14+
updateGridMarginBefore,
15+
updateStandardAllowance,
16+
type StdcmConfSliceActions,
17+
} from 'reducers/osrdconf/stdcmConf';
1318
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
1419
import { useAppDispatch } from 'store';
1520

@@ -52,13 +57,7 @@ const StdcmConfig = ({
5257

5358
const { infra } = useInfraStatus();
5459
const dispatch = useAppDispatch();
55-
const {
56-
updateGridMarginAfter,
57-
updateGridMarginBefore,
58-
updateStdcmStandardAllowance,
59-
updateStdcmPathStep,
60-
resetStdcmConfig,
61-
} = useOsrdConfActions() as StdcmConfSliceActions;
60+
const { updateStdcmPathStep, resetStdcmConfig } = useOsrdConfActions() as StdcmConfSliceActions;
6261

6362
const {
6463
getStdcmOrigin,
@@ -122,7 +121,7 @@ const StdcmConfig = ({
122121
if (!isDebugMode) {
123122
dispatch(updateGridMarginAfter(15));
124123
dispatch(updateGridMarginBefore(15));
125-
dispatch(updateStdcmStandardAllowance({ type: 'time_per_distance', value: 4.5 }));
124+
dispatch(updateStandardAllowance({ type: 'time_per_distance', value: 4.5 }));
126125
}
127126
}, [isDebugMode]);
128127

front/src/applications/stdcm/utils/formatStdcmConf.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { Dispatch } from 'redux';
44

55
import type { PathfindingItem, PostTimetableByIdStdcmApiArg } from 'common/api/osrdEditoastApi';
66
import getStepLocation from 'modules/pathfinding/helpers/getStepLocation';
7-
import type { InfraState } from 'reducers/infra';
87
import { setFailure } from 'reducers/main';
98
import type { OsrdStdcmConfState, StandardAllowance } from 'reducers/osrdconf/types';
109
import { dateTimeFormatting } from 'utils/date';
@@ -35,7 +34,7 @@ type ValidStdcmConfig = {
3534
export const checkStdcmConf = (
3635
dispatch: Dispatch,
3736
t: TFunction,
38-
osrdconf: OsrdStdcmConfState & InfraState
37+
osrdconf: OsrdStdcmConfState
3938
): ValidStdcmConfig | null => {
4039
const {
4140
stdcmPathSteps: pathSteps,
@@ -44,9 +43,7 @@ export const checkStdcmConf = (
4443
infraID,
4544
rollingStockID,
4645
towedRollingStockID,
47-
standardStdcmAllowance,
48-
gridMarginBefore,
49-
gridMarginAfter,
46+
margins: { standardAllowance, gridMarginBefore, gridMarginAfter },
5047
searchDatetimeWindow,
5148
workScheduleGroupId,
5249
temporarySpeedLimitGroupId,
@@ -191,7 +188,7 @@ export const checkStdcmConf = (
191188
totalLength,
192189
maxSpeed,
193190
towedRollingStockID,
194-
margin: standardStdcmAllowance,
191+
margin: standardAllowance,
195192
gridMarginBefore,
196193
gridMarginAfter,
197194
workScheduleGroupId,

front/src/modules/stdcmAllowances/components/StdcmAllowances.tsx

+10-13
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,21 @@ import type { AllowanceValue } from 'applications/stdcm/types';
55
import InputGroupSNCF from 'common/BootstrapSNCF/InputGroupSNCF';
66
import type { InputGroupSNCFValue } from 'common/BootstrapSNCF/InputGroupSNCF';
77
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
8-
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
98
import { ALLOWANCE_UNITS_KEYS } from 'modules/stdcmAllowances/allowancesConsts';
10-
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
11-
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
9+
import {
10+
updateGridMarginAfter,
11+
updateGridMarginBefore,
12+
updateStandardAllowance,
13+
} from 'reducers/osrdconf/stdcmConf';
14+
import { getMargins } from 'reducers/osrdconf/stdcmConf/selectors';
1215
import type { StandardAllowance } from 'reducers/osrdconf/types';
1316
import { useAppDispatch } from 'store';
1417
import { convertInputStringToNumber } from 'utils/strings';
1518

1619
const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
1720
const { t } = useTranslation('allowances');
1821
const dispatch = useAppDispatch();
19-
const { getGridMarginBefore, getGridMarginAfter, getStandardStdcmAllowance } =
20-
useOsrdConfSelectors() as StdcmConfSelectors;
21-
const { updateGridMarginAfter, updateGridMarginBefore, updateStdcmStandardAllowance } =
22-
useOsrdConfActions() as StdcmConfSliceActions;
23-
const gridMarginBefore = useSelector(getGridMarginBefore);
24-
const gridMarginAfter = useSelector(getGridMarginAfter);
25-
const stdcmStandardAllowance = useSelector(getStandardStdcmAllowance);
22+
const { gridMarginAfter, gridMarginBefore, standardAllowance } = useSelector(getMargins);
2623
const standardAllowanceTypes = [
2724
{
2825
id: 'percentage',
@@ -40,7 +37,7 @@ const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
4037
value: newTypeValue.value === undefined ? undefined : Math.abs(newTypeValue.value),
4138
};
4239

43-
dispatch(updateStdcmStandardAllowance(processedType));
40+
dispatch(updateStandardAllowance(processedType));
4441
};
4542

4643
return (
@@ -93,8 +90,8 @@ const StdcmAllowances = ({ disabled = false }: { disabled?: boolean }) => {
9390
options={standardAllowanceTypes}
9491
onChange={onchangeType}
9592
currentValue={{
96-
unit: stdcmStandardAllowance?.type || 'percentage',
97-
value: stdcmStandardAllowance?.value,
93+
unit: standardAllowance?.type || 'percentage',
94+
value: standardAllowance?.value,
9895
}}
9996
disabled={disabled}
10097
/>

front/src/reducers/osrdconf/osrdConfCommon/__tests__/utils.ts

-14
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,6 @@ const testCommonConfReducers = (slice: OperationalStudiesConfSlice | StdcmConfSl
121121
});
122122
});
123123

124-
it('should handle updateGridMarginBefore', () => {
125-
const newGridMarginBefore = 5;
126-
defaultStore.dispatch(slice.actions.updateGridMarginBefore(newGridMarginBefore));
127-
const state = defaultStore.getState()[slice.name];
128-
expect(state.gridMarginBefore).toStrictEqual(newGridMarginBefore);
129-
});
130-
131-
it('should handle updateGridMarginAfter', () => {
132-
const newGridMarginAfter = 5;
133-
defaultStore.dispatch(slice.actions.updateGridMarginAfter(newGridMarginAfter));
134-
const state = defaultStore.getState()[slice.name];
135-
expect(state.gridMarginAfter).toStrictEqual(newGridMarginAfter);
136-
});
137-
138124
it('should handle updatePathSteps', () => {
139125
const pathSteps = testDataBuilder.buildPathSteps();
140126
defaultStore.dispatch(slice.actions.updatePathSteps(pathSteps));

front/src/reducers/osrdconf/osrdConfCommon/index.ts

-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export const defaultCommonConf: OsrdConfState = {
2020
rollingStockID: undefined,
2121
powerRestriction: [],
2222
speedLimitByTag: undefined,
23-
gridMarginBefore: undefined,
24-
gridMarginAfter: undefined,
2523
...infraState,
2624
// Corresponds to origin and destination not defined
2725
pathSteps: [null, null],
@@ -35,8 +33,6 @@ interface CommonConfReducers<S extends OsrdConfState> extends InfraStateReducers
3533
['updateElectricalProfileSetId']: CaseReducer<S, PayloadAction<S['electricalProfileSetId']>>;
3634
['updateRollingStockID']: CaseReducer<S, PayloadAction<S['rollingStockID']>>;
3735
['updateSpeedLimitByTag']: CaseReducer<S, PayloadAction<S['speedLimitByTag'] | null>>;
38-
['updateGridMarginBefore']: CaseReducer<S, PayloadAction<S['gridMarginBefore']>>;
39-
['updateGridMarginAfter']: CaseReducer<S, PayloadAction<S['gridMarginAfter']>>;
4036
['updatePathSteps']: CaseReducer<S, PayloadAction<S['pathSteps']>>;
4137
['replaceItinerary']: CaseReducer<S, PayloadAction<S['pathSteps']>>;
4238
['deleteItinerary']: CaseReducer<S>;
@@ -69,12 +65,6 @@ export function buildCommonConfReducers<S extends OsrdConfState>(): CommonConfRe
6965
updateSpeedLimitByTag(state: Draft<S>, action: PayloadAction<S['speedLimitByTag'] | null>) {
7066
state.speedLimitByTag = action.payload === null ? undefined : action.payload;
7167
},
72-
updateGridMarginBefore(state: Draft<S>, action: PayloadAction<S['gridMarginBefore']>) {
73-
state.gridMarginBefore = action.payload;
74-
},
75-
updateGridMarginAfter(state: Draft<S>, action: PayloadAction<S['gridMarginAfter']>) {
76-
state.gridMarginAfter = action.payload;
77-
},
7868
// update path steps without changing the itinerary (only add vias on the existing pathfinding,
7969
// add schedules, margins or power restrictions)
8070
updatePathSteps(state: Draft<S>, action: PayloadAction<S['pathSteps']>) {

front/src/reducers/osrdconf/osrdConfCommon/selectors.ts

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ const buildCommonConfSelectors = (slice: OperationalStudiesConfSlice | StdcmConf
3131
getSearchDatetimeWindow: makeOsrdConfSelector('searchDatetimeWindow'),
3232
getRollingStockID: makeOsrdConfSelector('rollingStockID'),
3333
getSpeedLimitByTag: makeOsrdConfSelector('speedLimitByTag'),
34-
getGridMarginBefore: makeOsrdConfSelector('gridMarginBefore'),
35-
getGridMarginAfter: makeOsrdConfSelector('gridMarginAfter'),
3634
getPowerRestriction: makeOsrdConfSelector('powerRestriction'),
3735
getPathSteps,
3836
getOrigin: (state: RootState) => {

front/src/reducers/osrdconf/stdcmConf/index.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export const stdcmConfInitialState: OsrdStdcmConfState = {
3030
tolerances: { before: DEFAULT_TOLERANCE, after: DEFAULT_TOLERANCE },
3131
},
3232
],
33-
standardStdcmAllowance: undefined,
33+
margins: {
34+
standardAllowance: undefined,
35+
gridMarginBefore: undefined,
36+
gridMarginAfter: undefined,
37+
},
3438
totalMass: undefined,
3539
totalLength: undefined,
3640
maxSpeed: undefined,
@@ -103,11 +107,23 @@ export const stdcmConfSlice = createSlice({
103107
state.stdcmPathSteps = action.payload.stdcmPathSteps;
104108
state.speedLimitByTag = action.payload.speedLimitByTag;
105109
},
106-
updateStdcmStandardAllowance(
110+
updateStandardAllowance(
111+
state: Draft<OsrdStdcmConfState>,
112+
action: PayloadAction<OsrdStdcmConfState['margins']['standardAllowance']>
113+
) {
114+
state.margins = { ...state.margins, standardAllowance: action.payload };
115+
},
116+
updateGridMarginBefore(
107117
state: Draft<OsrdStdcmConfState>,
108-
action: PayloadAction<OsrdStdcmConfState['standardStdcmAllowance']>
118+
action: PayloadAction<OsrdStdcmConfState['margins']['gridMarginBefore']>
109119
) {
110-
state.standardStdcmAllowance = action.payload;
120+
state.margins = { ...state.margins, gridMarginBefore: action.payload };
121+
},
122+
updateGridMarginAfter(
123+
state: Draft<OsrdStdcmConfState>,
124+
action: PayloadAction<OsrdStdcmConfState['margins']['gridMarginAfter']>
125+
) {
126+
state.margins = { ...state.margins, gridMarginAfter: action.payload };
111127
},
112128
updateStdcmEnvironment(
113129
state: Draft<OsrdStdcmConfState>,
@@ -207,6 +223,9 @@ export const stdcmConfSlice = createSlice({
207223

208224
export const stdcmConfSliceActions = stdcmConfSlice.actions;
209225

226+
export const { updateGridMarginAfter, updateGridMarginBefore, updateStandardAllowance } =
227+
stdcmConfSliceActions;
228+
210229
export type StdcmConfSlice = typeof stdcmConfSlice;
211230

212231
export type StdcmConfSliceActions = typeof stdcmConfSliceActions;

front/src/reducers/osrdconf/stdcmConf/selectors.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ const buildStdcmConfSelectors = () => {
1515
return {
1616
...commonConfSelectors,
1717
getStdcmConf,
18-
getStandardStdcmAllowance: makeOsrdConfSelector('standardStdcmAllowance'),
18+
19+
getMargins: makeOsrdConfSelector('margins'),
1920
getTotalMass: makeOsrdConfSelector('totalMass'),
2021
getTotalLength: makeOsrdConfSelector('totalLength'),
2122
getMaxSpeed: makeOsrdConfSelector('maxSpeed'),
2223
getTowedRollingStockID: makeOsrdConfSelector('towedRollingStockID'),
24+
2325
getStdcmPathSteps,
2426
getStdcmOrigin: (state: RootState) => {
2527
const pathSteps = getStdcmPathSteps(state);
@@ -43,7 +45,7 @@ const buildStdcmConfSelectors = () => {
4345

4446
const selectors = buildStdcmConfSelectors();
4547

46-
export const { getStdcmConf } = selectors;
48+
export const { getStdcmConf, getMargins } = selectors;
4749

4850
export type StdcmConfSelectors = typeof selectors;
4951

front/src/reducers/osrdconf/stdcmConf/stdcmConfReducers.spec.ts

+29-11
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,38 @@ describe('stdcmConfReducers', () => {
7171
expect(state).toEqual(stdcmConfInitialState);
7272
});
7373

74-
it('should handle updateStdcmStandardAllowance', () => {
75-
const initialTimeStandardAllowance = testDataBuilder.buildTimeStandardAllowance(10);
76-
const store = createStore({
77-
standardStdcmAllowance: initialTimeStandardAllowance,
78-
});
74+
describe('should handle margins update', () => {
75+
it('should handle updateStandardAllowance', () => {
76+
const initialTimeStandardAllowance = testDataBuilder.buildTimeStandardAllowance(10);
77+
const store = createStore({
78+
margins: { standardAllowance: initialTimeStandardAllowance },
79+
});
80+
81+
const stateBefore = store.getState()[stdcmConfSlice.name];
82+
expect(stateBefore.margins.standardAllowance).toBe(initialTimeStandardAllowance);
7983

80-
const stateBefore = store.getState()[stdcmConfSlice.name];
81-
expect(stateBefore.standardStdcmAllowance).toBe(initialTimeStandardAllowance);
84+
const newStandardAllowance = testDataBuilder.buildPercentageStandardAllowance(5);
85+
store.dispatch(stdcmConfSliceActions.updateStandardAllowance(newStandardAllowance));
86+
87+
const stateAfter = store.getState()[stdcmConfSlice.name];
88+
expect(stateAfter.margins.standardAllowance).toBe(newStandardAllowance);
89+
});
8290

83-
const newStandardAllowance = testDataBuilder.buildPercentageStandardAllowance(5);
84-
store.dispatch(stdcmConfSliceActions.updateStdcmStandardAllowance(newStandardAllowance));
91+
it('should handle updateGridMarginBefore', () => {
92+
const newGridMarginBefore = 5;
93+
const store = createStore(initialStateSTDCMConfig);
94+
store.dispatch(stdcmConfSliceActions.updateGridMarginBefore(newGridMarginBefore));
95+
const state = store.getState()[stdcmConfSlice.name];
96+
expect(state.margins.gridMarginBefore).toStrictEqual(newGridMarginBefore);
97+
});
8598

86-
const stateAfter = store.getState()[stdcmConfSlice.name];
87-
expect(stateAfter.standardStdcmAllowance).toBe(newStandardAllowance);
99+
it('should handle updateGridMarginAfter', () => {
100+
const newGridMarginAfter = 5;
101+
const store = createStore(initialStateSTDCMConfig);
102+
store.dispatch(stdcmConfSliceActions.updateGridMarginAfter(newGridMarginAfter));
103+
const state = store.getState()[stdcmConfSlice.name];
104+
expect(state.margins.gridMarginAfter).toStrictEqual(newGridMarginAfter);
105+
});
88106
});
89107

90108
it('should handle resetStdcmConfig', () => {

front/src/reducers/osrdconf/types.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export type OsrdConfState = InfraState & {
2727
rollingStockID?: number;
2828
speedLimitByTag?: string;
2929
powerRestriction: PowerRestriction[];
30-
gridMarginBefore?: number;
31-
gridMarginAfter?: number;
3230
pathSteps: (PathStep | null)[];
3331
};
3432

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

4038
export type OsrdStdcmConfState = OsrdConfState & {
4139
stdcmPathSteps: StdcmPathStep[];
42-
standardStdcmAllowance?: StandardAllowance;
40+
margins: {
41+
standardAllowance?: StandardAllowance;
42+
gridMarginBefore?: number;
43+
gridMarginAfter?: number;
44+
};
4345
totalMass?: number;
4446
totalLength?: number;
4547
maxSpeed?: number;

0 commit comments

Comments
 (0)