Skip to content

Commit 32cce1b

Browse files
fix location selecting issue
1 parent 4f342b2 commit 32cce1b

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed
Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import React, { useState, useMemo } from 'react';
1+
import React, { useState, useEffect, useMemo } from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { ComboBox } from '@carbon/react';
44
import { useLocationByUuid, useLocations } from '../location-picker/location-picker.resource';
55
import styles from './location-selector.module.scss';
66

7+
interface Location {
8+
resource: { id: string; name: string };
9+
}
10+
711
interface 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

Comments
 (0)