Skip to content

Commit

Permalink
front: stdcm: fix op search payload
Browse files Browse the repository at this point in the history
Signed-off-by: SarahBellaha <sarah.bellaha@sncf.fr>
  • Loading branch information
SarahBellaha committed Nov 5, 2024
1 parent 8a8e684 commit 2013652
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ const StdcmLinkedTrainSearch = ({
{t('find')}
</button>
)}
{!displaySearchButton && !linkedTrainResults.length && (
{!displaySearchButton && !linkedTrainResults && (
<div className="stdcm-linked-train-button white">
<Gear size="lg" className="stdcm-linked-train-loading" />
</div>
)}
{linkedTrainResults.length > 0 ? (
{linkedTrainResults && linkedTrainResults.length > 0 ? (
<StdcmLinkedTrainResults
linkedTrainResults={linkedTrainResults}
linkedOp={{
Expand Down
61 changes: 33 additions & 28 deletions front/src/applications/stdcm/hooks/useLinkedTrainSearch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useMemo, useState, useCallback, useEffect } from 'react';

import { compact, groupBy } from 'lodash';
import { compact } from 'lodash';
import { useSelector } from 'react-redux';

import type { PathItem, SearchResultItemTrainSchedule } from 'common/api/osrdEditoastApi';
import type {
PathItem,
SearchQuery,
SearchResultItemTrainSchedule,
} from 'common/api/osrdEditoastApi';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { useInfraID, useOsrdConfSelectors } from 'common/osrdContext';
import { isArrivalDateInSearchTimeWindow, isEqualDate } from 'utils/date';
Expand Down Expand Up @@ -35,28 +39,28 @@ const useLinkedTrainSearch = () => {
const [hasSearchBeenLaunched, setHasSearchBeenLaunched] = useState(false);
const [trainNameInput, setTrainNameInput] = useState('');
const [linkedTrainDate, setLinkedTrainDate] = useState(selectableSlot.start);
const [linkedTrainResults, setLinkedTrainResults] = useState<StdcmLinkedTrainResult[]>([]);
const [linkedTrainResults, setLinkedTrainResults] = useState<StdcmLinkedTrainResult[]>();

const getExtremitiesDetails = useCallback(
async (pathItemList: PathItem[]) => {
const origin = pathItemList.at(0)!;
const destination = pathItemList.at(-1)!;
if (!('operational_point' in origin) || !('operational_point' in destination))
return undefined;
const originId = origin.operational_point;
const destinationId = destination.operational_point;
const getExtremityDetails = useCallback(
async (pathItem: PathItem) => {
if (!('operational_point' in pathItem) && !('uic' in pathItem)) return undefined;

const pathItemQuery =
'operational_point' in pathItem
? ['=', ['obj_id'], pathItem.operational_point]
: ([
'and',
['=', ['uic'], pathItem.uic],
['=', ['ch'], pathItem.secondary_code],
] as SearchQuery);

try {
const payloadOP = {
object: 'operationalpoint',
query: ['or', ['=', ['obj_id'], originId], ['=', ['obj_id'], destinationId]],
};
const resultsOP = await postSearch({ searchPayload: payloadOP, pageSize: 25 }).unwrap();
const groupedResults = groupBy(resultsOP, 'obj_id');
return {
origin: groupedResults[originId][0],
destination: groupedResults[destinationId][0],
query: pathItemQuery,
};
const opDetails = await postSearch({ searchPayload: payloadOP, pageSize: 25 }).unwrap();
return opDetails[0];
} catch (error) {
console.error('Failed to fetch operational points:', error);
return undefined;
Expand All @@ -80,8 +84,10 @@ const useLinkedTrainSearch = () => {
);

const launchTrainScheduleSearch = useCallback(async () => {
setLinkedTrainResults(undefined);
if (!trainNameInput) return;

setDisplaySearchButton(false);
setLinkedTrainResults([]);
try {
const results = (await postSearch({
searchPayload: {
Expand All @@ -108,20 +114,19 @@ const useLinkedTrainSearch = () => {
const newLinkedTrainResults = await Promise.all(
filteredResults.map(async (result) => {
const resultSummary = filteredResultsSummaries && filteredResultsSummaries[result.id];
const msFromStartTime =
resultSummary && resultSummary.status === 'success'
? resultSummary.path_item_times_final.at(-1)!
: 0; // TODO Better handling of this case
if (!resultSummary || resultSummary.status !== 'success') return undefined;
const msFromStartTime = resultSummary.path_item_times_final.at(-1)!;

const opDetails = await getExtremitiesDetails(result.path);
const originDetails = await getExtremityDetails(result.path.at(0)!);
const destinationDetails = await getExtremityDetails(result.path.at(-1)!);
const computedOpSchedules = computeOpSchedules(result.start_time, msFromStartTime);

if (opDetails === undefined) return undefined;
if (!originDetails || !destinationDetails) return undefined;
return {
trainName: result.train_name,
origin: { ...opDetails.origin, ...computedOpSchedules.origin } as StdcmLinkedPathStep,
origin: { ...originDetails, ...computedOpSchedules.origin } as StdcmLinkedPathStep,
destination: {
...opDetails.destination,
...destinationDetails,
...computedOpSchedules.destination,
} as StdcmLinkedPathStep,
};
Expand All @@ -133,7 +138,7 @@ const useLinkedTrainSearch = () => {
console.error('Train schedule search failed:', error);
setDisplaySearchButton(true);
}
}, [postSearch, trainNameInput, timetableId, linkedTrainDate, getExtremitiesDetails]);
}, [postSearch, trainNameInput, timetableId, linkedTrainDate, getExtremityDetails]);

useEffect(() => {
if (!isArrivalDateInSearchTimeWindow(linkedTrainDate, searchDatetimeWindow)) {
Expand Down

0 comments on commit 2013652

Please sign in to comment.