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: create itinerary reducer #10205

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { compact } from 'lodash';
import { useSelector } from 'react-redux';

import type { InfraWithState } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import type { RangedValue } from 'common/types';
import getPathVoltages from 'modules/pathfinding/helpers/getPathVoltages';
import usePathfinding from 'modules/pathfinding/hooks/usePathfinding';
import type { PathfindingState } from 'modules/pathfinding/types';
import { upsertPathStepsInOPs } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { getPathSteps } from 'reducers/osrdconf/operationalStudiesConf/selectors';
import type { PathStep } from 'reducers/osrdconf/types';

import type { ManageTrainSchedulePathProperties } from '../types';
Expand All @@ -33,7 +33,6 @@ type ManageTrainScheduleContextProviderProps = { children: ReactNode };
export const ManageTrainScheduleContextProvider = ({
children,
}: ManageTrainScheduleContextProviderProps) => {
const { getPathSteps } = useOsrdConfSelectors();
const pathSteps = useSelector(getPathSteps);

const [pathProperties, setPathProperties] = useState<ManageTrainSchedulePathProperties>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import {
type PathfindingResult,
type SearchResultItemOperationalPoint,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
import buildOpSearchQuery from 'modules/operationalPoint/helpers/buildOpSearchQuery';
import { formatSuggestedOperationalPoints, matchPathStepAndOp } from 'modules/pathfinding/utils';
import { getSupportedElectrification, isThermal } from 'modules/rollingStock/helpers/electric';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import computeBasePathStep from 'modules/trainschedule/helpers/computeBasePathStep';
import { setFailure } from 'reducers/main';
import { updatePathSteps } from 'reducers/osrdconf/operationalStudiesConf';
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { castErrorToFailure } from 'utils/error';
Expand Down Expand Up @@ -62,7 +62,6 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
const { setPathProperties } = useManageTrainScheduleContext();

const { infraId, getTrackSectionsByIds } = useScenarioContext();
const { updatePathSteps } = useOsrdConfActions();

const [getTrainScheduleById] = osrdEditoastApi.endpoints.getTrainScheduleById.useLazyQuery({});
const [getRollingStockByName] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import allowancesPic from 'assets/pictures/components/allowances.svg';
import pahtFindingPic from 'assets/pictures/components/pathfinding.svg';
import simulationSettings from 'assets/pictures/components/simulationSettings.svg';
import rollingStockPic from 'assets/pictures/components/train.svg';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { useStoreDataForSpeedLimitByTagSelector } from 'common/SpeedLimitByTagSelector/useStoreDataForSpeedLimitByTagSelector';
import Tabs from 'common/Tabs';
import IncompatibleConstraints from 'modules/pathfinding/components/IncompatibleConstraints';
Expand All @@ -23,6 +22,9 @@ import SimulationSettings from 'modules/trainschedule/components/ManageTrainSche
import TrainSettings from 'modules/trainschedule/components/ManageTrainSchedule/TrainSettings';
import {
getConstraintDistribution,
getDestination,
getOrigin,
getPathSteps,
getStartTime,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { formatKmValue } from 'utils/strings';
Expand All @@ -38,7 +40,6 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
const { pathProperties, voltageRanges, pathStepsAndSuggestedOPs } =
useManageTrainScheduleContext();

const { getOrigin, getDestination, getPathSteps } = useOsrdConfSelectors();
const origin = useSelector(getOrigin);
const destination = useSelector(getDestination);
const pathSteps = useSelector(getPathSteps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import { useNavigate } from 'react-router-dom';
import type { Infra } from 'common/api/osrdEditoastApi';
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
import { ModalContext } from 'common/BootstrapSNCF/ModalSNCF/ModalProvider';
import {
useInfraActions,
useInfraID,
useOsrdConfActions,
useOsrdContext,
} from 'common/osrdContext';
import { useInfraActions, useInfraID, useOsrdContext } from 'common/osrdContext';
import { MODES } from 'main/consts';
import { deleteItinerary } from 'reducers/osrdconf/operationalStudiesConf';
import { useAppDispatch } from 'store';

type InfraSelectorModalBodyStandardProps = {
Expand Down Expand Up @@ -51,7 +47,6 @@ export default function InfraSelectorModalBodyStandard({
const { updateInfraID } = useInfraActions();
const infraID = useInfraID();
const { closeModal } = useContext(ModalContext);
const { deleteItinerary } = useOsrdConfActions();
const navigate = useNavigate();

const setInfraID = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { IoFlag } from 'react-icons/io5';
import { useSelector } from 'react-redux';

import { useManageTrainScheduleContext } from 'applications/operationalStudies/hooks/useManageTrainScheduleContext';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { isPathStepInvalid } from 'modules/pathfinding/utils';
import { getDestination, getPathSteps } from 'reducers/osrdconf/operationalStudiesConf/selectors';

type DestinationProps = {
zoomToFeaturePoint: (lngLat?: Position, id?: string) => void;
Expand All @@ -16,7 +16,6 @@ type DestinationProps = {
const Destination = ({ zoomToFeaturePoint }: DestinationProps) => {
const { launchPathfinding } = useManageTrainScheduleContext();

const { getDestination, getPathSteps } = useOsrdConfSelectors();
const destination = useSelector(getDestination);
const pathSteps = useSelector(getPathSteps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { RiMapPin2Fill } from 'react-icons/ri';
import { useSelector } from 'react-redux';

import { useManageTrainScheduleContext } from 'applications/operationalStudies/hooks/useManageTrainScheduleContext';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { isPathStepInvalid } from 'modules/pathfinding/utils';
import { getOrigin, getPathSteps } from 'reducers/osrdconf/operationalStudiesConf/selectors';

type OriginProps = {
zoomToFeaturePoint: (lngLat?: Position, id?: string) => void;
Expand All @@ -17,7 +17,6 @@ const Origin = ({ zoomToFeaturePoint }: OriginProps) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const { launchPathfinding } = useManageTrainScheduleContext();

const { getOrigin, getPathSteps } = useOsrdConfSelectors();
const origin = useSelector(getOrigin);
const pathSteps = useSelector(getPathSteps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

import { useManageTrainScheduleContext } from 'applications/operationalStudies/hooks/useManageTrainScheduleContext';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { isPathStepInvalid } from 'modules/pathfinding/utils';
import { getPathSteps, getVias } from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { moveElement, removeElementAtIndex } from 'utils/array';
import { formatUicToCi } from 'utils/strings';

Expand All @@ -17,7 +17,6 @@ type ViasProps = {

const Vias = ({ zoomToFeaturePoint }: ViasProps) => {
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const { getVias, getPathSteps } = useOsrdConfSelectors();
const vias = useSelector(getVias());
const pathSteps = useSelector(getPathSteps);
const { launchPathfinding } = useManageTrainScheduleContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ import { useSelector } from 'react-redux';
import { useManageTrainScheduleContext } from 'applications/operationalStudies/hooks/useManageTrainScheduleContext';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { useOsrdConfSelectors } from 'common/osrdContext';
import Tipped from 'common/Tipped';
import Pathfinding from 'modules/pathfinding/components/Pathfinding/Pathfinding';
import TypeAndPath from 'modules/pathfinding/components/Pathfinding/TypeAndPath';
import { setWarning } from 'reducers/main';
import { updateViewport } from 'reducers/map';
import { getMap } from 'reducers/map/selectors';
import {
getOrigin,
getDestination,
getPathSteps,
getPowerRestrictions,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { useAppDispatch } from 'store';
import { isEmptyArray } from 'utils/array';

Expand All @@ -26,11 +31,10 @@ import Vias from './DisplayItinerary/Vias';
import ModalSuggestedVias from './ModalSuggestedVias';

const Itinerary = () => {
const { getPathSteps, getOrigin, getDestination, getPowerRestriction } = useOsrdConfSelectors();
const origin = useSelector(getOrigin);
const destination = useSelector(getDestination);
const pathSteps = useSelector(getPathSteps);
const powerRestrictions = useSelector(getPowerRestriction);
const powerRestrictions = useSelector(getPowerRestrictions);

const [displayTypeAndPath, setDisplayTypeAndPath] = useState(false);
const dispatch = useAppDispatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import { useSelector } from 'react-redux';
import ModalBodySNCF from 'common/BootstrapSNCF/ModalSNCF/ModalBodySNCF';
import ModalFooterSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalFooterSNCF';
import ModalHeaderSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalHeaderSNCF';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { useOsrdConfActions } from 'common/osrdContext';
import { isVia, matchPathStepAndOp } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { OperationalStudiesConfSliceActions } from 'reducers/osrdconf/operationalStudiesConf';
import {
getVias,
getDestination,
getPathSteps,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { formatUicToCi } from 'utils/strings';
Expand All @@ -23,7 +28,6 @@ type ModalSuggestedViasProps = {

const ModalSuggestedVias = ({ suggestedVias, launchPathfinding }: ModalSuggestedViasProps) => {
const { upsertViaFromSuggestedOP } = useOsrdConfActions() as OperationalStudiesConfSliceActions;
const { getVias, getDestination, getPathSteps } = useOsrdConfSelectors();
const dispatch = useAppDispatch();
const vias = useSelector(getVias());
const destination = useSelector(getDestination);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import infraLogo from 'assets/pictures/components/tracks.svg';
import { Spinner } from 'common/Loaders';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { isPathStepInvalid } from 'modules/pathfinding/utils';
import {
getPathSteps,
getOrigin,
getDestination,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { conditionalStringConcat, formatKmValue } from 'utils/strings';

import { InfraHardError, InfraSoftError } from './InfraError';

const Pathfinding = () => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);

const { getOrigin, getDestination, getPathSteps } = useOsrdConfSelectors();
const pathSteps = useSelector(getPathSteps);
const hasInvalidPathStep = pathSteps.some((pathStep) => isPathStepInvalid(pathStep));
const origin = useSelector(getOrigin, isEqual);
Expand Down
12 changes: 6 additions & 6 deletions front/src/modules/pathfinding/hooks/usePathfinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
PostInfraByInfraIdPathPropertiesApiArg,
} from 'common/api/osrdEditoastApi';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import {
formatSuggestedOperationalPoints,
getPathfindingQuery,
Expand All @@ -22,6 +21,11 @@ import {
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { setFailure, setWarning } from 'reducers/main';
import { replaceItinerary, updatePathSteps } from 'reducers/osrdconf/operationalStudiesConf';
import {
getPathSteps,
getPowerRestrictions,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';
import { isEmptyArray } from 'utils/array';
Expand All @@ -41,9 +45,8 @@ const usePathfinding = (
) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const dispatch = useAppDispatch();
const { getPathSteps, getPowerRestriction } = useOsrdConfSelectors();
const pathSteps = useSelector(getPathSteps);
const powerRestrictions = useSelector(getPowerRestriction);
const powerRestrictions = useSelector(getPowerRestrictions);
const { infra, reloadCount, setIsInfraError } = useInfraStatus();
const { rollingStock } = useStoreDataForRollingStockSelector();

Expand All @@ -59,7 +62,6 @@ const usePathfinding = (
const [postPathProperties] =
osrdEditoastApi.endpoints.postInfraByInfraIdPathProperties.useLazyQuery();

const { updatePathSteps, replaceItinerary } = useOsrdConfActions();
const { infraId } = useScenarioContext();

const setIsMissingParam = () =>
Expand Down Expand Up @@ -132,13 +134,11 @@ const usePathfinding = (
const theoreticalMargin = i === 0 ? step.theoreticalMargin || '0%' : step.theoreticalMargin;

const stopFor = i === pathStepsInput.length - 1 && !step.stopFor ? '0' : step.stopFor;
const stopType = i === pathStepsInput.length - 1 && !step.stopFor ? undefined : step.stopType;

return {
...step,
positionOnPath: pathResult.path_item_positions[i],
stopFor,
stopType,
theoreticalMargin,
...(correspondingOp && {
name: correspondingOp.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { useSelector } from 'react-redux';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import type { RollingStock } from 'common/api/osrdEditoastApi';
import type { IntervalItem } from 'common/IntervalsEditor/types';
import { useOsrdConfSelectors } from 'common/osrdContext';
import type { RangedValue } from 'common/types';
import {
getPowerRestrictions,
getPathSteps,
} from 'reducers/osrdconf/operationalStudiesConf/selectors';
import { mmToM } from 'utils/physics';

import usePowerRestrictionSelectorBehaviours from './usePowerRestrictionSelectorBehaviours';
Expand All @@ -21,8 +24,7 @@ const usePowerRestrictionSelector = (
rollingStockModes: RollingStock['effort_curves']['modes'],
pathProperties: ManageTrainSchedulePathProperties
) => {
const { getPowerRestriction, getPathSteps } = useOsrdConfSelectors();
const powerRestrictionRanges = useSelector(getPowerRestriction);
const powerRestrictionRanges = useSelector(getPowerRestrictions);
const pathSteps = compact(useSelector(getPathSteps));

const [cutPositions, setCutPositions] = useState<number[]>([]); // in meters
Expand Down
7 changes: 5 additions & 2 deletions front/src/modules/timesStops/TimesStopsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { useScenarioContext } from 'applications/operationalStudies/hooks/useSce
import { useOsrdConfActions } from 'common/osrdContext';
import { isVia, matchPathStepAndOp } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { OperationalStudiesConfSliceActions } from 'reducers/osrdconf/operationalStudiesConf';
import {
updatePathSteps,
type OperationalStudiesConfSliceActions,
} from 'reducers/osrdconf/operationalStudiesConf';
import type { PathStep } from 'reducers/osrdconf/types';
import { useAppDispatch } from 'store';

Expand Down Expand Up @@ -71,7 +74,7 @@ const TimesStopsInput = ({
}: TimesStopsInputProps) => {
const dispatch = useAppDispatch();
const { t } = useTranslation('timesStops');
const { updatePathSteps, upsertSeveralViasFromSuggestedOP } =
const { upsertSeveralViasFromSuggestedOP } =
useOsrdConfActions() as OperationalStudiesConfSliceActions;

const [rows, setRows] = useState<TimesStopsInputRow[]>([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { ManageTrainSchedulePathProperties } from 'applications/operational
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { setPointIti } from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/setPointIti';
import { getOrigin, getDestination } from 'reducers/osrdconf/operationalStudiesConf/selectors';
import type { PathStep } from 'reducers/osrdconf/types';

import type { FeatureInfoClick } from '../types';
Expand All @@ -32,7 +33,7 @@ function AddPathStepPopup({
featureInfoClick,
resetFeatureInfoClick,
}: AddPathStepPopupProps) {
const { getInfraID, getOrigin, getDestination } = useOsrdConfSelectors();
const { getInfraID } = useOsrdConfSelectors();
const { launchPathfinding } = useManageTrainScheduleContext();
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const infraId = useSelector(getInfraID);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { beforeEach, it, expect } from 'vitest';

import {
operationalStudiesConfSlice,
operationalStudiesInitialConf,
type OperationalStudiesConfState,
} from 'reducers/osrdconf/operationalStudiesConf';
import commonConfBuilder from 'reducers/osrdconf/osrdConfCommon/__tests__/commonConfBuilder';
import { createStoreWithoutMiddleware } from 'store';

const createStore = (extraInitialState?: Partial<OperationalStudiesConfState>) =>
createStoreWithoutMiddleware({
[operationalStudiesConfSlice.name]: {
...operationalStudiesInitialConf,
...extraInitialState,
},
});

const testItineraryReducer = () => {
let defaultStore: ReturnType<typeof createStore>;
const testDataBuilder = commonConfBuilder();
const slice = operationalStudiesConfSlice;
const { updatePathSteps, deleteItinerary } = slice.actions;

const getState = () => defaultStore.getState()[operationalStudiesConfSlice.name];

beforeEach(() => {
defaultStore = createStore();
});

it('should handle updatePathSteps', () => {
const pathSteps = testDataBuilder.buildPathSteps();
defaultStore.dispatch(updatePathSteps(pathSteps));
const state = getState();
expect(state.pathSteps).toEqual(pathSteps);
});

it('should handle deleteItinerary', () => {
const pathSteps = testDataBuilder.buildPathSteps();
const store = createStore({ pathSteps });
store.dispatch(deleteItinerary());
const state = getState();
expect(state.pathSteps).toEqual([null, null]);
});
};

export default testItineraryReducer;
Loading
Loading