import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import classNames from 'classnames'; import { DataTable, type DataTableCell, type DataTableSortState, Table, TableCell, TableContainer, TableBody, TableHead, TableHeader, TableRow, TableExpandHeader, TableExpandRow, TableExpandedRow, } from '@carbon/react'; import { orderBy } from 'lodash-es'; import { formatDate, formatTime, parseDate, useLayoutType, usePagination } from '@openmrs/esm-framework'; import { PatientChartPagination } from '@openmrs/esm-patient-common-lib'; import type { PatientNote } from '../types'; import styles from './notes-overview.scss'; interface PaginatedNotes { notes: Array; pageSize: number; pageUrl: string; urlLabel: string; } const PaginatedNotes: React.FC = ({ notes, pageSize, pageUrl, urlLabel }) => { const { t } = useTranslation(); const layout = useLayoutType(); const isTablet = layout === 'tablet'; const [sortParams, setSortParams] = useState({ key: '', order: 'none' }); const tableHeaders = [ { key: 'encounterDate', header: t('date', 'Date'), }, { key: 'diagnoses', header: t('diagnoses', 'Diagnoses'), }, ]; const sortDate = (myArray, order) => order === 'ASC' ? orderBy(myArray, [(obj) => new Date(obj.encounterDate).getTime()], ['desc']) : orderBy(myArray, [(obj) => new Date(obj.encounterDate).getTime()], ['asc']); const { key, order } = sortParams; const sortedData = key === 'encounterDate' ? sortDate(notes, order) : order === 'DESC' ? orderBy(notes, [key], ['desc']) : orderBy(notes, [key], ['asc']); function customSortRow( cellA, cellB, { sortDirection, sortStates, }: { sortDirection: string; sortStates: any; locale: string; }, ) { const key = Object.keys(sortStates).find((k) => sortStates[k] === sortDirection); setSortParams({ key: key ?? '', order: sortDirection }); return 0; } const { results: paginatedNotes, goTo, currentPage } = usePagination(sortedData, pageSize); const tableRows = React.useMemo( () => paginatedNotes?.map((note) => ({ ...note, id: `${note.id}`, encounterDate: formatDate(parseDate(note.encounterDate), { mode: 'wide' }), diagnoses: note.diagnoses ? note.diagnoses : '--', })), [paginatedNotes], ); return ( <> {({ getExpandedRowProps, getExpandHeaderProps, getHeaderProps, getRowProps, getTableContainerProps, getTableProps, headers, rows, }) => ( {headers.map((header, i) => ( {header.header} ))} {rows.map((row, i) => ( {row.cells.map((cell) => ( {cell.value} ))} {row.isExpanded ? (
{tableRows?.[i]?.encounterNote ? (
{tableRows?.[i]?.encounterNote} {formatTime(new Date(tableRows?.[i]?.encounterNoteRecordedAt))} ·{' '} {tableRows?.[i]?.encounterProvider}, {tableRows?.[i]?.encounterProviderRole}
) : ( {t('noVisitNoteToDisplay', 'No visit note to display')} )}
) : ( )}
))}
)}
goTo(page)} dashboardLinkUrl={pageUrl} dashboardLinkLabel={urlLabel} /> ); }; export default PaginatedNotes;