Skip to content

Commit

Permalink
chore: adding loading
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Nov 28, 2024
1 parent e2b9a70 commit 499d120
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
19 changes: 10 additions & 9 deletions frontend/src/components/AutocompleteClientLocation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ForestClientLocation
} from "../../services/OpeningClientLocationService";


interface AutocompleteProps {
id: string,
label: string,
Expand Down Expand Up @@ -65,7 +64,8 @@ export const fetchValues = async (query: string, key: string) => {
};

const AutocompleteClientLocation: React.ForwardRefExoticComponent<AutocompleteComponentProps & React.RefAttributes<AutocompleteComponentRefProps>> = forwardRef<AutocompleteComponentRefProps, AutocompleteComponentProps>(
({ setValue }, ref) => {
({ setValue }, ref) =>
{
const { options, fetchOptions, setOptions } = useAutocomplete();
const [isActive, setIsActive] = useState(false);
const [location, setLocation] = useState<AutocompleteProps | null>(null);
Expand Down Expand Up @@ -107,6 +107,7 @@ const AutocompleteClientLocation: React.ForwardRefExoticComponent<AutocompleteCo
clearLocation();
}
};

useImperativeHandle(ref, () => ({
reset: clearLocation
}));
Expand All @@ -116,23 +117,23 @@ const AutocompleteClientLocation: React.ForwardRefExoticComponent<AutocompleteCo
<ComboBox
id="client-name"
className="flex-fill"
allowCustomValue
allowCustomValue={false}
selectedItem={client}
onInputChange={(e: string) => fetchOptions(e, "clients")}
onInputChange={(value: string) => fetchOptions(value, "clients")}
onChange={handleClientChange}
itemToElement={(item: AutocompleteProps) => item.label}
helperText="Search by client name, number or acronym"
items={options["clients"] || []}
titleText="Client"
typeahead />
titleText="Client" />

<ComboBox
disabled={!isActive}
id="client-location"
className="flex-fill"
allowCustomValue
selectedItem={location}
onChange={handleLocationSelection}
items={options["locations"] || []}
onChange={handleLocationSelection}
itemToElement={(item: AutocompleteProps) => item.label}
items={options["locations"] || [{ id: "", label: "No results found" }]}
titleText="Location code"
typeahead />
</div>
Expand Down
25 changes: 20 additions & 5 deletions frontend/src/contexts/AutocompleteProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createContext, useContext, ReactNode, useState, useRef, useEffect } from "react";
import { InlineLoading } from "@carbon/react";
import { debounce, DebouncedFunc } from "lodash";

// TODO: test this

interface AutocompleteProviderProps {
fetchOptions: (query: string, key: string) => Promise<any[]>;
skipConditions?: Record<string, (query: string) => boolean>;
Expand All @@ -17,6 +16,13 @@ interface AutocompleteContextType {
setOptions: (key: string, items: any[]) => void;
}

const searchingForItems = [{
id: "",
label: (<InlineLoading status="active" iconDescription="Loading" description="Loading data..." />)
}];

const noDataFound = [{ id: "", label: "No results found" }];

const AutocompleteContext = createContext<AutocompleteContextType | undefined>(undefined);

export const AutocompleteProvider = ({ fetchOptions, skipConditions, children }: AutocompleteProviderProps) => {
Expand All @@ -39,8 +45,9 @@ export const AutocompleteProvider = ({ fetchOptions, skipConditions, children }:
if (skipConditions && skipConditions[key] && skipConditions[key](query)) {
return;
}
setOptions(key, searchingForItems);
const fetchedOptions = await fetchOptions(query, key);
setOptions(key, fetchedOptions);
setOptions(key, fetchedOptions.length ? fetchedOptions : noDataFound);
} catch (fetchError) {
setError("Error fetching options");
} finally {
Expand All @@ -49,8 +56,16 @@ export const AutocompleteProvider = ({ fetchOptions, skipConditions, children }:
}, 450);
};

const fetchAndSetOptions = (query: string, key: string) => {
setLoading(true);
const fetchAndSetOptions = async (query: string, key: string) => {
if(!key || !query) return;
if (skipConditions && skipConditions[key] && skipConditions[key](query)) {
return;
}
/*setLoading(true);
setOptions(key, searchingForItems);
const fetchedOptions = await fetchOptions(query, key);
setOptions(key, fetchedOptions.length ? fetchedOptions : noDataFound);
setLoading(false);*/
if (!debouncedFetchMap.current.has(key)) {
debouncedFetchMap.current.set(key, createDebouncedFetch(key));
}
Expand Down

0 comments on commit 499d120

Please sign in to comment.