Skip to content

Commit

Permalink
front: replace selector getConf by getStdcmConf and getOperationalStu…
Browse files Browse the repository at this point in the history
…diesConf

Signed-off-by: Clara Ni <[email protected]>
  • Loading branch information
clarani committed Dec 31, 2024
1 parent 980149c commit 9c5ee0d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 26 deletions.
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 @@ -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
15 changes: 8 additions & 7 deletions front/src/reducers/osrdconf/operationalStudiesConf/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {
operationalStudiesConfSlice,
type OperationalStudiesConfState,
} from 'reducers/osrdconf/operationalStudiesConf';
import type { RootState } from 'reducers';
import { operationalStudiesConfSlice } from 'reducers/osrdconf/operationalStudiesConf';
import buildCommonConfSelectors from 'reducers/osrdconf/osrdConfCommon/selectors';

const buildOperationalStudiesConfSelectors = () => {
const commonConfSelectors = buildCommonConfSelectors<OperationalStudiesConfState>(
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;
4 changes: 2 additions & 2 deletions front/src/reducers/osrdconf/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ export interface StandardAllowance {
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

0 comments on commit 9c5ee0d

Please sign in to comment.