Skip to content

Commit 601f66f

Browse files
committed
front: fix stdcm empty ch list in new query
Signed-off-by: SharglutDev <p.filimon75@gmail.com>
1 parent 8affe7b commit 601f66f

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

front/src/applications/stdcm/components/StdcmForm/StdcmOperationalPoint.tsx

+25-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useEffect, useMemo, useState } from 'react';
33
import { Select, ComboBox } from '@osrd-project/ui-core';
44
import { useTranslation } from 'react-i18next';
55

6+
import type { SearchResultItemOperationalPoint } from 'common/api/osrdEditoastApi';
67
import useSearchOperationalPoint from 'common/Map/Search/useSearchOperationalPoint';
78
import { useOsrdConfActions } from 'common/osrdContext';
89
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
@@ -23,6 +24,20 @@ function formatChCode(chCode: string) {
2324
return chCode === '' ? 'BV' : chCode;
2425
}
2526

27+
const extractChCodes = (searchResults: SearchResultItemOperationalPoint[], selectedCI: CIOption) =>
28+
searchResults
29+
.filter((pr) => pr.name === selectedCI.name)
30+
.reduce((acc, pr) => {
31+
const newObject = {
32+
label: formatChCode(pr.ch),
33+
id: pr.ch,
34+
coordinates: pr.geographic.coordinates as [number, number],
35+
};
36+
const isDuplicate = acc.some((option) => option.label === newObject.label);
37+
if (!isDuplicate) acc.push(newObject);
38+
return acc;
39+
}, [] as CHOption[]);
40+
2641
const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperationalPointProps) => {
2742
const { t } = useTranslation('stdcm');
2843
const dispatch = useAppDispatch();
@@ -93,18 +108,7 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
93108

94109
const handleCiSelect = (selectedSuggestion?: CIOption) => {
95110
if (selectedSuggestion) {
96-
const newChSuggestions = searchResults
97-
.filter((pr) => pr.name === selectedSuggestion.name)
98-
.reduce((acc, pr) => {
99-
const newObject = {
100-
label: formatChCode(pr.ch),
101-
id: pr.ch,
102-
coordinates: pr.geographic.coordinates as [number, number],
103-
};
104-
const isDuplicate = acc.some((option) => option.label === newObject.label);
105-
if (!isDuplicate) acc.push(newObject);
106-
return acc;
107-
}, [] as CHOption[]);
111+
const newChSuggestions = extractChCodes(searchResults, selectedSuggestion);
108112
setChSuggestions(newChSuggestions);
109113
} else {
110114
setChSuggestions([]);
@@ -147,6 +151,15 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
147151
}
148152
}, [location]);
149153

154+
useEffect(() => {
155+
// If we start a new query with inputs (ch suggestions will be empty at load),
156+
// fetch the ch list again for the corresponding CI
157+
if (chSuggestions.length === 0 && selectedCi && searchResults.length > 0) {
158+
const updatedChSuggestions = extractChCodes(searchResults, selectedCi);
159+
setChSuggestions(updatedChSuggestions);
160+
}
161+
}, [searchResults, selectedCi, chSuggestions]);
162+
150163
return (
151164
<div className="location-line">
152165
<div className="col-9 ci-input">

0 commit comments

Comments
 (0)