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: add scenario train ids to query params and local storage #9429

Merged
merged 1 commit into from
Oct 24, 2024
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
Expand Up @@ -11,7 +11,6 @@ import {
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { setFailure } from 'reducers/main';
import { updateSelectedTrainId, updateTrainIdUsedForProjection } from 'reducers/simulationResults';
import {
getSelectedTrainId,
getTrainIdUsedForProjection,
Expand Down Expand Up @@ -158,15 +157,6 @@ const useScenarioData = (
});
}, []);

useEffect(() => {
dispatch(updateTrainIdUsedForProjection(undefined));
dispatch(updateSelectedTrainId(undefined));
return () => {
dispatch(updateTrainIdUsedForProjection(undefined));
dispatch(updateSelectedTrainId(undefined));
};
}, []);

return {
selectedTrainId,
trainScheduleSummaries,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useEffect, useCallback, useMemo } from 'react';

import { useSelector } from 'react-redux';
import { useLocation, useNavigate, useParams } from 'react-router-dom';

import { updateSelectedTrainId, updateTrainIdUsedForProjection } from 'reducers/simulationResults';
import {
getSelectedTrainId,
getTrainIdUsedForProjection,
} from 'reducers/simulationResults/selectors';
import { useAppDispatch } from 'store';

type SimulationParams = {
projectId: string;
studyId: string;
scenarioId: string;
};

const useScenarioQueryParams = () => {
const location = useLocation();
const navigate = useNavigate();
const dispatch = useAppDispatch();

const {
projectId: urlProjectId,
studyId: urlStudyId,
scenarioId: urlScenarioId,
} = useParams() as SimulationParams;
const localKey = `useScenarioQueryParams_project${urlProjectId}_study${urlStudyId}_scenario${urlScenarioId}`;

const searchParams = useMemo(() => new URLSearchParams(location.search), [location.search]);

const reduxSelectedTrainId = useSelector(getSelectedTrainId);
const reduxProjectionTrainId = useSelector(getTrainIdUsedForProjection);

// Helper function to get a parameter from the URL, or if absent from local storage
const getParamFromUrlOrStorage = useCallback(
(paramName: string) =>
searchParams.get(paramName) || localStorage.getItem(`${localKey}_${paramName}`) || undefined,
[localKey, searchParams]
);

// Helper function to get a parameter from the URL or local storage
const setParamsInUrlAndStorage = useCallback(
(paramName: string, paramValue: string | undefined) => {
if (paramValue === undefined) {
searchParams.delete(paramName);
localStorage.removeItem(`${localKey}_${paramName}`);
} else {
searchParams.set(paramName, paramValue);
localStorage.setItem(`${localKey}_${paramName}`, paramValue);
}
navigate(`${location.pathname}?${searchParams.toString()}`, { replace: true });
},
[localKey, searchParams, location.pathname, navigate]
);

// Update the redux store on page load
useEffect(() => {
const selectedTrainFromUrl = getParamFromUrlOrStorage('selected_train');
const projectionFromUrl = getParamFromUrlOrStorage('projection');

if (selectedTrainFromUrl) {
dispatch(updateSelectedTrainId(Number(selectedTrainFromUrl)));
}
if (projectionFromUrl) {
dispatch(updateTrainIdUsedForProjection(Number(projectionFromUrl)));
}
}, [dispatch, getParamFromUrlOrStorage]);

// Update the URL and local storage on redux store change
useEffect(() => {
if (reduxSelectedTrainId?.toString() !== getParamFromUrlOrStorage('selected_train')) {
setParamsInUrlAndStorage('selected_train', reduxSelectedTrainId?.toString());
}
if (reduxProjectionTrainId?.toString() !== getParamFromUrlOrStorage('projection')) {
setParamsInUrlAndStorage('projection', reduxProjectionTrainId?.toString());
}
}, [
reduxSelectedTrainId,
reduxProjectionTrainId,
setParamsInUrlAndStorage,
getParamFromUrlOrStorage,
]);
};

export default useScenarioQueryParams;
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import BreadCrumbs from 'applications/operationalStudies/components/BreadCrumbs';
import ScenarioContent from 'applications/operationalStudies/components/Scenario/ScenarioContent';
import useScenario from 'applications/operationalStudies/hooks/useScenario';
import useScenarioQueryParams from 'applications/operationalStudies/hooks/useScenarioQueryParams';
import NavBarSNCF from 'common/BootstrapSNCF/NavBarSNCF';
import useInfraStatus from 'modules/pathfinding/hooks/useInfraStatus';

const Scenario = () => {
const { scenario, timetable } = useScenario();

// Initialize and sync the URL and local storage with Redux
useScenarioQueryParams();

const infraData = useInfraStatus();
const { infra } = infraData;

Expand Down
Loading