/** @module @category UI */
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { InlineLoading } from '@carbon/react';
import { type CoreTranslationKey, getCoreTranslation } from '@openmrs/esm-translations';
import { ConfigurableLink, usePatient } from '@openmrs/esm-react-utils';
import { parseDate } from '@openmrs/esm-utils';
import { usePatientContactAttributes } from './usePatientAttributes';
import { usePatientListsForPatient } from './usePatientListsForPatient';
import { useRelationships } from './useRelationships';
import styles from './patient-banner-contact-details.module.scss';
interface ContactDetailsProps {
patientId: string;
deceased: boolean;
}
const PatientLists: React.FC<{ patientUuid: string }> = ({ patientUuid }) => {
const { cohorts = [], isLoading } = usePatientListsForPatient(patientUuid);
return (
<>
{getCoreTranslation('patientLists', 'Patient Lists')} ({cohorts?.length ?? 0})
{isLoading ? (
) : (
{(() => {
if (cohorts?.length > 0) {
const sortedLists = cohorts.sort(
(a, b) => parseDate(a?.startDate).getTime() - parseDate(b?.startDate).getTime(),
);
const slicedLists = sortedLists.slice(0, 3);
return slicedLists?.map((cohort) => (
-
{cohort.name}
));
}
return - --
;
})()}
{cohorts.length > 3 && (
-
{getCoreTranslation('seeMoreLists', 'See {{count}} more lists', {
count: cohorts?.length - 3,
})}
)}
)}
>
);
};
const Address: React.FC<{ patientId: string }> = ({ patientId }) => {
const { patient, isLoading } = usePatient(patientId);
const address = patient?.address?.find((a) => a.use === 'home');
const getAddressKey = (url: string) => url.split('#')[1];
if (isLoading) {
return ;
}
return (
<>
{getCoreTranslation('address', 'Address')}
{address ? (
Object.entries(address)
.filter(([key]) => key !== 'id' && key !== 'use')
.map(([key, value]) =>
key === 'extension' ? (
address.extension?.[0]?.extension?.map((add, i) => (
-
{getCoreTranslation(
getAddressKey(add.url) as CoreTranslationKey,
getAddressKey(add.url) as CoreTranslationKey,
)}
: {add.valueString}
))
) : (
-
{getCoreTranslation(key as CoreTranslationKey, key)}: {value}
),
)
) : (
- --
)}
>
);
};
const Contact: React.FC<{ patientUuid: string; deceased?: boolean }> = ({ patientUuid }) => {
const { isLoading: isLoadingAttributes, contactAttributes } = usePatientContactAttributes(patientUuid);
const contacts = useMemo(
() =>
contactAttributes
? [
...contactAttributes?.map((contact) => [
contact.attributeType.display
? getCoreTranslation(
/** TODO: We should probably add translation strings for some of these */
contact.attributeType.display as CoreTranslationKey,
contact.attributeType.display,
)
: '',
contact.value,
]),
]
: [],
[contactAttributes],
);
return (
<>
{getCoreTranslation('contactDetails', 'Contact Details')}
{isLoadingAttributes ? (
) : (
{contacts.length ? (
contacts.map(([label, value], index) => (
-
{label}: {value}
))
) : (
- --
)}
)}
>
);
};
const Relationships: React.FC<{ patientId: string }> = ({ patientId }) => {
const { data: relationships, isLoading } = useRelationships(patientId);
return (
<>
{getCoreTranslation('relationships', 'Relationships')}
{isLoading ? (
) : (
)}
>
);
};
export function PatientBannerContactDetails({ patientId, deceased }: ContactDetailsProps) {
return (
);
}