Skip to content

Commit

Permalink
fixup! front: refacto useOutputData
Browse files Browse the repository at this point in the history
  • Loading branch information
clarani committed Nov 22, 2024
1 parent c77241e commit 79322d9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,18 @@ const SimulationResults = ({
</div>

{/* TIME STOPS TABLE */}
{selectedTrainSchedule && pathProperties && selectedTrainSummary && (
<div className="time-stop-outputs">
<p className="mt-2 mb-3 ml-3 font-weight-bold">{t('timetableOutput')}</p>
<TimesStopsOutput
simulatedTrain={trainSimulation}
trainSummary={selectedTrainSummary}
pathProperties={pathProperties}
selectedTrainSchedule={selectedTrainSchedule}
path={path}
dataIsLoading={formattedOpPointsLoading}
/>
</div>
)}

<div className="time-stop-outputs">
<p className="mt-2 mb-3 ml-3 font-weight-bold">{t('timetableOutput')}</p>
<TimesStopsOutput
simulatedTrain={trainSimulation}
trainSummary={selectedTrainSummary}
pathProperties={pathProperties}
selectedTrainSchedule={selectedTrainSchedule}
path={path}
dataIsLoading={formattedOpPointsLoading}
/>
</div>

{/* SIMULATION EXPORT BUTTONS */}
{selectedTrainSchedule &&
Expand Down
8 changes: 4 additions & 4 deletions front/src/modules/timesStops/TimesStopsOutput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { TableType, type TimeStopsRow } from './types';

type TimesStopsOutputProps = {
simulatedTrain: SimulationResponseSuccess;
trainSummary: TrainScheduleWithDetails;
pathProperties: PathPropertiesFormatted;
selectedTrainSchedule: TrainScheduleResult;
trainSummary?: TrainScheduleWithDetails;
pathProperties?: PathPropertiesFormatted;
selectedTrainSchedule?: TrainScheduleResult;
path?: PathfindingResultSuccess;
dataIsLoading: boolean;
};
Expand All @@ -38,7 +38,7 @@ const TimesStopsOutput = ({
path
);

if (dataIsLoading) {
if (dataIsLoading || !trainSummary || !pathProperties || !selectedTrainSchedule) {
return (
<div style={{ height: '600px' }}>
<Loader />
Expand Down
83 changes: 42 additions & 41 deletions front/src/modules/timesStops/hooks/useOutputTableData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-plusplus */
import { useMemo } from 'react';

import { keyBy } from 'lodash';
Expand All @@ -20,24 +19,24 @@ import { type ScheduleEntry, type TimeStopsRow } from '../types';

const useOutputTableData = (
{ final_output: simulatedTrain }: SimulationResponseSuccess,
trainSummary: TrainScheduleWithDetails,
pathProperties: PathPropertiesFormatted,
selectedTrainSchedule: TrainScheduleResult,
trainSummary?: TrainScheduleWithDetails,
pathProperties?: PathPropertiesFormatted,
selectedTrainSchedule?: TrainScheduleResult,
path?: PathfindingResultSuccess
): TimeStopsRow[] => {
const { t } = useTranslation('timesStops');

const scheduleByAt: Record<string, ScheduleEntry> = keyBy(selectedTrainSchedule.schedule, 'at');

const startDatetime = new Date(selectedTrainSchedule.start_time);
const scheduleByAt: Record<string, ScheduleEntry> = keyBy(selectedTrainSchedule?.schedule, 'at');

const pathStepRows = useMemo(() => {
if (!path || !selectedTrainSchedule || !trainSummary.pathItemTimes) return [];
if (!path || !selectedTrainSchedule || !trainSummary?.pathItemTimes) return [];

const startDatetime = new Date(selectedTrainSchedule.start_time);

const result: (TimeStopsRow & { positionOnPath: number })[] = [];
let previousTime = new Date(selectedTrainSchedule.start_time);

for (let index = 0; index < selectedTrainSchedule.path.length; index++) {
for (let index = 0; index < selectedTrainSchedule.path.length; index += 1) {
const pathStep = selectedTrainSchedule.path[index];
const schedule: ScheduleEntry | undefined = scheduleByAt[pathStep.id];

Expand Down Expand Up @@ -81,44 +80,46 @@ const useOutputTableData = (
}

return result;
}, [selectedTrainSchedule, path]);

const rows: TimeStopsRow[] = useMemo(
() =>
pathProperties.operationalPoints.map((op) => {
const matchingPathStep = pathStepRows.find(
(pathStepRow) => op.position === pathStepRow.positionOnPath
);
if (matchingPathStep) {
return {
...matchingPathStep,
opId: op.id,
name: op.extensions?.identifier?.name,
ch: op.extensions?.sncf?.ch,
};
}

// compute arrival time
const matchingReportTrainIndex = simulatedTrain.positions.findIndex(
(position) => position === op.position
);

const time =
matchingReportTrainIndex === -1
? interpolateValue(simulatedTrain, op.position, 'times')
: simulatedTrain.times[matchingReportTrainIndex];
const calculatedArrival = new Date(startDatetime.getTime() + time);
}, [selectedTrainSchedule, path, trainSummary?.pathItemTimes]);

const rows: TimeStopsRow[] = useMemo(() => {
if (!pathProperties || !selectedTrainSchedule) return [];

const startDatetime = new Date(selectedTrainSchedule.start_time);

return pathProperties.operationalPoints.map((op) => {
const matchingPathStep = pathStepRows.find(
(pathStepRow) => op.position === pathStepRow.positionOnPath
);
if (matchingPathStep) {
return {
isWaypoint: false,
...matchingPathStep,
opId: op.id,
name: op.extensions?.identifier?.name,
ch: op.extensions?.sncf?.ch,
calculatedArrival: dateToHHMM(calculatedArrival, { withSeconds: true }),
};
}),
[pathProperties.operationalPoints]
);
}

// compute arrival time
const matchingReportTrainIndex = simulatedTrain.positions.findIndex(
(position) => position === op.position
);

const time =
matchingReportTrainIndex === -1
? interpolateValue(simulatedTrain, op.position, 'times')
: simulatedTrain.times[matchingReportTrainIndex];
const calculatedArrival = new Date(startDatetime.getTime() + time);

return {
isWaypoint: false,
opId: op.id,
name: op.extensions?.identifier?.name,
ch: op.extensions?.sncf?.ch,
calculatedArrival: dateToHHMM(calculatedArrival, { withSeconds: true }),
};
});
}, [pathProperties?.operationalPoints, pathStepRows, simulatedTrain, selectedTrainSchedule]);

return rows;
};
Expand Down

0 comments on commit 79322d9

Please sign in to comment.