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

(fix) O3-2768: Display error in case there are no locations #906

Merged
merged 12 commits into from
Jun 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useLoginLocations } from '../login.resource';
import styles from './location-picker.scss';
import { useDefaultLocation } from './location-picker.resource';
import type { ConfigSchema } from '../config-schema';
import { Snackbar } from '@openmrs/esm-styleguide/src/snackbars/snackbar.component';
brandones marked this conversation as resolved.
Show resolved Hide resolved

interface LocationPickerProps {
hideWelcomeMessage?: boolean;
Expand All @@ -33,7 +34,6 @@ const LocationPicker: React.FC<LocationPickerProps> = ({ hideWelcomeMessage, cur
useDefaultLocation(isUpdateFlow);

const [searchTerm, setSearchTerm] = useState(null);

const { user, sessionLocation } = useSession();
const { currentUser, userProperties } = useMemo(
() => ({
Expand All @@ -50,6 +50,7 @@ const LocationPicker: React.FC<LocationPickerProps> = ({ hideWelcomeMessage, cur
hasMore,
loadingNewData,
setPage,
mutate,
} = useLoginLocations(chooseLocation.useLoginLocationTag, chooseLocation.locationsPerRequest, searchTerm);

const locations = useMemo(() => {
Expand Down Expand Up @@ -77,12 +78,14 @@ const LocationPicker: React.FC<LocationPickerProps> = ({ hideWelcomeMessage, cur

const changeLocation = useCallback(
(locationUuid?: string, saveUserPreference?: boolean) => {
setIsSubmitting(true);
if (!locationUuid) {
return;
}

const referrer = state?.referrer;
const returnToUrl = new URLSearchParams(location?.search).get('returnToUrl');

const sessionDefined = locationUuid ? setSessionLocation(locationUuid, new AbortController()) : Promise.resolve();
const sessionDefined = locationUuid ? setSessionLocation(locationUuid, new AbortController()) : Promise.reject();
brandones marked this conversation as resolved.
Show resolved Hide resolved

updateDefaultLocation(locationUuid, saveUserPreference);
sessionDefined.then(() => {
Expand All @@ -107,9 +110,6 @@ const LocationPicker: React.FC<LocationPickerProps> = ({ hideWelcomeMessage, cur
if (!config.chooseLocation.enabled || locations?.length === 1) {
changeLocation(locations[0]?.resource.id, false);
}
if (!locations?.length) {
changeLocation();
}
brandones marked this conversation as resolved.
Show resolved Hide resolved
}
}, [changeLocation, config.chooseLocation.enabled, isLoading, locations, searchTerm]);

Expand Down Expand Up @@ -178,15 +178,32 @@ const LocationPicker: React.FC<LocationPickerProps> = ({ hideWelcomeMessage, cur
)}
</p>
</div>
<Search
autoFocus
labelText={t('searchForLocation', 'Search for a location')}
id="search-1"
placeholder={t('searchForLocation', 'Search for a location')}
onChange={(event) => search(event.target.value)}
name="searchForLocation"
size="lg"
/>
{locations?.length > 0 ? (
<Search
autoFocus
labelText={t('searchForLocation', 'Search for a location')}
id="search-1"
placeholder={t('searchForLocation', 'Search for a location')}
onChange={(event) => search(event.target.value)}
name="searchForLocation"
size="lg"
/>
) : (
<Snackbar
snackbar={{
id: 1,
actionButtonLabel: t('reload', 'Reload'),
onActionButtonClick: () => mutate(),
kind: 'error',
title: t('locationsFailedToLoad', 'Locations failed to load'),
subtitle: t(
'locationIssueContactAdministrator',
'If the issue persists please contact your administrator',
),
}}
closeSnackbar={() => {}}
/>
brandones marked this conversation as resolved.
Show resolved Hide resolved
)}
<div className={styles.searchResults}>
{isLoading ? (
<div className={styles.loadingContainer}>
Expand Down
11 changes: 7 additions & 4 deletions packages/apps/esm-login-app/src/login.resource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import type { KeyedMutator } from 'swr';
import useSwrInfinite from 'swr/infinite';
import useSwrImmutable from 'swr/immutable';
import { type FetchResponse, fhirBaseUrl, openmrsFetch, useDebounce, showNotification } from '@openmrs/esm-framework';
Expand All @@ -12,6 +13,7 @@ interface LoginLocationData {
hasMore: boolean;
loadingNewData: boolean;
setPage: (size: number | ((_size: number) => number)) => Promise<FetchResponse<LocationResponse>[]>;
mutate: KeyedMutator<FetchResponse<LocationResponse>[]>;
}

export function useLoginLocations(
Expand Down Expand Up @@ -66,10 +68,10 @@ export function useLoginLocations(
return url + urlSearchParameters.toString();
}

const { data, isLoading, isValidating, setSize, error } = useSwrInfinite<FetchResponse<LocationResponse>, Error>(
constructUrl,
openmrsFetch,
);
const { data, isLoading, isValidating, setSize, error, mutate } = useSwrInfinite<
FetchResponse<LocationResponse>,
Error
>(constructUrl, openmrsFetch);

if (error) {
showNotification({
Expand All @@ -89,6 +91,7 @@ export function useLoginLocations(
loadingNewData: isValidating,
setPage: setSize,
error,
mutate,
};
}, [isLoading, data, isValidating, setSize]);

Expand Down