@@ -3,6 +3,7 @@ import { useEffect, useMemo, useState } from 'react';
3
3
import { Select , ComboBox } from '@osrd-project/ui-core' ;
4
4
import { useTranslation } from 'react-i18next' ;
5
5
6
+ import type { SearchResultItemOperationalPoint } from 'common/api/osrdEditoastApi' ;
6
7
import useSearchOperationalPoint from 'common/Map/Search/useSearchOperationalPoint' ;
7
8
import { useOsrdConfActions } from 'common/osrdContext' ;
8
9
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf' ;
@@ -23,6 +24,20 @@ function formatChCode(chCode: string) {
23
24
return chCode === '' ? 'BV' : chCode ;
24
25
}
25
26
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
+
26
41
const StdcmOperationalPoint = ( { location, pathStepId, disabled } : StdcmOperationalPointProps ) => {
27
42
const { t } = useTranslation ( 'stdcm' ) ;
28
43
const dispatch = useAppDispatch ( ) ;
@@ -93,18 +108,7 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
93
108
94
109
const handleCiSelect = ( selectedSuggestion ?: CIOption ) => {
95
110
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 ) ;
108
112
setChSuggestions ( newChSuggestions ) ;
109
113
} else {
110
114
setChSuggestions ( [ ] ) ;
@@ -147,6 +151,15 @@ const StdcmOperationalPoint = ({ location, pathStepId, disabled }: StdcmOperatio
147
151
}
148
152
} , [ location ] ) ;
149
153
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
+
150
163
return (
151
164
< div className = "location-line" >
152
165
< div className = "col-9 ci-input" >
0 commit comments