Skip to content

Commit

Permalink
front: adapt trainschedule results for trainschedule v2
Browse files Browse the repository at this point in the history
  • Loading branch information
SharglutDev committed May 14, 2024
1 parent 56e7358 commit 91f3edb
Show file tree
Hide file tree
Showing 24 changed files with 1,779 additions and 872 deletions.
66 changes: 64 additions & 2 deletions front/src/applications/operationalStudies/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { useEffect } from 'react';
import { useEffect, useState } from 'react';

import { useSelector } from 'react-redux';

import { enhancedEditoastApi } from 'common/api/enhancedEditoastApi';
import {
osrdEditoastApi,
type PathProperties,
type PostV2InfraByInfraIdPathPropertiesApiArg,
type PostV2InfraByInfraIdPathfindingBlocksApiArg,
} from 'common/api/osrdEditoastApi';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { useInfraID, useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { formatSuggestedOperationalPoints, insertViasInOPs } from 'modules/pathfinding/utils';
import { getSupportedElectrification, isThermal } from 'modules/rollingStock/helpers/electric';
import { adjustConfWithTrainToModifyV2 } from 'modules/trainschedule/components/ManageTrainSchedule/helpers/adjustConfWithTrainToModify';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import { setFailure } from 'reducers/main';
import type { PathStep } from 'reducers/osrdconf/types';
import { getSelectedProjectionV2, getSelectedTrainId } from 'reducers/osrdsimulation/selectors';
import { useAppDispatch } from 'store';
import { castErrorToFailure } from 'utils/error';
import { getPointCoordinates } from 'utils/geometry';
Expand Down Expand Up @@ -133,4 +135,64 @@ export const useSetupItineraryForTrainUpdate = (
}, [trainScheduleIDsToModify]);
};

export const useSimulationResults = () => {
const infraId = useInfraID();
const selectedTrainId = useSelector(getSelectedTrainId);
const selectedProjection = useSelector(getSelectedProjectionV2);

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

const { data: selectedTrainSchedule } = osrdEditoastApi.endpoints.getTrainScheduleById.useQuery(
{
id: selectedTrainId as number,
},
{ skip: !selectedTrainId }
);

const { data: selectedTrainRollingStock } =
osrdEditoastApi.endpoints.getLightRollingStockByRollingStockId.useQuery(
{
rollingStockId: selectedTrainSchedule?.rolling_stock_id as number,
},
{ skip: !selectedTrainSchedule }
);

const { data: pathfindingResult } = osrdEditoastApi.endpoints.getV2TrainScheduleByIdPath.useQuery(
{
id: selectedProjection as number,
infraId: infraId as number,
},
{ skip: !selectedProjection || !infraId }
);

const { data: trainSimulation } =
osrdEditoastApi.endpoints.getV2TrainScheduleByIdSimulation.useQuery(
{ id: selectedTrainId as number, infraId: infraId as number },
{ skip: !selectedTrainId || !infraId }
);

const [postPathProperties] =
enhancedEditoastApi.endpoints.postV2InfraByInfraIdPathProperties.useMutation();

useEffect(() => {
const getPathProperties = async () => {
if (infraId && pathfindingResult && pathfindingResult.status === 'success') {
const pathPropertiesParams: PostV2InfraByInfraIdPathPropertiesApiArg = {
infraId,
props: ['electrifications', 'geometry', 'operational_points', 'gradients', 'slopes'],
pathPropertiesInput: {
track_section_ranges: pathfindingResult.track_section_ranges,
},
};
const pathProperties = await postPathProperties(pathPropertiesParams).unwrap();

setPathProperties(pathProperties);
}
};
getPathProperties();
}, [pathfindingResult, infraId]);

return { selectedTrainSchedule, selectedTrainRollingStock, trainSimulation };
};

export default useSetupItineraryForTrainUpdate;
88 changes: 39 additions & 49 deletions front/src/applications/operationalStudies/views/v2/ScenarioV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import BreadCrumbs from 'applications/operationalStudies/components/BreadCrumbs'
import InfraLoadingState from 'applications/operationalStudies/components/Scenario/InfraLoadingState';
import { MANAGE_TRAIN_SCHEDULE_TYPES } from 'applications/operationalStudies/consts';
import infraLogo from 'assets/pictures/components/tracks.svg';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { osrdEditoastApi, type ProjectPathTrainResult } from 'common/api/osrdEditoastApi';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import NavBarSNCF from 'common/BootstrapSNCF/NavBarSNCF';
import { useInfraID, useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
Expand All @@ -19,10 +19,11 @@ import ScenarioLoaderMessage from 'modules/scenario/components/ScenarioLoaderMes
import TimetableManageTrainScheduleV2 from 'modules/trainschedule/components/ManageTrainSchedule/TimetableManageTrainScheduleV2';
import TimetableV2 from 'modules/trainschedule/components/TimetableV2/TimetableV2';
import type { RootState } from 'reducers';
import { updateSelectedProjection, updateSimulation } from 'reducers/osrdsimulation/actions';
import { getPresentSimulation, getSelectedTrainId } from 'reducers/osrdsimulation/selectors';
import { updateSelectedProjection } from 'reducers/osrdsimulation/actions';
import { getSelectedProjectionV2, getSelectedTrainId } from 'reducers/osrdsimulation/selectors';
import { useAppDispatch } from 'store';

import { getSimulationResultsV2, selectProjectionV2 } from './getSimulationResultsV2';
import ImportTrainScheduleV2 from './ImportTrainScheduleV2';
import ManageTrainScheduleV2 from './ManageTrainScheduleV2';
import SimulationResultsV2 from './SimulationResultsV2';
Expand All @@ -43,7 +44,8 @@ const ScenarioV2 = () => {
const [collapsedTimetable, setCollapsedTimetable] = useState(false);
const [isInfraLoaded, setIsInfraLoaded] = useState(false);
const [reloadCount, setReloadCount] = useState(1);
const [, setTrainResultsToFetch] = useState<number[] | undefined>();
const [spaceTimeData, setSpaceTimeData] = useState<ProjectPathTrainResult>();
const [trainResultsToFetch, setTrainResultsToFetch] = useState<number[] | undefined>();
const isUpdating = useSelector((state: RootState) => state.osrdsimulation.isUpdating);

const { openModal } = useModal();
Expand All @@ -66,9 +68,7 @@ const ScenarioV2 = () => {
const infraId = useInfraID();
const timetableId = useSelector(getTimetableID);
const selectedTrainId = useSelector(getSelectedTrainId);
// const selectedProjection = useSelector(getSelectedProjection);
// const allowancesSettings = useSelector(getAllowancesSettings);
const simulation = useSelector(getPresentSimulation);
const selectedProjection = useSelector(getSelectedProjectionV2);

const {
data: scenario,
Expand Down Expand Up @@ -136,50 +136,38 @@ const ScenarioV2 = () => {
}
}, [infraId]);

/*
* Timetable is refetched automatically if a train schedule is updated or deleted
* but not if one is created (in importTrainScheduleModal, we don't want to refetch
* the timetable each time a train is created), that is why the refetch is needed here
*/
const { data: timetable, refetch: refetchTimetable } =
osrdEditoastApi.endpoints.getV2TimetableById.useQuery(
{ id: timetableId! },
{
skip: !timetableId,
// This forces the refetch of the timetable each time the component mounts
refetchOnMountOrArgChange: true,
}
);
const { data: timetable } = osrdEditoastApi.endpoints.getV2TimetableById.useQuery(
{ id: timetableId! },
{
skip: !timetableId,
// This forces the refetch of the timetable each time the component mounts
refetchOnMountOrArgChange: true,
}
);

// const { refetch: refetchConflicts } =
// osrdEditoastApi.endpoints.getV2TimetableByIdConflicts.useQuery(
// { id: timetable!.id, infraId: infraId! },
// { skip: !timetable || !infraId }
// );

// useEffect(() => {
// if (timetable && infra?.state === 'CACHED' && timetable.train_schedule_summaries.length > 0) {
// selectProjection(timetable.train_schedule_summaries, selectedProjection, selectedTrainId);
// }
// }, [timetable, infra]);
useEffect(() => {
if (timetable && infra?.state === 'CACHED' && timetable.train_ids.length > 0) {
selectProjectionV2(timetable.train_ids, selectedProjection, selectedTrainId);
}
}, [timetable, infra]);

// useEffect(() => {
// if (timetable && infra?.state === 'CACHED' && selectedProjection) {
// // If trainResultsToFetch is undefined that means it's the first load of the scenario
// // and we want to get all timetable trains results
// if (trainResultsToFetch) {
// getSimulationResults(
// trainResultsToFetch,
// selectedProjection,
// allowancesSettings,
// simulation.trains as SimulationReport[]
// );
// } else {
// const trainScheduleIds = timetable.train_schedule_summaries.map((train) => train.id);
// getSimulationResults(trainScheduleIds, selectedProjection, allowancesSettings);
// }
// }
// }, [timetable, infra, selectedProjection]);
useEffect(() => {
if (timetable && infra?.state === 'CACHED' && selectedProjection && infraId) {
// If trainResultsToFetch is undefined that means it's the first load of the scenario
// and we want to get all timetable trains results
if (trainResultsToFetch) {
getSimulationResultsV2(trainResultsToFetch, selectedProjection, infraId, setSpaceTimeData);
} else {
getSimulationResultsV2(timetable.train_ids, selectedProjection, infraId, setSpaceTimeData);
}
}
}, [timetable, infra, selectedProjection]);

useEffect(() => {
if (!projectId || !studyId || !scenarioId) {
Expand All @@ -191,7 +179,6 @@ const ScenarioV2 = () => {
() => () => {
dispatch(updateTimetableID(undefined));
dispatch(updateInfraID(undefined));
dispatch(updateSimulation({ trains: [] }));
dispatch(updateSelectedProjection(undefined));
},
[]
Expand Down Expand Up @@ -298,7 +285,6 @@ const ScenarioV2 = () => {
setDisplayTrainScheduleManagement={setDisplayTrainScheduleManagement}
setTrainResultsToFetch={setTrainResultsToFetch}
infraState={infra.state}
// refetchTimetable={refetchTimetable}
// refetchConflicts={() => {}}
/>
)}
Expand All @@ -307,12 +293,10 @@ const ScenarioV2 = () => {
setDisplayTrainScheduleManagement={setDisplayTrainScheduleManagement}
trainsWithDetails={trainsWithDetails}
infraState={infra.state}
timetable={undefined}
trainIds={timetable.train_ids}
selectedTrainId={selectedTrainId}
refetchTimetable={refetchTimetable}
conflicts={[]}
setTrainResultsToFetch={setTrainResultsToFetch}
simulation={simulation}
/>
)}
</div>
Expand Down Expand Up @@ -360,7 +344,13 @@ const ScenarioV2 = () => {
</div>
</div>
)}
{isInfraLoaded && infra && <SimulationResultsV2 />}
{isInfraLoaded && infra && (
<SimulationResultsV2
collapsedTimetable={collapsedTimetable}
// setTrainResultsToFetch={setTrainResultsToFetch}
spaceTimeData={spaceTimeData}
/>
)}
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit 91f3edb

Please sign in to comment.