diff --git a/front/src/applications/stdcm/components/StdcmForm/StdcmLinkedTrainSearch.tsx b/front/src/applications/stdcm/components/StdcmForm/StdcmLinkedTrainSearch.tsx
index 59289e85af4..2f4b46f264e 100644
--- a/front/src/applications/stdcm/components/StdcmForm/StdcmLinkedTrainSearch.tsx
+++ b/front/src/applications/stdcm/components/StdcmForm/StdcmLinkedTrainSearch.tsx
@@ -93,12 +93,12 @@ const StdcmLinkedTrainSearch = ({
{t('find')}
)}
- {!displaySearchButton && !linkedTrainResults.length && (
+ {!displaySearchButton && !linkedTrainResults && (
)}
- {linkedTrainResults.length > 0 ? (
+ {linkedTrainResults && linkedTrainResults.length > 0 ? (
{
const [hasSearchBeenLaunched, setHasSearchBeenLaunched] = useState(false);
const [trainNameInput, setTrainNameInput] = useState('');
const [linkedTrainDate, setLinkedTrainDate] = useState(selectableSlot.start);
- const [linkedTrainResults, setLinkedTrainResults] = useState([]);
+ const [linkedTrainResults, setLinkedTrainResults] = useState();
- 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;
@@ -80,8 +84,10 @@ const useLinkedTrainSearch = () => {
);
const launchTrainScheduleSearch = useCallback(async () => {
+ setLinkedTrainResults(undefined);
+ if (!trainNameInput) return;
+
setDisplaySearchButton(false);
- setLinkedTrainResults([]);
try {
const results = (await postSearch({
searchPayload: {
@@ -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,
};
@@ -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)) {