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: clean selectors #10202

Merged
merged 2 commits into from
Dec 31, 2024
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
6 changes: 3 additions & 3 deletions front/src/applications/stdcm/hooks/useStdcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { useOsrdConfSelectors } from 'common/osrdContext';
import { useStoreDataForSpeedLimitByTagSelector } from 'common/SpeedLimitByTagSelector/useStoreDataForSpeedLimitByTagSelector';
import { setFailure } from 'reducers/main';
import type { OsrdStdcmConfState } from 'reducers/osrdconf/types';
import { getStdcmConf } from 'reducers/osrdconf/stdcmConf/selectors';
import { updateSelectedTrainId } from 'reducers/simulationResults';
import { useAppDispatch } from 'store';
import { castErrorToFailure } from 'utils/error';
Expand Down Expand Up @@ -49,8 +49,8 @@ const useStdcm = ({

const dispatch = useAppDispatch();
const { t } = useTranslation(['translation', 'stdcm']);
const { getConf, getTimetableID } = useOsrdConfSelectors();
const osrdconf = useSelector(getConf) as OsrdStdcmConfState;
const { getTimetableID } = useOsrdConfSelectors();
const osrdconf = useSelector(getStdcmConf);
const timetableId = useSelector(getTimetableID);
const requestPromise = useRef<ReturnType<typeof postTimetableByIdStdcm>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type {
TrainScheduleBase,
TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import trainNameWithNum from 'modules/trainschedule/components/ManageTrainSchedule/helpers/trainNameHelper';
import { setFailure, setSuccess } from 'reducers/main';
import { getOperationalStudiesConf } from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { useAppDispatch } from 'store';
import { isoDateToMs, isoDateWithTimezoneToSec } from 'utils/date';
import { castErrorToFailure } from 'utils/error';
Expand All @@ -38,8 +38,7 @@ const AddTrainScheduleButton = ({
const dispatch = useAppDispatch();
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);

const { getConf } = useOsrdConfSelectors();
const simulationConf = useSelector(getConf);
const simulationConf = useSelector(getOperationalStudiesConf);

// TODO TS2 : remove this when rollingStockName will replace rollingStockId in the store
const { rollingStock } = useStoreDataForRollingStockSelector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import type { Dispatch } from 'redux';
import getStepLocation from 'modules/pathfinding/helpers/getStepLocation';
import type { ValidConfig } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { setFailure } from 'reducers/main';
import type { OsrdConfState } from 'reducers/osrdconf/types';
import type { OperationalStudiesConfState } from 'reducers/osrdconf/operationalStudiesConf';
import { isInvalidFloatNumber } from 'utils/numbers';
import { kmhToMs } from 'utils/physics';

import formatMargin from './formatMargin';
import formatSchedule from './formatSchedule';

const checkCurrentConfig = (
osrdconf: OsrdConfState,
osrdconf: OperationalStudiesConfState,
t: (arg0: string) => string,
dispatch: Dispatch,
// TODO TS2 : remove this when rollingStockName will replace rollingStockId in the store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useOsrdConfSelectors } from 'common/osrdContext';
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import checkCurrentConfig from 'modules/trainschedule/components/ManageTrainSchedule/helpers/checkCurrentConfig';
import { setFailure, setSuccess } from 'reducers/main';
import { getOperationalStudiesConf } from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { updateSelectedTrainId } from 'reducers/simulationResults';
import { useAppDispatch } from 'store';
import { formatToIsoDate } from 'utils/date';
Expand All @@ -25,9 +26,9 @@ const useUpdateTrainSchedule = (
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const [putTrainScheduleById] = osrdEditoastApi.endpoints.putTrainScheduleById.useMutation();
const dispatch = useAppDispatch();
const { getConf, getName, getStartTime } = useOsrdConfSelectors();
const { getName, getStartTime } = useOsrdConfSelectors();
const confName = useSelector(getName);
const simulationConf = useSelector(getConf);
const simulationConf = useSelector(getOperationalStudiesConf);
const startTime = useSelector(getStartTime);
const { rollingStock } = useStoreDataForRollingStockSelector();

Expand Down
13 changes: 7 additions & 6 deletions front/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import type { MapViewerState, MapViewerSlice } from 'reducers/mapViewer';
import mapViewerReducer, { mapViewerInitialState, mapViewerSlice } from 'reducers/mapViewer';
import operationalStudiesConfReducer, {
operationalStudiesConfSlice,
type OperationalStudiesConfState,
} from 'reducers/osrdconf/operationalStudiesConf';
import stdcmConfReducer, {
stdcmConfInitialState,
stdcmConfSlice,
} from 'reducers/osrdconf/stdcmConf';
import type { OsrdConfState, OsrdStdcmConfState } from 'reducers/osrdconf/types';
import type { OsrdStdcmConfState } from 'reducers/osrdconf/types';
import simulationReducer, {
simulationResultsInitialState,
simulationResultsSlice,
Expand Down Expand Up @@ -79,7 +80,7 @@ const operationalStudiesDateTransform = createTransform(
);

// Useful to only blacklist a sub-propertie of osrdconf
const buildOsrdConfPersistConfig = <T extends OsrdConfState>(
const buildOsrdConfPersistConfig = <T extends OperationalStudiesConfState | OsrdStdcmConfState>(
slice: ConfSlice
): PersistConfig<T> => ({
key: slice.name,
Expand All @@ -106,7 +107,7 @@ export interface RootState {
[editorSlice.name]: EditorState;
[mainSlice.name]: MainState;
[stdcmConfSlice.name]: OsrdStdcmConfState;
[operationalStudiesConfSlice.name]: OsrdConfState;
[operationalStudiesConfSlice.name]: OperationalStudiesConfState;
[simulationResultsSlice.name]: SimulationResultsState;
[osrdEditoastApi.reducerPath]: ReturnType<typeof osrdEditoastApi.reducer>;
[osrdGatewayApi.reducerPath]: ReturnType<typeof osrdGatewayApi.reducer>;
Expand All @@ -132,7 +133,7 @@ export type AnyReducerState =
| EditorState
| MainState
| OsrdStdcmConfState
| OsrdConfState
| OperationalStudiesConfState
| SimulationResultsState;

export const rootReducer: ReducersMapObject<RootState> = {
Expand All @@ -146,9 +147,9 @@ export const rootReducer: ReducersMapObject<RootState> = {
stdcmConfReducer
) as unknown as Reducer<OsrdStdcmConfState, AnyAction>,
[operationalStudiesConfSlice.name]: persistReducer(
buildOsrdConfPersistConfig<OsrdConfState>(operationalStudiesConfSlice),
buildOsrdConfPersistConfig<OperationalStudiesConfState>(operationalStudiesConfSlice),
operationalStudiesConfReducer
) as unknown as Reducer<OsrdConfState, AnyAction>,
) as unknown as Reducer<OperationalStudiesConfState, AnyAction>,
[simulationResultsSlice.name]: simulationReducer,
[osrdEditoastApi.reducerPath]: osrdEditoastApi.reducer,
[osrdGatewayApi.reducerPath]: osrdGatewayApi.reducer,
Expand Down
7 changes: 4 additions & 3 deletions front/src/reducers/osrdconf/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { pathStepMatchesOp } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { addElementAtIndex } from 'utils/array';

import type { OsrdConfState, PathStep } from './types';
import type { OperationalStudiesConfState } from './operationalStudiesConf';
import type { PathStep } from './types';

export const insertViaFromMap = (
pathSteps: OsrdConfState['pathSteps'],
pathSteps: OperationalStudiesConfState['pathSteps'],
newVia: PathStep,
pathProperties: ManageTrainSchedulePathProperties
): OsrdConfState['pathSteps'] => {
): OperationalStudiesConfState['pathSteps'] => {
// If one of these is missing, via is not valid (it hasn't been added via click on map) and we return the same array
if (!('track' in newVia) || !newVia.coordinates) return pathSteps;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { compact, isEqual, keyBy, sortBy } from 'lodash';

import type { PowerRestriction } from 'applications/operationalStudies/types';
import { NO_POWER_RESTRICTION } from 'modules/powerRestriction/consts';
import type { OsrdConfState, PathStep } from 'reducers/osrdconf/types';
import type { PathStep } from 'reducers/osrdconf/types';
import { addElementAtIndex } from 'utils/array';

import type { OperationalStudiesConfState } from '.';
import { addPathStep, cleanPathSteps, isRangeCovered, updateRestrictions } from './utils';

export type PowerRestrictionReducer<S extends OsrdConfState> = {
export type PowerRestrictionReducer<S extends OperationalStudiesConfState> = {
['updatePowerRestrictionRanges']: CaseReducer<S, PayloadAction<PowerRestriction[]>>;
['upsertPowerRestrictionRanges']: CaseReducer<
S,
Expand All @@ -35,7 +36,9 @@ export type PowerRestrictionReducer<S extends OsrdConfState> = {
>;
};

export function builPowerRestrictionReducer<S extends OsrdConfState>(): PowerRestrictionReducer<S> {
export function builPowerRestrictionReducer<
S extends OperationalStudiesConfState,
>(): PowerRestrictionReducer<S> {
return {
updatePowerRestrictionRanges(state: Draft<S>, action: PayloadAction<PowerRestriction[]>) {
state.powerRestriction = action.payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { RootState } from 'reducers';
import { operationalStudiesConfSlice } from 'reducers/osrdconf/operationalStudiesConf';
import buildCommonConfSelectors from 'reducers/osrdconf/osrdConfCommon/selectors';

import type { OsrdConfState } from '../types';

const buildOperationalStudiesConfSelectors = () => {
const commonConfSelectors = buildCommonConfSelectors<OsrdConfState>(operationalStudiesConfSlice);
const commonConfSelectors = buildCommonConfSelectors(operationalStudiesConfSlice);
const getOperationalStudiesConf = (state: RootState) => state[operationalStudiesConfSlice.name];

return {
...commonConfSelectors,
getOperationalStudiesConf,
};
};

const selectors = buildOperationalStudiesConfSelectors();

export const { getOperationalStudiesConf } = selectors;

export type OperationalStudiesConfSelectors = typeof selectors;

export default selectors;
9 changes: 2 additions & 7 deletions front/src/reducers/osrdconf/osrdConfCommon/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import type { RootState } from 'reducers';
import buildInfraStateSelectors from 'reducers/infra/selectors';
import type { OperationalStudiesConfSlice } from 'reducers/osrdconf/operationalStudiesConf';
import type { StdcmConfSlice } from 'reducers/osrdconf/stdcmConf';
import type { OsrdConfState } from 'reducers/osrdconf/types';
import { makeSubSelector } from 'utils/selectors';

const buildCommonConfSelectors = <ConfState extends OsrdConfState>(
slice: OperationalStudiesConfSlice | StdcmConfSlice
) => {
const getConf = (state: RootState) => state[slice.name] as ConfState;
const makeOsrdConfSelector = makeSubSelector<ConfState>(getConf);
const buildCommonConfSelectors = (slice: OperationalStudiesConfSlice | StdcmConfSlice) => {
const makeOsrdConfSelector = makeSubSelector((state: RootState) => state[slice.name]);

const getPathSteps = makeOsrdConfSelector('pathSteps');

Expand All @@ -26,7 +22,6 @@ const buildCommonConfSelectors = <ConfState extends OsrdConfState>(

return {
...buildInfraStateSelectors(slice),
getConf,
getConstraintDistribution: makeOsrdConfSelector('constraintDistribution'),
getName: makeOsrdConfSelector('name'),
getTrainCount: makeOsrdConfSelector('trainCount'),
Expand Down
9 changes: 7 additions & 2 deletions front/src/reducers/osrdconf/stdcmConf/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import type { OsrdStdcmConfState } from 'reducers/osrdconf/types';
import { makeSubSelector } from 'utils/selectors';

const buildStdcmConfSelectors = () => {
const commonConfSelectors = buildCommonConfSelectors<OsrdStdcmConfState>(stdcmConfSlice);
const makeOsrdConfSelector = makeSubSelector<OsrdStdcmConfState>(commonConfSelectors.getConf);
const commonConfSelectors = buildCommonConfSelectors(stdcmConfSlice);

const getStdcmConf = (state: RootState) => state[stdcmConfSlice.name];
const makeOsrdConfSelector = makeSubSelector<OsrdStdcmConfState>(getStdcmConf);

const getStdcmPathSteps = makeOsrdConfSelector('stdcmPathSteps');

return {
...commonConfSelectors,
getStdcmConf,
getStandardStdcmAllowance: makeOsrdConfSelector('standardStdcmAllowance'),
getTotalMass: makeOsrdConfSelector('totalMass'),
getTotalLength: makeOsrdConfSelector('totalLength'),
Expand Down Expand Up @@ -40,6 +43,8 @@ const buildStdcmConfSelectors = () => {

const selectors = buildStdcmConfSelectors();

export const { getStdcmConf } = selectors;

export type StdcmConfSelectors = typeof selectors;

export default selectors;
8 changes: 4 additions & 4 deletions front/src/reducers/osrdconf/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
import type { IsoDurationString } from 'common/types';
import type { InfraState } from 'reducers/infra';

export interface OsrdConfState extends InfraState {
export type OsrdConfState = InfraState & {
constraintDistribution: Distribution;
name: string;
trainCount: number;
Expand All @@ -42,22 +42,22 @@ export interface OsrdConfState extends InfraState {
pathSteps: (PathStep | null)[];
rollingStockComfort: Comfort;
startTime: Date;
}
};

export interface StandardAllowance {
type: AllowanceValue['value_type'];
value?: number;
}

export interface OsrdStdcmConfState extends OsrdConfState {
export type OsrdStdcmConfState = OsrdConfState & {
stdcmPathSteps: StdcmPathStep[];
standardStdcmAllowance?: StandardAllowance;
totalMass?: number;
totalLength?: number;
maxSpeed?: number;
towedRollingStockID?: number;
linkedTrains: LinkedTrains;
}
};

export type PathStep = PathItemLocation & {
id: string;
Expand Down
Loading