Skip to content

Commit

Permalink
front: move gridMargins from commonConf to stdcmConf
Browse files Browse the repository at this point in the history
Also rename stdcmStandardAllowance and group all the margins values together in the store

Signed-off-by: Clara Ni <[email protected]>
  • Loading branch information
clarani committed Jan 2, 2025
1 parent 9c5ee0d commit e613c85
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ 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 {
updateGridMarginAfter,
updateGridMarginBefore,
updateStandardAllowance,
type StdcmConfSliceActions,
} from 'reducers/osrdconf/stdcmConf';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import { useAppDispatch } from 'store';

Expand Down Expand Up @@ -52,13 +57,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 @@ -122,7 +121,7 @@ const StdcmConfig = ({
if (!isDebugMode) {
dispatch(updateGridMarginAfter(15));
dispatch(updateGridMarginBefore(15));
dispatch(updateStdcmStandardAllowance({ type: 'time_per_distance', value: 4.5 }));
dispatch(updateStandardAllowance({ type: 'time_per_distance', value: 4.5 }));
}
}, [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 @@ -8,7 +8,6 @@ import type {
TrainScheduleBase,
} 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 @@ -40,7 +39,7 @@ type ValidStdcmConfig = {
export const checkStdcmConf = (
dispatch: Dispatch,
t: TFunction,
osrdconf: OsrdStdcmConfState & InfraState
osrdconf: OsrdStdcmConfState
): ValidStdcmConfig | null => {
const {
stdcmPathSteps: pathSteps,
Expand All @@ -50,9 +49,7 @@ export const checkStdcmConf = (
infraID,
rollingStockID,
towedRollingStockID,
standardStdcmAllowance,
gridMarginBefore,
gridMarginAfter,
margins: { standardAllowance, gridMarginBefore, gridMarginAfter },
searchDatetimeWindow,
workScheduleGroupId,
temporarySpeedLimitGroupId,
Expand Down Expand Up @@ -198,7 +195,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 @@ -186,20 +186,6 @@ const testCommonConfReducers = (slice: OperationalStudiesConfSlice | StdcmConfSl
expect(state.initialSpeed).toBe(newInitialSpeed);
});

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 @@ -28,8 +28,6 @@ export const defaultCommonConf: OsrdConfState = {
powerRestriction: [],
speedLimitByTag: undefined,
initialSpeed: 0,
gridMarginBefore: undefined,
gridMarginAfter: undefined,
...infraState,
// Corresponds to origin and destination not defined
pathSteps: [null, null],
Expand All @@ -53,8 +51,6 @@ interface CommonConfReducers<S extends OsrdConfState> extends InfraStateReducers
['updateRollingStockID']: CaseReducer<S, PayloadAction<S['rollingStockID']>>;
['updateSpeedLimitByTag']: CaseReducer<S, PayloadAction<S['speedLimitByTag'] | null>>;
['updateInitialSpeed']: CaseReducer<S, PayloadAction<S['initialSpeed']>>;
['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 @@ -116,12 +112,6 @@ export function buildCommonConfReducers<S extends OsrdConfState>(): CommonConfRe
updateInitialSpeed(state: Draft<S>, action: PayloadAction<S['initialSpeed']>) {
state.initialSpeed = 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 @@ -39,8 +39,6 @@ const buildCommonConfSelectors = (slice: OperationalStudiesConfSlice | StdcmConf
getRollingStockID: makeOsrdConfSelector('rollingStockID'),
getSpeedLimitByTag: makeOsrdConfSelector('speedLimitByTag'),
getInitialSpeed: makeOsrdConfSelector('initialSpeed'),
getGridMarginBefore: makeOsrdConfSelector('gridMarginBefore'),
getGridMarginAfter: makeOsrdConfSelector('gridMarginAfter'),
getPowerRestriction: makeOsrdConfSelector('powerRestriction'),
getPathSteps,
getOrigin: (state: RootState) => {
Expand Down
27 changes: 23 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: undefined,
gridMarginBefore: undefined,
gridMarginAfter: undefined,
},
totalMass: undefined,
totalLength: undefined,
maxSpeed: undefined,
Expand Down Expand Up @@ -103,11 +107,23 @@ export const stdcmConfSlice = createSlice({
state.stdcmPathSteps = action.payload.stdcmPathSteps;
state.speedLimitByTag = action.payload.speedLimitByTag;
},
updateStdcmStandardAllowance(
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 +223,9 @@ export const stdcmConfSlice = createSlice({

export const stdcmConfSliceActions = stdcmConfSlice.actions;

export const { 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,
});
it('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 @@ -37,8 +37,6 @@ export type OsrdConfState = InfraState & {
speedLimitByTag?: string;
powerRestriction: PowerRestriction[];
initialSpeed?: number;
gridMarginBefore?: number;
gridMarginAfter?: number;
pathSteps: (PathStep | null)[];
rollingStockComfort: Comfort;
startTime: Date;
Expand All @@ -51,7 +49,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

0 comments on commit e613c85

Please sign in to comment.