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: move allWaypoints to ManageTrainScheduleContextType #10169

Merged
merged 2 commits into from
Jan 3, 2025
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { createContext, useContext, useMemo, type ReactNode, useState } from 'react';

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 type { PathStep } from 'reducers/osrdconf/types';

import type { ManageTrainSchedulePathProperties } from '../types';
Expand All @@ -16,6 +22,8 @@ type ManageTrainScheduleContextType = {
launchPathfinding: (pathSteps: (PathStep | null)[]) => void;
pathfindingState: PathfindingState;
infraInfo: { infra?: InfraWithState; reloadCount: number };
/** Operational points along the path (including origin and destination) and vias added by clicking on map */
pathStepsAndSuggestedOPs?: SuggestedOP[];
} | null;

const ManageTrainScheduleContext = createContext<ManageTrainScheduleContextType>(null);
Expand All @@ -25,6 +33,9 @@ type ManageTrainScheduleContextProviderProps = { children: ReactNode };
export const ManageTrainScheduleContextProvider = ({
children,
}: ManageTrainScheduleContextProviderProps) => {
const { getPathSteps } = useOsrdConfSelectors();
const pathSteps = useSelector(getPathSteps);

const [pathProperties, setPathProperties] = useState<ManageTrainSchedulePathProperties>();

const { launchPathfinding, pathfindingState, infraInfo } = usePathfinding(setPathProperties);
Expand All @@ -34,6 +45,11 @@ export const ManageTrainScheduleContextProvider = ({
[pathProperties]
);

const pathStepsAndSuggestedOPs = useMemo(() => {
if (!pathProperties) return undefined;
return upsertPathStepsInOPs(pathProperties.suggestedOperationalPoints, compact(pathSteps));
}, [pathProperties?.suggestedOperationalPoints, pathSteps]);

const providedContext = useMemo(
() => ({
pathProperties,
Expand All @@ -42,6 +58,7 @@ export const ManageTrainScheduleContextProvider = ({
launchPathfinding,
pathfindingState,
infraInfo,
pathStepsAndSuggestedOPs,
}),
[
pathProperties,
Expand All @@ -50,6 +67,7 @@ export const ManageTrainScheduleContextProvider = ({
launchPathfinding,
pathfindingState,
infraInfo,
pathStepsAndSuggestedOPs,
]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import {
} from 'common/api/osrdEditoastApi';
import { useOsrdConfActions } from 'common/osrdContext';
import buildOpSearchQuery from 'modules/operationalPoint/helpers/buildOpSearchQuery';
import {
formatSuggestedOperationalPoints,
matchPathStepAndOp,
upsertPathStepsInOPs,
} from 'modules/pathfinding/utils';
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';
Expand Down Expand Up @@ -213,14 +209,11 @@ const useSetupItineraryForTrainUpdate = (trainIdToEdit: number) => {
});
}

const allWaypoints = upsertPathStepsInOPs(suggestedOperationalPoints, updatedPathSteps);

return {
pathProperties: {
electrifications,
geometry,
suggestedOperationalPoints,
allWaypoints,
length: pathfindingResult.length,
trackSectionRanges: pathfindingResult.track_section_ranges,
},
Expand Down
2 changes: 0 additions & 2 deletions front/src/applications/operationalStudies/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ export type ManageTrainSchedulePathProperties = {
electrifications: NonNullable<PathProperties['electrifications']>;
geometry: NonNullable<PathProperties['geometry']>;
suggestedOperationalPoints: SuggestedOP[];
/** Operational points along the path (including origin and destination) and vias added by clicking on map */
allWaypoints: SuggestedOP[];
length: number;
trackSectionRanges: NonNullable<PathfindingResultSuccess['track_section_ranges']>;
incompatibleConstraints?: IncompatibleConstraints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type ManageTrainScheduleProps = {

const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const { pathProperties, voltageRanges } = useManageTrainScheduleContext();
const { pathProperties, voltageRanges, pathStepsAndSuggestedOPs } =
useManageTrainScheduleContext();

const { getOrigin, getDestination, getPathSteps, getConstraintDistribution, getStartTime } =
useOsrdConfSelectors();
Expand Down Expand Up @@ -117,7 +118,7 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
// If pathProperties is defined we know that pathSteps won't have any null values
content: (
<TimesStopsInput
allWaypoints={pathProperties?.allWaypoints}
pathStepsAndSuggestedOPs={pathStepsAndSuggestedOPs}
startTime={new Date(startTime)}
pathSteps={compact(pathSteps)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const Itinerary = () => {
const { t } = useTranslation('operationalStudies/manageTrainSchedule');
const { openModal } = useModal();

const { pathProperties, setPathProperties, launchPathfinding } = useManageTrainScheduleContext();
const { pathProperties, setPathProperties, launchPathfinding, pathStepsAndSuggestedOPs } =
useManageTrainScheduleContext();

const zoomToFeaturePoint = (lngLat?: Position) => {
if (lngLat) {
Expand Down Expand Up @@ -118,15 +119,15 @@ const Itinerary = () => {
>
<Route />
</button>
{pathProperties && pathProperties.suggestedOperationalPoints && (
{pathStepsAndSuggestedOPs && (
<button
data-testid="add-waypoints-button"
className="col ml-1 my-1 text-white btn bg-info btn-sm"
type="button"
onClick={() =>
openModal(
<ModalSuggestedVias
suggestedVias={pathProperties.allWaypoints}
suggestedVias={pathStepsAndSuggestedOPs}
launchPathfinding={launchPathfinding}
/>
)
Expand Down
9 changes: 1 addition & 8 deletions front/src/modules/pathfinding/hooks/usePathfinding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useState } from 'react';

import { compact, isObject } from 'lodash';
import { isObject } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';

Expand All @@ -18,7 +18,6 @@ import {
formatSuggestedOperationalPoints,
getPathfindingQuery,
matchPathStepAndOp,
upsertPathStepsInOPs,
} from 'modules/pathfinding/utils';
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
Expand Down Expand Up @@ -161,16 +160,10 @@ const usePathfinding = (
}
dispatch(updatePathSteps(updatedPathSteps));

const allWaypoints = upsertPathStepsInOPs(
suggestedOperationalPoints,
compact(updatedPathSteps)
);

setPathProperties({
electrifications,
geometry,
suggestedOperationalPoints,
allWaypoints,
length: pathResult.length,
trackSectionRanges: pathResult.track_section_ranges,
incompatibleConstraints,
Expand Down
26 changes: 15 additions & 11 deletions front/src/modules/timesStops/TimesStopsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ type ClearButtonProps = {
removeVia: () => void;
rowIndex: number;
rowData: TimesStopsInputRow;
allWaypoints?: SuggestedOP[];
pathStepsAndSuggestedOPs?: SuggestedOP[];
pathSteps: PathStep[];
};

const createClearViaButton = ({
removeVia,
rowIndex,
rowData,
allWaypoints,
pathStepsAndSuggestedOPs,
pathSteps,
}: ClearButtonProps) => {
const isClearBtnShown =
allWaypoints &&
pathStepsAndSuggestedOPs &&
rowIndex > 0 &&
rowIndex < allWaypoints.length - 1 &&
rowIndex < pathStepsAndSuggestedOPs.length - 1 &&
isVia(pathSteps || [], rowData, { withKP: true }) &&
(!isNil(rowData.stopFor) ||
rowData.theoreticalMargin !== undefined ||
Expand All @@ -59,12 +59,16 @@ const createClearViaButton = ({
};

type TimesStopsInputProps = {
allWaypoints?: SuggestedOP[];
pathStepsAndSuggestedOPs?: SuggestedOP[];
startTime: Date;
pathSteps: PathStep[];
};

const TimesStopsInput = ({ allWaypoints, startTime, pathSteps }: TimesStopsInputProps) => {
const TimesStopsInput = ({
pathStepsAndSuggestedOPs,
startTime,
pathSteps,
}: TimesStopsInputProps) => {
const dispatch = useAppDispatch();
const { t } = useTranslation('timesStops');
const { updatePathSteps, upsertSeveralViasFromSuggestedOP } =
Expand Down Expand Up @@ -128,10 +132,10 @@ const TimesStopsInput = ({ allWaypoints, startTime, pathSteps }: TimesStopsInput

useEffect(() => {
const fetchAndFormatRows = async () => {
if (allWaypoints) {
const trackIds = allWaypoints.map((op) => op.track);
if (pathStepsAndSuggestedOPs) {
const trackIds = pathStepsAndSuggestedOPs.map((op) => op.track);
const trackSections = await getTrackSectionsByIds(trackIds);
const suggestedOPsWithTrackNames = allWaypoints.map((op) => ({
const suggestedOPsWithTrackNames = pathStepsAndSuggestedOPs.map((op) => ({
...op,
trackName: trackSections[op.track]?.extensions?.sncf?.track_name,
}));
Expand All @@ -147,7 +151,7 @@ const TimesStopsInput = ({ allWaypoints, startTime, pathSteps }: TimesStopsInput
};

fetchAndFormatRows();
}, [allWaypoints, pathSteps, startTime]);
}, [pathStepsAndSuggestedOPs, pathSteps, startTime]);

return (
<TimesStops
Expand All @@ -159,7 +163,7 @@ const TimesStopsInput = ({ allWaypoints, startTime, pathSteps }: TimesStopsInput
removeVia: () => clearPathStep(rowData),
rowIndex,
rowData,
allWaypoints,
pathStepsAndSuggestedOPs,
pathSteps,
}),
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export default function commonConfBuilder() {
],
},
suggestedOperationalPoints: [],
allWaypoints: [],
length: 1169926000,
trackSectionRanges: [],
}),
Expand Down
Loading