-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathusePersonAttributes.tsx
More file actions
31 lines (28 loc) · 1009 Bytes
/
usePersonAttributes.tsx
File metadata and controls
31 lines (28 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { openmrsFetch, type PersonAttribute, restBaseUrl } from '@openmrs/esm-framework';
import { useEffect, useState } from 'react';
import { type FormSchema } from '../types';
export const usePersonAttributes = (patientUuid: string, formJson: FormSchema) => {
const [personAttributes, setPersonAttributes] = useState<Array<PersonAttribute>>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
if (formJson.meta?.personAttributes?.hasPersonAttributeFields && patientUuid) {
openmrsFetch(`${restBaseUrl}/patient/${patientUuid}?v=custom:(attributes)`)
.then((response) => {
setPersonAttributes(response.data?.attributes);
setIsLoading(false);
})
.catch((error) => {
setError(error);
setIsLoading(false);
});
} else {
setIsLoading(false);
}
}, [patientUuid]);
return {
personAttributes,
error,
isLoading: isLoading,
};
};