import React, { useEffect, useState } from 'react'; import { DataTable, DataTableSkeleton, Layer, Pagination, Table, TableBody, TableCell, TableContainer, TableExpandedRow, TableExpandHeader, TableExpandRow, TableHead, TableHeader, TableRow, Tile, } from '@carbon/react'; import { useTranslation } from 'react-i18next'; import { formatDatetime, parseDate, useConfig } from '@openmrs/esm-framework'; import { usePrescriptionsTable } from '../medication-request/medication-request.resource'; import { type PharmacyConfig } from '../config-schema'; import { type SimpleLocation } from '../types'; import PatientInfoCell from '../patient/patient-info-cell.component'; import PrescriptionExpanded from './prescription-expanded.component'; import styles from './prescriptions.scss'; interface PrescriptionsTableProps { loadData: boolean; debouncedSearchTerm: string; locations: SimpleLocation[]; status?: string; customPrescriptionsTableEndpoint?: string; } const PrescriptionsTable: React.FC = ({ loadData, debouncedSearchTerm, locations, status, customPrescriptionsTableEndpoint, }) => { const { t } = useTranslation(); const config = useConfig(); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(10); const nextOffSet = (page - 1) * pageSize; const { prescriptionsTableRows, error, isLoading, totalOrders } = usePrescriptionsTable( loadData, customPrescriptionsTableEndpoint, status, pageSize, nextOffSet, debouncedSearchTerm, locations, config.medicationRequestExpirationPeriodInDays, config.refreshInterval, ); // reset back to page 1 whenever search term changes useEffect(() => { setPage(1); }, [debouncedSearchTerm]); // dynamic status keys we need to maintain // t('active', 'Active') // t('paused', 'Paused') // t('closed', 'Closed') // t('completed', 'Completed') // t('expired', 'Expired') // t('cancelled', 'Cancelled') // t('dispensed', 'Dispensed') let columns = [ { header: t('created', 'Created'), key: 'created' }, { header: t('patientName', 'Patient name'), key: 'patient' }, { header: t('prescriber', 'Prescriber'), key: 'prescriber' }, { header: t('drugs', 'Drugs'), key: 'drugs' }, { header: t('lastDispenser', 'Last dispenser'), key: 'lastDispenser' }, { header: t('status', 'Status'), key: 'status' }, ]; // add the locations column, if enabled if (config.locationBehavior?.locationColumn?.enabled) { columns = [...columns.slice(0, 3), { header: t('location', 'Location'), key: 'location' }, ...columns.slice(3)]; } return (
{isLoading && } {error && (

{t('errorLoadingPrescriptions', 'Error loading prescriptions')}

{error?.message}

)} {prescriptionsTableRows && ( <> {({ rows, headers, getExpandHeaderProps, getHeaderProps, getRowProps, getTableProps }) => ( {headers.map((header) => ( {header.header} ))} {rows.map((row) => ( {row.cells.map((cell) => ( {cell.id.endsWith('created') ? ( formatDatetime(parseDate(cell.value)) ) : cell.id.endsWith('patient') ? ( ) : cell.id.endsWith('status') ? ( t(cell.value) ) : ( cell.value )} ))} {row.isExpanded ? ( {row.cells.find((cell) => cell.id.endsWith('patient'))?.value?.uuid && ( cell.id.endsWith('patient')).value.uuid} /> )} ) : ( )} ))}
)}
{prescriptionsTableRows?.length === 0 && (

{t('noPrescriptionsToDisplay', 'No prescriptions to display')}

{t('checkFilters', 'Check the filters above')}

)} {prescriptionsTableRows?.length > 0 && (
{ if (newPageSize !== pageSize) { setPage(1); } else { setPage(newPage); } setPageSize(newPageSize); }} />
)} )}
); }; export default PrescriptionsTable;