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: prevent pathfinding from reloading on each re-render #5277

Merged
merged 1 commit into from
Oct 18, 2023
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
1 change: 0 additions & 1 deletion front/src/applications/operationalStudies/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export interface OsrdConfState {
infraID?: number;
switchTypes?: SwitchType[];
pathfindingID?: number;
shouldRunPathfinding: boolean;
timetableID?: number;
rollingStockID?: number;
speedLimitByTag?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';

Expand All @@ -13,10 +13,8 @@ import Allowances from 'modules/trainschedule/components/ManageTrainSchedule/All
import {
getPathfindingID,
getRollingStockID,
getShouldRunPathfinding,
getTrainScheduleIDsToModify,
} from 'reducers/osrdconf/selectors';
import { updateShouldRunPathfinding } from 'reducers/osrdconf';
import {
RollingStock2Img,
RollingStockSelector,
Expand All @@ -32,8 +30,6 @@ import { isElectric } from 'modules/rollingStock/helpers/utils';

export default function ManageTrainSchedule() {
const dispatch = useDispatch();
const shouldRunPathfinding = useSelector(getShouldRunPathfinding);
const [mustUpdatePathfinding, setMustUpdatePathfinding] = useState<boolean>(false);
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const rollingStockID = useSelector(getRollingStockID);
const pathFindingID = useSelector(getPathfindingID);
Expand Down Expand Up @@ -110,7 +106,7 @@ export default function ManageTrainSchedule() {
content: (
<div className="osrd-config-item-container-map" data-testid="map">
<span className="floating-itinerary">
<Itinerary mustUpdate={mustUpdatePathfinding} />
<Itinerary />
</span>
<Map />
</div>
Expand Down Expand Up @@ -170,17 +166,6 @@ export default function ManageTrainSchedule() {
});
}, [trainScheduleIDsToModify]);

useEffect(() => {
setMustUpdatePathfinding(false);
dispatch(updateShouldRunPathfinding(false));
}, []);

useEffect(() => {
if (shouldRunPathfinding && mustUpdatePathfinding === false) {
setMustUpdatePathfinding(true);
}
}, [shouldRunPathfinding]);

return (
<>
<div className="osrd-config-item-container mb-3">
Expand Down
29 changes: 20 additions & 9 deletions front/src/common/Pathfinding/Pathfinding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ interface Action {
destination?: Partial<PointOnMap>;
rollingStockID?: number;
vias?: Partial<PointOnMap>[];
pathfindingId?: number;
};
}

Expand Down Expand Up @@ -122,7 +123,11 @@ export function reducer(state: PathfindingState, action: Action): PathfindingSta
case 'PATHFINDING_PARAM_CHANGED':
case 'VIAS_CHANGED':
case 'INFRA_CHANGED': {
if (!action.params || state.running) {
if (
!action.params ||
state.running ||
(action.type === 'INFRA_CHANGED' && action.params.pathfindingId)
) {
return state;
}
const { origin, destination, rollingStockID } = action.params;
Expand Down Expand Up @@ -170,7 +175,6 @@ function init({
}

interface PathfindingProps {
mustUpdate?: boolean;
zoomToFeature: (lngLat: Position, id?: undefined, source?: undefined) => void;
}

Expand Down Expand Up @@ -257,7 +261,7 @@ export function getPathfindingQuery({
return null;
}

function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
function Pathfinding({ zoomToFeature }: PathfindingProps) {
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const [pathfindingRequest, setPathfindingRequest] =
useState<ReturnType<typeof postPathfinding>>();
Expand All @@ -281,8 +285,12 @@ function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
const [isInfraLoaded, setIsInfraLoaded] = useState(false);
const [reloadCount, setReloadCount] = useState(1);
const [isInfraError, setIsInfraError] = useState(false);
const [postPathfinding] = osrdEditoastApi.usePostPathfindingMutation();
const { data: infra } = osrdEditoastApi.useGetInfraByIdQuery(

const [isPathfindingInitialized, setIsPathfindingInitialized] = useState(false);

const [postPathfinding] = osrdEditoastApi.endpoints.postPathfinding.useMutation();

const { data: infra } = osrdEditoastApi.endpoints.getInfraById.useQuery(
{ id: infraID as number },
{
refetchOnMountOrArgChange: true,
Expand Down Expand Up @@ -323,6 +331,7 @@ function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
origin,
destination,
rollingStockID,
pathfindingId: pathfindingID,
},
});
break;
Expand Down Expand Up @@ -426,7 +435,7 @@ function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
}, []);

useEffect(() => {
if (mustUpdate) {
if (isPathfindingInitialized) {
pathfindingDispatch({
type: 'VIAS_CHANGED',
params: {
Expand All @@ -437,7 +446,7 @@ function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
},
});
}
}, [mustUpdate, vias]);
}, [vias]);

useEffect(() => {
if (isInfraError) {
Expand All @@ -453,7 +462,7 @@ function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
}, [pathfindingState.mustBeLaunched, infra]);

useEffect(() => {
if (mustUpdate) {
if (isPathfindingInitialized) {
pathfindingDispatch({
type: 'PATHFINDING_PARAM_CHANGED',
params: {
Expand All @@ -463,7 +472,7 @@ function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
},
});
}
}, [mustUpdate, origin, destination, rollingStockID]);
}, [origin, destination, rollingStockID]);

const pathDetailsToggleButton = (
<button
Expand Down Expand Up @@ -507,6 +516,8 @@ function Pathfinding({ mustUpdate = true, zoomToFeature }: PathfindingProps) {
(state) => state === false || state === ''
);

useEffect(() => setIsPathfindingInitialized(true), []);

return (
<div className="pathfinding-state-main-container">
{infra && infra.state !== 'CACHED' && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
updatePowerRestrictionRanges,
updateRollingStockComfort,
updateRollingStockID,
updateShouldRunPathfinding,
} from 'reducers/osrdconf';
import { getRollingStockComfort } from 'reducers/osrdconf/selectors';
import { comfort2pictogram } from './RollingStockHelpers';
Expand All @@ -35,7 +34,6 @@ const RollingStockCardButtons = ({
const selectRollingStock = () => {
setOpenedRollingStockCardId(undefined);
dispatch(updateRollingStockComfort(comfort));
dispatch(updateShouldRunPathfinding(true));
dispatch(updateRollingStockID(id));
dispatch(updatePowerRestrictionRanges([]));
dispatch(updatePathfindingID(undefined));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import { getOrigin, getDestination, getVias } from 'reducers/osrdconf/selectors'
import { getMap } from 'reducers/map/selectors';
import Pathfinding from 'common/Pathfinding/Pathfinding';

type Props = {
mustUpdate?: boolean;
};

function Itinerary({ mustUpdate }: Props) {
function Itinerary() {
const origin = useSelector(getOrigin);
const destination = useSelector(getDestination);
const vias = useSelector(getVias);
Expand Down Expand Up @@ -95,7 +91,7 @@ function Itinerary({ mustUpdate }: Props) {
return (
<div className="osrd-config-item">
<div className="mb-2">
<Pathfinding mustUpdate={mustUpdate} zoomToFeature={zoomToFeature} />
<Pathfinding zoomToFeature={zoomToFeature} />
</div>
<div className="osrd-config-item-container pathfinding-details" data-testid="itinerary">
<DisplayItinerary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { useState, useEffect } from 'react';
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
import { useSelector, useDispatch } from 'react-redux';
import {
deleteVias,
permuteVias,
updateShouldRunPathfinding,
updateViaStopTime,
} from 'reducers/osrdconf';
import { deleteVias, permuteVias, updateViaStopTime } from 'reducers/osrdconf';
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
import { useDebounce } from 'utils/helpers';
import { getConf, getVias } from 'reducers/osrdconf/selectors';
Expand Down Expand Up @@ -63,7 +58,6 @@ export default function DisplayVias({ zoomToFeaturePoint }: DisplayViasProps) {
const [indexSelected, setIndexSelected] = useState<number | undefined>(undefined);

const dispatchAndRun = (action: ThunkAction<void, RootState, null, AnyAction>) => {
dispatch(updateShouldRunPathfinding(true));
dispatch(action);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSelector, useDispatch } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { FaLongArrowAltUp, FaLongArrowAltDown, FaTrash, FaMinus } from 'react-icons/fa';

import { replaceVias, updateShouldRunPathfinding } from 'reducers/osrdconf';
import { replaceVias } from 'reducers/osrdconf';
import { getSuggeredVias, getVias } from 'reducers/osrdconf/selectors';

import ModalHeaderSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalHeaderSNCF';
Expand Down Expand Up @@ -39,7 +39,6 @@ export default function ModalSugerredVias({
const { closeModal } = useContext(ModalContext);

const removeViaFromPath = (step: ArrayElement<Path['steps']>) => {
dispatch(updateShouldRunPathfinding(true));
dispatch(
replaceVias(
vias.filter(
Expand All @@ -60,7 +59,6 @@ export default function ModalSugerredVias({
}
return [];
});
dispatch(updateShouldRunPathfinding(true));
dispatch(replaceVias(newVias));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import {
updateDestination,
updateVias,
updateFeatureInfoClickOSRD,
updateShouldRunPathfinding,
} from 'reducers/osrdconf';
import { PointOnMap } from 'applications/operationalStudies/consts';

export default function setPointIti(pointType: string, data: PointOnMap) {
store.dispatch(updateShouldRunPathfinding(true));
const point: PointOnMap = {
...data,
location: { track_section: data.id, geo_coordinates: data.coordinates },
Expand Down
13 changes: 0 additions & 13 deletions front/src/reducers/osrdconf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const UPDATE_SCENARIO_ID = 'osrdconf/UPDATE_SCENARIO_ID';
export const UPDATE_INFRA_ID = 'osrdconf/UPDATE_INFRA_ID';
export const UPDATE_SWITCH_TYPES = 'osrdconf/UPDATE_SWITCH_TYPES';
export const UPDATE_PATHFINDING_ID = 'osrdconf/UPDATE_PATHFINDING_ID';
export const UPDATE_SHOULD_RUN_PATHFINDING = 'osrdconf/UPDATE_SHOULD_RUN_PATHFINDING';
export const UPDATE_TIMETABLE_ID = 'osrdconf/UPDATE_TIMETABLE_ID';
export const UPDATE_ROLLINGSTOCK_ID = 'osrdconf/UPDATE_ROLLINGSTOCK_ID';
export const UPDATE_ROLLINGSTOCK_COMFORT = 'osrdconf/UPDATE_ROLLINGSTOCK_COMFORT';
Expand Down Expand Up @@ -92,7 +91,6 @@ const defaultCommonConf = {
origin: undefined,
initialSpeed: 0,
departureTime: '08:00:00',
shouldRunPathfinding: true,
originDate: formatIsoDate(new Date()),
originTime: '08:00:00',
originUpperBoundDate: formatIsoDate(new Date()),
Expand Down Expand Up @@ -187,9 +185,6 @@ export default function reducer(inputState: OsrdMultiConfState | undefined, acti
// reset power restriction ranges
draft[section].powerRestrictionRanges = [];
break;
case UPDATE_SHOULD_RUN_PATHFINDING:
draft[section].shouldRunPathfinding = action.shouldRunPathfinding;
break;
case UPDATE_TIMETABLE_ID:
draft[section].timetableID = action.timetableID;
break;
Expand Down Expand Up @@ -460,14 +455,6 @@ export function updatePathfindingID(pathfindingID?: number) {
});
};
}
export function updateShouldRunPathfinding(shouldRunPathfinding: boolean) {
return (dispatch: Dispatch) => {
dispatch({
type: UPDATE_SHOULD_RUN_PATHFINDING,
shouldRunPathfinding,
});
};
}
export function updateTimetableID(timetableID?: number) {
return (dispatch: Dispatch) => {
dispatch({
Expand Down
1 change: 0 additions & 1 deletion front/src/reducers/osrdconf/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const getScenarioID = (state: RootState) => getSection(state).scenarioID;
export const getInfraID = (state: RootState) => getSection(state).infraID;
export const getSwitchTypes = (state: RootState) => getSection(state).switchTypes;
export const getPathfindingID = (state: RootState) => getSection(state).pathfindingID;
export const getShouldRunPathfinding = (state: RootState) => getSection(state).shouldRunPathfinding;
export const getTimetableID = (state: RootState) => getSection(state).timetableID;
export const getRollingStockID = (state: RootState) => getSection(state).rollingStockID;
export const getRollingStockComfort = (state: RootState) => getSection(state).rollingStockComfort;
Expand Down