import { DataTable, DataTableSkeleton, OverflowMenu, OverflowMenuItem, Pagination, Search, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, } from '@carbon/react'; import { formatDatetime, launchWorkspace, parseDate, useAppContext, useConfig, usePagination, } from '@openmrs/esm-framework'; import { usePaginationInfo } from '@openmrs/esm-patient-common-lib'; import dayjs from 'dayjs'; import React, { useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { type WardConfigObject } from '../config-schema'; import { type WardPatient, type WardViewContext } from '../types'; import { bedLayoutToBed, getOpenmrsId } from '../ward-view/ward-view.resource'; import { HyperLinkPatientCell } from './patient-cells'; import { EmptyState } from './table-state-components'; const AdmittedPatients = () => { const [search, setSearch] = useState(''); const { wardPatientGroupDetails } = useAppContext('ward-view-context') ?? {}; const { bedLayouts, wardAdmittedPatientsWithBed, isLoading } = wardPatientGroupDetails ?? {}; const { t } = useTranslation(); const config = useConfig(); const headers = [ { key: 'admissionDate', header: t('admissionDate', 'Admission Date') }, { key: 'idNumber', header: t('idNumber', 'ID Number') }, { key: 'name', header: t('name', 'Name') }, { key: 'gender', header: t('gender', 'Gender') }, { key: 'age', header: t('age', 'Age') }, { key: 'bedNumber', header: t('bedNumber', 'Bed Number') }, { key: 'daysAdmitted', header: t('daysInWard', 'Days In Ward') }, { key: 'action', header: t('action', 'Action') }, ]; const patients = useMemo(() => { return ( bedLayouts ?.map((bedLayout) => { const { patients } = bedLayout; const bed = bedLayoutToBed(bedLayout); const wardPatients: WardPatient[] = patients.map((patient): WardPatient => { const inpatientAdmission = wardAdmittedPatientsWithBed?.get(patient.uuid); if (inpatientAdmission) { const { patient, visit, currentInpatientRequest } = inpatientAdmission; return { patient, visit, bed, inpatientAdmission, inpatientRequest: currentInpatientRequest || null }; } else { // for some reason this patient is in a bed but not in the list of admitted patients, so we need to use the patient data from the bed endpoint return { patient: patient, visit: null, bed, inpatientAdmission: null, // populate after BED-13 inpatientRequest: null, }; } }); return wardPatients; }) ?.flat() ?? [] ).filter((pat) => { const ipdDischargeEncounter = pat?.visit?.encounters?.find( (encounter) => encounter.encounterType?.uuid === config.ipdDischargeEncounterTypeUuid, ); if (!ipdDischargeEncounter) return true; return false; }); }, [bedLayouts, wardAdmittedPatientsWithBed, config]); const [pageSize, setPageSize] = useState(5); const searchResults = useMemo(() => { return patients?.filter((pat) => pat?.patient?.person?.display?.toLowerCase().includes(search.toLowerCase())); }, [patients, search]); const { paginated, results, totalPages, currentPage, goTo } = usePagination(searchResults, pageSize); const { pageSizes } = usePaginationInfo(pageSize, totalPages, currentPage, results.length); const tableRows = useMemo(() => { return results.map((patient, index) => { const { encounterAssigningToCurrentInpatientLocation } = patient.inpatientAdmission ?? {}; const admissionDate = encounterAssigningToCurrentInpatientLocation?.encounterDatetime ? formatDatetime(parseDate(encounterAssigningToCurrentInpatientLocation!.encounterDatetime!)) : '--'; const encounterDate = encounterAssigningToCurrentInpatientLocation?.encounterDatetime; const daysAdmitted = encounterDate && dayjs(encounterDate).isValid() ? Math.abs(dayjs().startOf('day').diff(dayjs(encounterDate).startOf('day'), 'days')) : '--'; return { id: patient.patient?.uuid ?? index, admissionDate, idNumber: getOpenmrsId(patient.patient?.identifiers ?? []) ?? '--', name: ( ), gender: patient.patient?.person?.gender ?? '--', age: patient.patient?.person?.age ?? '--', bedNumber: patient.bed?.bedNumber ?? '--', daysAdmitted, action: ( launchWorkspace('patient-transfer-swap-workspace', { workspaceTitle: 'Trasfer', wardPatient: patient, withContentSwitcher: false, defaultTransfersection: 'transfer', }) } /> launchWorkspace('patient-transfer-swap-workspace', { workspaceTitle: 'Bed Swap', wardPatient: patient, withContentSwitcher: false, defaultTransfersection: 'bed-swap', }) } /> { launchWorkspace('patient-discharge-workspace', { wardPatient: patient, patientUuid: patient.patient.uuid, formUuid: config.inpatientDischargeFormUuid, dischargePatientOnSuccesfullSubmission: false, }); }} /> ), }; }); }, [results, config, t]); if (isLoading) return ; if (!patients.length) return ; return (
setSearch(e.target.value)} /> {({ rows, headers, getHeaderProps, getRowProps, getTableProps, getCellProps }) => ( {headers.map((header) => ( {header.header} ))} {rows.map((row) => { return ( {row.cells.map((cell) => ( {cell.value} ))} ); })}
{paginated && !isLoading && ( { goTo(page); setPageSize(pageSize); }} /> )}
)}
); }; export default AdmittedPatients;