Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Selected location not being shown for some cases in Assets #6790

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/Components/Common/LocationSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDispatch } from "react-redux";
import { listFacilityAssetLocation } from "../../Redux/actions";
import AutocompleteFormField from "../Form/FormFields/Autocomplete";
import AutocompleteMultiSelectFormField from "../Form/FormFields/AutocompleteMultiselect";
import { useAsyncOptions } from "../../Common/hooks/useAsyncOptions";
interface LocationSelectProps {
name: string;
disabled?: boolean;
Expand Down Expand Up @@ -32,8 +33,13 @@ export const LocationSelect = (props: LocationSelectProps) => {
const [locations, setLocations] = useState<{ name: string; id: string }[]>(
[]
);

const { fetchOptions, isLoading, options } = useAsyncOptions<{
id: string;
name: string;
}>("id");

const [query, setQuery] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const dispatchAction: any = useDispatch();

const handleValueChange = (current: string[]) => {
Expand All @@ -47,12 +53,10 @@ export const LocationSelect = (props: LocationSelectProps) => {
limit: 14,
search_text: query,
};
setLoading(true);
dispatchAction(
listFacilityAssetLocation(params, { facility_external_id: facilityId })
).then(({ data }: any) => {
setLocations(data.results);
setLoading(false);
});
}, [query, facilityId]);

Expand All @@ -78,12 +82,21 @@ export const LocationSelect = (props: LocationSelectProps) => {
name={name}
disabled={disabled}
value={selected as string}
options={locations}
options={options(locations)}
onChange={({ value }) => handleValueChange([value])}
onQuery={(query) => {
setQuery(query);
}}
isLoading={loading}
onQuery={(query) =>
facilityId ??
fetchOptions(
listFacilityAssetLocation(
{
limit: 14,
search_text: query,
},
{ facility_external_id: facilityId }
)
)
}
isLoading={isLoading}
placeholder="Search by location name"
optionLabel={(option) => option.name}
optionValue={(option) => option.id}
Expand Down
Loading