import React, { useEffect, useState, type FC } from 'react'; import classNames from 'classnames'; import { useTranslation } from 'react-i18next'; import { DataTable, DataTableSkeleton, InlineLoading, Pagination, Table, TableBody, TableCell, TableContainer, TableExpandHeader, TableExpandRow, TableExpandedRow, TableHead, TableHeader, TableRow, TableToolbar, TableToolbarContent, Tile, } from '@carbon/react'; import { isDesktop, useLayoutType, usePagination } from '@openmrs/esm-framework'; import { type QueueEntry, type QueueTableColumn } from '../types'; import { useColumns } from './cells/columns.resource'; import styles from './queue-table.scss'; interface QueueTableProps { queueEntries: QueueEntry[]; isValidating?: boolean; // the queueUuid and statusUuid are used to determine the columns // to display based on the tablesConfig configuration. // For a table displaying entries of a particular queue (across all statuses) // statusUuid param should be null. // For a table displaying entries from multiple quueues // both queueUuid and statusUuid params should be null queueUuid: string; statusUuid: string; // If provided, overides the columns specified by the tablesConfig configuration. queueTableColumnsOverride?: QueueTableColumn[]; // if provided, a queue entry row can be expanded with the // provided component rendering more info about the row ExpandedRow?: FC<{ queueEntry: QueueEntry }>; // if provided, adds addition table toolbar elements tableFilters?: React.ReactNode; isLoading?: boolean; } function QueueTable({ queueEntries, isValidating, queueUuid, statusUuid, queueTableColumnsOverride, ExpandedRow, tableFilters, isLoading, }: QueueTableProps) { const { t } = useTranslation(); const [currentPageSize, setPageSize] = useState(10); const pageSizes = [10, 20, 30, 40, 50]; const { goTo, results: paginatedQueueEntries, currentPage, paginated } = usePagination(queueEntries, currentPageSize); const layout = useLayoutType(); const responsiveSize = isDesktop(layout) ? 'sm' : 'lg'; const columnsFromConfig = useColumns(queueUuid, statusUuid); const columns = queueTableColumnsOverride ?? columnsFromConfig ?? []; useEffect(() => { goTo(1); }, [goTo, queueEntries?.length]); const rows = paginatedQueueEntries?.map((queueEntry) => { const row: { id: string; [key: string]: JSX.Element | string } = { id: queueEntry.uuid }; columns.forEach(({ key, CellComponent }) => { row[key] = ; }); return row; }) ?? []; if (isLoading) { return ; } if (columns.length === 0) { return

{t('noColumnsDefined', 'No table columns defined. Check Configuration')}

; } return ( 1}> {({ rows, headers, getTableProps, getHeaderProps, getRowProps, getToolbarProps, getExpandHeaderProps }) => ( <>
{isValidating ? (
) : null} {tableFilters && ( {tableFilters} )}
{ExpandedRow && } {headers.map((header) => ( {header.header} ))} {rows.map((row, i) => { const Row = ExpandedRow ? TableExpandRow : TableRow; return ( {row.cells.map((cell, i) => ( {cell.value} ))} {ExpandedRow && row.isExpanded && paginatedQueueEntries[i] ? ( ) : ( )} ); })}
{rows.length === 0 && (

{t('noPatientsToDisplay', 'No patients to display')}

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

)} {paginated && ( { if (pageSize !== currentPageSize) { setPageSize(pageSize); } if (page !== currentPage) { goTo(page); } }} /> )} )}
); } export default QueueTable;