Skip to content

Commit

Permalink
front: move some actions from commonConf to OperationalStudiesConf
Browse files Browse the repository at this point in the history
Signed-off-by: Clara Ni <clara.ni@outlook.fr>
  • Loading branch information
clarani committed Dec 30, 2024
1 parent 177278f commit 21a93d2
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import TimesStopsInput from 'modules/timesStops/TimesStopsInput';
import { Map } from 'modules/trainschedule/components/ManageTrainSchedule';
import SimulationSettings from 'modules/trainschedule/components/ManageTrainSchedule/SimulationSettings';
import TrainSettings from 'modules/trainschedule/components/ManageTrainSchedule/TrainSettings';
import {
getConstraintDistribution,
getStartTime,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { formatKmValue } from 'utils/strings';

import { useManageTrainScheduleContext } from '../hooks/useManageTrainScheduleContext';
Expand All @@ -33,8 +37,7 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const { pathProperties, voltageRanges } = useManageTrainScheduleContext();

const { getOrigin, getDestination, getPathSteps, getConstraintDistribution, getStartTime } =
useOsrdConfSelectors();
const { getOrigin, getDestination, getPathSteps } = useOsrdConfSelectors();
const origin = useSelector(getOrigin);
const destination = useSelector(getDestination);
const pathSteps = useSelector(getPathSteps);
Expand Down
11 changes: 2 additions & 9 deletions front/src/applications/stdcm/utils/formatStdcmConf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import type { TFunction } from 'i18next';
import { compact } from 'lodash';
import type { Dispatch } from 'redux';

import type {
PathfindingItem,
PostTimetableByIdStdcmApiArg,
TrainScheduleBase,
} from 'common/api/osrdEditoastApi';
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';
Expand All @@ -23,7 +19,6 @@ type ValidStdcmConfig = {
towedRollingStockID?: number;
timetableId: number;
infraId: number;
rollingStockComfort: TrainScheduleBase['comfort'];
path: PathfindingItem[];
speedLimitByTag?: string;
totalMass?: number;
Expand All @@ -46,7 +41,6 @@ export const checkStdcmConf = (
stdcmPathSteps: pathSteps,
timetableID,
speedLimitByTag,
rollingStockComfort,
infraID,
rollingStockID,
towedRollingStockID,
Expand Down Expand Up @@ -191,7 +185,6 @@ export const checkStdcmConf = (
infraId: infraID!,
rollingStockId: rollingStockID!,
timetableId: timetableID!,
rollingStockComfort,
path,
speedLimitByTag,
totalMass,
Expand All @@ -216,7 +209,7 @@ export const formatStdcmPayload = (
infra: validConfig.infraId,
id: validConfig.timetableId,
body: {
comfort: validConfig.rollingStockComfort || 'STANDARD',
comfort: 'STANDARD',
margin: createMargin(validConfig.margin),
rolling_stock_id: validConfig.rollingStockId,
towed_rolling_stock_id: validConfig.towedRollingStockID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import type { Comfort } from 'common/api/osrdEditoastApi';
import { ModalContext } from 'common/BootstrapSNCF/ModalSNCF/ModalProvider';
import OptionsSNCF from 'common/BootstrapSNCF/OptionsSNCF';
import type { Option } from 'common/BootstrapSNCF/OptionsSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { useOsrdConfActions } from 'common/osrdContext';
import { comfort2pictogram } from 'modules/rollingStock/components/RollingStockSelector/RollingStockHelpers';
import { updateRollingStockComfort } from 'reducers/osrdconf/operationalStudiesConf';
import { getRollingStockComfort } from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { useAppDispatch } from 'store';

interface RollingStockCardButtonsProps {
Expand All @@ -26,11 +28,10 @@ const RollingStockCardButtons = ({
const { t } = useTranslation(['rollingstock']);
const { closeModal } = useContext(ModalContext);

const { getRollingStockComfort } = useOsrdConfSelectors();
const currentComfortInStore = useSelector(getRollingStockComfort);
const [comfort, setComfort] = useState<string>(currentComfortInStore);

const { updateRollingStockComfort, updateRollingStockID } = useOsrdConfActions();
const { updateRollingStockID } = useOsrdConfActions();

const selectRollingStock = () => {
setOpenedRollingStockCardId(undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { useSelector } from 'react-redux';

import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { getRollingStockComfort } from 'reducers/osrdconf/operationalStudiesConf/selectors';

export const useStoreDataForRollingStockSelector = () => {
const { getRollingStockID, getRollingStockComfort } = useOsrdConfSelectors();
const { getRollingStockID } = useOsrdConfSelectors();
const rollingStockId = useSelector(getRollingStockID);
const rollingStockComfort = useSelector(getRollingStockComfort);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { useTranslation } from 'react-i18next';

import type { Distribution } from 'common/api/osrdEditoastApi';
import OptionsSNCF from 'common/BootstrapSNCF/OptionsSNCF';
import { useOsrdConfActions } from 'common/osrdContext';
import { updateConstraintDistribution } from 'reducers/osrdconf/operationalStudiesConf';
import { useAppDispatch } from 'store';

type Props = {
constraintDistribution: Distribution;
};
const ConstraintDistributionSwitch = ({ constraintDistribution }: Props) => {
const { updateConstraintDistribution } = useOsrdConfActions();
const dispatch = useAppDispatch();
const [constraint, setConstraint] = useState(constraintDistribution);
const { t } = useTranslation(['operationalStudies/allowances', 'translation']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { useSelector } from 'react-redux';

import electricalProfilesIcon from 'assets/pictures/components/electricalProfiles.svg';
import SwitchSNCF, { SWITCH_TYPES } from 'common/BootstrapSNCF/SwitchSNCF/SwitchSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { toggleUsingElectricalProfiles } from 'reducers/osrdconf/operationalStudiesConf';
import { getUsingElectricalProfiles } from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { useAppDispatch } from 'store';

export default function ElectricalProfiles() {
const { getUsingElectricalProfiles } = useOsrdConfSelectors();
const { toggleUsingElectricalProfiles } = useOsrdConfActions();
const dispatch = useAppDispatch();
const usingElectricalProfiles = useSelector(getUsingElectricalProfiles);
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import { RxSpaceEvenlyHorizontally } from 'react-icons/rx';
import { useSelector } from 'react-redux';

import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import {
updateTrainStep,
updateTrainCount,
updateTrainDelta,
} from 'reducers/osrdconf/operationalStudiesConf';
import {
getTrainStep,
getTrainCount,
getTrainDelta,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { useAppDispatch } from 'store';
import { useDebounce } from 'utils/helpers';

export default function TrainAddingSettings() {
const { getTrainStep, getTrainCount, getTrainDelta } = useOsrdConfSelectors();
const { updateTrainStep, updateTrainCount, updateTrainDelta } = useOsrdConfActions();
const [trainStep, setTrainStep] = useState(useSelector(getTrainStep));
const [trainCount, setTrainCount] = useState(useSelector(getTrainCount));
const [trainDelta, setTrainDelta] = useState(useSelector(getTrainDelta));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import { useSelector } from 'react-redux';
import { isInvalidName } from 'applications/operationalStudies/utils';
import ChipsSNCF from 'common/BootstrapSNCF/ChipsSNCF';
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import {
updateLabels,
updateName,
updateStartTime,
updateInitialSpeed,
} from 'reducers/osrdconf/operationalStudiesConf';
import {
getLabels,
getName,
getInitialSpeed,
getStartTime,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { useAppDispatch } from 'store';
import { parseLocalDateTime, formatLocalDateTime } from 'utils/date';
import { useDebounce } from 'utils/helpers';
Expand All @@ -18,9 +29,6 @@ import { isInvalidFloatNumber } from 'utils/numbers';
export default function TrainSettings() {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);

const { getLabels, getInitialSpeed, getName, getStartTime } = useOsrdConfSelectors();
const { updateLabels, updateInitialSpeed, updateName, updateStartTime } = useOsrdConfActions();

const labels = useSelector(getLabels);
const nameFromStore = useSelector(getName);
const initialSpeedFromStore = useSelector(getInitialSpeed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { useSelector } from 'react-redux';

import { MANAGE_TRAIN_SCHEDULE_TYPES } from 'applications/operationalStudies/consts';
import { osrdEditoastApi, type TrainScheduleResult } from 'common/api/osrdEditoastApi';
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 {
getName,
getStartTime,
getOperationalStudiesConf,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { updateSelectedTrainId } from 'reducers/simulationResults';
import { useAppDispatch } from 'store';
import { formatToIsoDate } from 'utils/date';
Expand All @@ -26,7 +29,6 @@ const useUpdateTrainSchedule = (
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const [putTrainScheduleById] = osrdEditoastApi.endpoints.putTrainScheduleById.useMutation();
const dispatch = useAppDispatch();
const { getName, getStartTime } = useOsrdConfSelectors();
const confName = useSelector(getName);
const simulationConf = useSelector(getOperationalStudiesConf);
const startTime = useSelector(getStartTime);
Expand Down
5 changes: 3 additions & 2 deletions front/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { MapViewerState, MapViewerSlice } from 'reducers/mapViewer';
import mapViewerReducer, { mapViewerInitialState, mapViewerSlice } from 'reducers/mapViewer';
import operationalStudiesConfReducer, {
operationalStudiesConfSlice,
operationalStudiesInitialConf,
type OperationalStudiesConfState,
} from 'reducers/osrdconf/operationalStudiesConf';
import stdcmConfReducer, {
Expand All @@ -32,7 +33,7 @@ import type { SimulationResultsState } from 'reducers/simulationResults/types';
import userReducer, { userInitialState, userSlice } from 'reducers/user';
import type { UserState } from 'reducers/user';

import { type ConfSlice, defaultCommonConf } from './osrdconf/osrdConfCommon';
import type { ConfSlice } from './osrdconf/osrdConfCommon';

const compressor = createCompressor({
whitelist: ['rollingstock'],
Expand Down Expand Up @@ -120,7 +121,7 @@ export const rootInitialState: RootState = {
[editorSlice.name]: editorInitialState,
[mainSlice.name]: mainInitialState,
[stdcmConfSlice.name]: stdcmConfInitialState,
[operationalStudiesConfSlice.name]: defaultCommonConf,
[operationalStudiesConfSlice.name]: operationalStudiesInitialConf,
[simulationResultsSlice.name]: simulationResultsInitialState,
[osrdEditoastApi.reducerPath]: {} as ReturnType<typeof osrdEditoastApi.reducer>,
[osrdGatewayApi.reducerPath]: {} as ReturnType<typeof osrdGatewayApi.reducer>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSc
import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/Timetable/types';
import {
operationalStudiesConfSlice,
operationalStudiesInitialConf,
type OperationalStudiesConfState,
} from 'reducers/osrdconf/operationalStudiesConf';
import { defaultCommonConf } from 'reducers/osrdconf/osrdConfCommon';
import commonConfBuilder from 'reducers/osrdconf/osrdConfCommon/__tests__/commonConfBuilder';
import testCommonConfReducers from 'reducers/osrdconf/osrdConfCommon/__tests__/utils';
import type { PathStep } from 'reducers/osrdconf/types';
import { createStoreWithoutMiddleware } from 'store';

import commonConfBuilder from '../osrdConfCommon/__tests__/commonConfBuilder';
import type { PathStep } from '../types';
import testTrainSettingsReducer from './trainSettingsReducer';

const createStore = (extraInitialState?: Partial<OperationalStudiesConfState>) =>
createStoreWithoutMiddleware({
[operationalStudiesConfSlice.name]: {
...defaultCommonConf,
...operationalStudiesInitialConf,
...extraInitialState,
},
});
Expand All @@ -26,7 +27,7 @@ describe('simulationConfReducer', () => {
it('should return initial state', () => {
const store = createStore();
const state = store.getState()[operationalStudiesConfSlice.name];
expect(state).toEqual(defaultCommonConf);
expect(state).toEqual(operationalStudiesInitialConf);
});

it('selectTrainToEdit', () => {
Expand Down Expand Up @@ -57,7 +58,7 @@ describe('simulationConfReducer', () => {

const state = store.getState()[operationalStudiesConfSlice.name];
expect(state).toEqual({
...defaultCommonConf,
...operationalStudiesInitialConf,
usingElectricalProfiles: false,
labels: ['label1'],
rollingStockID: 1,
Expand Down Expand Up @@ -160,4 +161,5 @@ describe('simulationConfReducer', () => {
});

testCommonConfReducers(operationalStudiesConfSlice);
testTrainSettingsReducer();
});
Loading

0 comments on commit 21a93d2

Please sign in to comment.