Skip to content

Commit

Permalink
Fixed the search term and loading flows
Browse files Browse the repository at this point in the history
  • Loading branch information
vasharma05 committed Dec 14, 2023
1 parent 17fb6b8 commit edcf383
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ const LocationPicker: React.FC<LocationPickerProps> = () => {
const [searchParams] = useSearchParams();

const isUpdateFlow = useMemo(() => searchParams.get('update') === 'true', [searchParams]);
const { userDefaultLocationUuid, updateDefaultLocation, savePreference, setSavePreference, defaultLocationFhir } =
useDefaultLocation(isUpdateFlow, debouncedSearchQuery);
const {
isLoadingValidation,
userDefaultLocationUuid,
updateDefaultLocation,
savePreference,
setSavePreference,
defaultLocationFhir,
} = useDefaultLocation(isUpdateFlow, debouncedSearchQuery);

const { user, sessionLocation } = useSession();
const { currentUser } = useMemo(
Expand All @@ -56,6 +62,8 @@ const LocationPicker: React.FC<LocationPickerProps> = () => {
setPage,
} = useLoginLocations(chooseLocation.useLoginLocationTag, chooseLocation.locationsPerRequest, debouncedSearchQuery);

const isLoadingLocations = isLoading || isLoadingValidation;

const locations = useMemo(() => {
if (!defaultLocationFhir?.length || !fetchedLocations) {
return fetchedLocations;
Expand Down Expand Up @@ -105,21 +113,22 @@ const LocationPicker: React.FC<LocationPickerProps> = () => {

// Handle cases where the location picker is disabled, there is only one location, or there are no locations.
useEffect(() => {
if (!isLoading && !searchTerm) {
if (!isLoadingLocations && !debouncedSearchQuery) {
if (!config.chooseLocation.enabled || locations?.length === 1) {
changeLocation(locations[0]?.resource.id, false);
}
if (!locations?.length) {
changeLocation();
}
}
}, [changeLocation, config.chooseLocation.enabled, isLoading, locations, searchTerm]);
}, [changeLocation, config.chooseLocation.enabled, isLoadingLocations, locations, debouncedSearchQuery]);

// Handle cases where the login location is present in the userProperties.
useEffect(() => {
if (isUpdateFlow) {
return;
}

if (userDefaultLocationUuid && !isSubmitting) {
setActiveLocation(userDefaultLocationUuid);
changeLocation(userDefaultLocationUuid, true);
Expand Down Expand Up @@ -178,7 +187,7 @@ const LocationPicker: React.FC<LocationPickerProps> = () => {
value={searchTerm}
/>
<div className={styles.searchResults}>
{isLoading ? (
{isLoadingLocations ? (
<div className={styles.loadingContainer}>
<RadioButtonSkeleton className={styles.radioButtonSkeleton} role="progressbar" />
<RadioButtonSkeleton className={styles.radioButtonSkeleton} role="progressbar" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ export function useDefaultLocation(isUpdateFlow: boolean, searchTerm: string) {

const userDefaultLocationUuid = useMemo(() => userProperties?.defaultLocation, [userProperties?.defaultLocation]);

const { isLocationValid, location: defaultLocationFhir } = useValidateLocationUuid(
userDefaultLocationUuid,
searchTerm,
);
const {
isLoading: isLoadingValidation,
isLocationValid,
location: defaultLocationFhir,
} = useValidateLocationUuid(userDefaultLocationUuid, searchTerm);

useEffect(() => {
if (userDefaultLocationUuid) {
Expand Down Expand Up @@ -218,6 +219,7 @@ export function useDefaultLocation(isUpdateFlow: boolean, searchTerm: string) {
);

return {
isLoadingValidation,
defaultLocationFhir,
userDefaultLocationUuid: isLocationValid ? userDefaultLocationUuid : null,
updateDefaultLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,21 +351,22 @@ describe('LocationPicker', () => {

const radios = screen.getAllByRole('radio');
expect(radios[0].getAttribute('id')).toBe('Community Outreach');
expect(radios[0]).toHaveAttribute('checked');
// @ts-ignore
expect(radios[0].checked).toBe(true);
});

// it("should not show default location if the searched term doesn't matches the default location", async () => {
// const user = userEvent.setup();
// await act(() => {
// renderWithRouter(LocationPicker, {});
// });
it("should not show default location if the searched term doesn't matches the default location", async () => {
const user = userEvent.setup();
await act(() => {
renderWithRouter(LocationPicker, {});
});

// const searchBox = screen.getByRole('searchbox', {
// name: /search for a location/i,
// });
// await user.type(searchBox, 'site');
// expect(searchBox.getAttribute('value')).toBe('site');
const searchBox = screen.getByRole('searchbox', {
name: /search for a location/i,
});
await user.type(searchBox, 'site');
expect(searchBox.getAttribute('value')).toBe('site');

// expect(screen.queryByRole('radio', { name: new RegExp(/community outreach/, 'i') })).not.toBeInTheDocument();
// });
expect(screen.queryByRole('radio', { name: new RegExp(/community outreach/, 'i') })).not.toBeInTheDocument();
});
});

0 comments on commit edcf383

Please sign in to comment.