From 2f73d024a55be6df23bfc26d81ec92e4424972f9 Mon Sep 17 00:00:00 2001 From: Usama Idriss Kakumba Date: Wed, 16 Oct 2024 16:41:07 +0300 Subject: [PATCH] feat: add loading status for searching location --- .../src/patient-registration/field/field.scss | 10 +++++++ ...ation-person-attribute-field.component.tsx | 28 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/packages/esm-patient-registration-app/src/patient-registration/field/field.scss b/packages/esm-patient-registration-app/src/patient-registration/field/field.scss index 7f3595392..821fb6f8c 100644 --- a/packages/esm-patient-registration-app/src/patient-registration/field/field.scss +++ b/packages/esm-patient-registration-app/src/patient-registration/field/field.scss @@ -115,6 +115,16 @@ margin-bottom: layout.$spacing-05; } +.locationAttributeFieldContainer { + position: relative; + + .loadingContainer { + position: absolute; + right: -6rem; + bottom: 0; + } +} + :global(.omrs-breakpoint-lt-desktop) { .grid { grid-template-columns: 1fr; diff --git a/packages/esm-patient-registration-app/src/patient-registration/field/person-attributes/location-person-attribute-field.component.tsx b/packages/esm-patient-registration-app/src/patient-registration/field/person-attributes/location-person-attribute-field.component.tsx index c0da16bf0..6996b7a28 100644 --- a/packages/esm-patient-registration-app/src/patient-registration/field/person-attributes/location-person-attribute-field.component.tsx +++ b/packages/esm-patient-registration-app/src/patient-registration/field/person-attributes/location-person-attribute-field.component.tsx @@ -1,11 +1,12 @@ -import React, { useCallback, useMemo, useState } from 'react'; +import React, { useCallback, useMemo, useRef, useState } from 'react'; import classNames from 'classnames'; import { Field, useField } from 'formik'; import { type PersonAttributeTypeResponse } from '../../patient-registration.types'; import styles from './../field.scss'; import { useLocations } from './location-person-attribute-field.resource'; -import { ComboBox, Layer } from '@carbon/react'; +import { ComboBox, InlineLoading, Layer } from '@carbon/react'; import { useTranslation } from 'react-i18next'; +import { isDesktop, useLayoutType } from '@openmrs/esm-framework'; export interface LocationPersonAttributeFieldProps { id: string; @@ -23,14 +24,21 @@ export function LocationPersonAttributeField({ required, }: LocationPersonAttributeFieldProps) { const { t } = useTranslation(); + const isTablet = !isDesktop(useLayoutType()); const fieldName = `attributes.${personAttributeType.uuid}`; const [field, meta, { setValue }] = useField(`attributes.${personAttributeType.uuid}`); const [searchQuery, setSearchQuery] = useState(''); - const { locations } = useLocations(locationTag || null, searchQuery); + const { locations, isLoading, loadingNewData } = useLocations(locationTag || null, searchQuery); + const prevLocationOptions = useRef([]); const locationOptions = useMemo(() => { - return locations.map(({ resource: { id, name } }) => ({ value: id, label: name })); - }, [locations]); + if (!(isLoading && loadingNewData)) { + const newOptions = locations.map(({ resource: { id, name } }) => ({ value: id, label: name })); + prevLocationOptions.current = newOptions; + return newOptions; + } + return prevLocationOptions.current; + }, [locations, isLoading, loadingNewData]); const selectedItem = useMemo(() => { if (typeof meta.value === 'string') { @@ -66,7 +74,9 @@ export function LocationPersonAttributeField({ ); return ( -
+
+ {loadingNewData && isTablet && } {({ field, form: { touched, errors } }) => { @@ -82,11 +92,17 @@ export function LocationPersonAttributeField({ onChange={handleSelect} selectedItem={selectedItem} invalid={errors[fieldName] && touched[fieldName]} + typeahead /> ); }} + {loadingNewData && !isTablet && ( +
+ +
+ )}
); }