1- import React , { useState , useMemo } from 'react' ;
1+ import React , { useState , useEffect , useMemo } from 'react' ;
22import { useTranslation } from 'react-i18next' ;
33import { ComboBox } from '@carbon/react' ;
44import { useLocationByUuid , useLocations } from '../location-picker/location-picker.resource' ;
55import styles from './location-selector.module.scss' ;
66
7+ interface Location {
8+ resource : { id : string ; name : string } ;
9+ }
10+
711interface LocationSelectorProps {
812 selectedLocationUuid ?: string ;
913 defaultLocationUuid ?: string ;
@@ -25,46 +29,49 @@ export const LocationSelector: React.FC<LocationSelectorProps> = ({
2529} ) => {
2630 const { t } = useTranslation ( ) ;
2731 const [ searchTerm , setSearchTerm ] = useState < string > ( '' ) ;
32+ const [ tempLocations , setTempLocations ] = useState < Location [ ] > ( [ ] ) ;
2833
29- let defaultLocation = useLocationByUuid ( defaultLocationUuid ) . location ;
34+ const { location : defaultLocation } = useLocationByUuid ( defaultLocationUuid ) ;
35+ const { locations : fetchedLocations = [ ] } = useLocations ( locationTag , locationsPerRequest , searchTerm ) ;
3036
31- const { locations : fetchedLocations } = useLocations ( locationTag , locationsPerRequest , searchTerm ) ;
37+ useEffect ( ( ) => {
38+ if ( fetchedLocations . length > 0 ) {
39+ setTempLocations ( fetchedLocations ) ;
40+ }
41+ } , [ fetchedLocations ] ) ;
3242
33- const locations = useMemo ( ( ) => {
43+ const effectiveLocations = useMemo < Location [ ] > ( ( ) => {
44+ const base = fetchedLocations . length > 0 ? fetchedLocations : tempLocations ;
3445 if ( defaultLocation && ! searchTerm ) {
35- return [ defaultLocation , ...fetchedLocations ? .filter ( ( { resource } ) => resource . id !== defaultLocationUuid ) ] ;
46+ return [ defaultLocation , ...base . filter ( ( loc ) => loc . resource . id !== defaultLocationUuid ) ] ;
3647 }
37- return fetchedLocations ;
38- } , [ defaultLocation , fetchedLocations ] ) ;
48+ return base ;
49+ } , [ defaultLocation , defaultLocationUuid , fetchedLocations , tempLocations , searchTerm ] ) ;
3950
40- const handleSearch = ( searchString ) => {
41- setSearchTerm ( searchString ) ;
42- } ;
43-
44- const items = useMemo ( ( ) => {
45- const filtered = locations . map ( ( loc ) => ( {
46- id : loc . resource . id ,
47- name : loc . resource . name ,
48- } ) ) ;
49- return filtered ;
50- } , [ locations , searchTerm ] ) ;
51+ const items = useMemo (
52+ ( ) =>
53+ effectiveLocations . map ( ( loc ) => ( {
54+ id : loc . resource . id ,
55+ label : loc . resource . name ,
56+ } ) ) ,
57+ [ effectiveLocations ] ,
58+ ) ;
5159
5260 return (
5361 < section data-testid = "combo" >
5462 < div className = { styles . sectionTitle } > { Locationlabel } </ div >
55- < div >
56- < ComboBox
57- aria-label = { comBoxLabel }
58- id = "location"
59- invalidText = { 'Required' }
60- items = { items }
61- itemToString = { ( item ) => item ?. name || '' }
62- selectedItem = { items . find ( ( item ) => item . id === selectedLocationUuid ) }
63- onChange = { ( { selectedItem } ) => onChange ( selectedItem ?. id ) }
64- onInputChange = { ( searchTerm ) => handleSearch ( searchTerm ) }
65- titleText = { comBoxLabel }
66- />
67- </ div >
63+ < ComboBox
64+ aria-label = { comBoxLabel }
65+ id = "location"
66+ invalidText = { t ( 'Required' ) }
67+ items = { items }
68+ initialSelectedItem = { items . find ( ( i ) => i . id === defaultLocationUuid ) }
69+ selectedItem = { items . find ( ( i ) => i . id === selectedLocationUuid ) }
70+ itemToString = { ( item ) => item ?. label || '' }
71+ onChange = { ( evt ) => onChange ( evt . selectedItem ?. id ) }
72+ onInputChange = { ( value ) => setSearchTerm ( value ) }
73+ titleText = { comBoxLabel }
74+ />
6875 </ section >
6976 ) ;
7077} ;
0 commit comments