import { Button, ButtonSet, DataTable, DataTableSkeleton, Pagination, Search, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, TableSelectAll, TableSelectRow, } from '@carbon/react'; import { ArrowRight, Printer, TrashCan } from '@carbon/react/icons'; import { ErrorState, formatDate, parseDate, showModal, showSnackbar, usePagination } from '@openmrs/esm-framework'; import { CardHeader, EmptyState, usePaginationInfo } from '@openmrs/esm-patient-common-lib'; import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useLabManifest } from '../hooks'; import useLabManifestOrders, { LabManifestSample } from '../hooks/useLabManifestOrders'; import { mutateManifestLinks, printSpecimentLabel, removeSampleFromTheManifest, sampleRemovableManifestStatus, } from '../lab-manifest.resources'; import styles from './lab-manifest-table.scss'; import PatientCCCNumbercell from './patient-ccc-no-cell.component'; import PatientNameCell from './patient-name-cell.component'; interface LabManifestSamplesProps { manifestUuid: string; } const LabManifestSamples: React.FC = ({ manifestUuid }) => { const { error, isLoading, manifest } = useLabManifest(manifestUuid); const { labmanifestOrders, setSearchvalue, searchValue, isLoading: isLoadingLabOrders, error: labOrderErrors, } = useLabManifestOrders(manifestUuid); const { t } = useTranslation(); const [pageSize, setPageSize] = useState(10); const headerTitle = t('labManifestSamples', 'Lab Manifest Samples'); const { results, totalPages, currentPage, goTo } = usePagination(labmanifestOrders, pageSize); const { pageSizes } = usePaginationInfo(pageSize, totalPages, currentPage, results.length); const headers = [ { header: t('patient', 'Patient'), key: 'patientName', }, { header: t('cccKDODNumber', 'CCC/KDOD Number'), key: 'cccKDODNumber', }, { header: t('batchNumber', 'Batch Number'), key: 'batchNumber', }, { header: t('sampleType', 'Sample type'), key: 'sampleType', }, { header: t('status', 'Status'), key: 'status', }, { header: t('dateRequested', 'Date Requested'), key: 'dateRequested', isSortable: true, }, { header: t('resultDate', 'Result Date'), key: 'resultDate', isSortable: true, }, { header: t('result', 'Result'), key: 'result', }, { header: t('actions', 'Actions'), key: 'actions', }, ]; const handleDeleteManifestSample = (sampleUUid: string) => { const dispose = showModal('sample-delete-confirm-dialog', { onClose: () => dispose(), samples: labmanifestOrders.filter((s) => s.uuid === sampleUUid), onDelete: async () => { try { await removeSampleFromTheManifest(sampleUUid); mutateManifestLinks(manifest?.uuid, manifest?.manifestStatus); dispose(); showSnackbar({ title: 'Success', kind: 'success', subtitle: t('sampleRemoveSuccess', 'Sample removed from manifest successfully!'), }); } catch (e: any) { const _sample = labmanifestOrders.find((sample) => sample.uuid === sampleUUid); showSnackbar({ title: t('errorRemovingSample', 'Error removing sample {{sample}} from the manifest', { sample: _sample.id ?? _sample?.uuid, }), kind: 'error', subtitle: `${e?.responseBody?.error?.message ?? e?.message} `, }); } }, }); }; const handleDeleteSelectedSamples = (selected: Array) => { if (selected.length > 0) { const dispose = showModal('sample-delete-confirm-dialog', { onClose: () => dispose(), samples: selected, onDelete: async () => { try { const samplesDeletionAsyncTasks = await Promise.allSettled( selected.map(({ uuid }) => removeSampleFromTheManifest(uuid)), ); mutateManifestLinks(manifest?.uuid, manifest?.manifestStatus); dispose(); samplesDeletionAsyncTasks.forEach((task, index) => { const _sample = selected[index]; if (task.status === 'rejected') { showSnackbar({ title: t('errorRemovingSample', 'Error removing sample {{sample}} from the manifest', { sample: _sample.id ?? _sample.uuid, }), kind: 'error', subtitle: `${task.reason?.responseBody?.error?.message ?? task.reason?.message} `, }); } else { showSnackbar({ title: 'Success', kind: 'success', subtitle: 'Sample removed from manifest successfully!', }); } }); } catch (e) { showSnackbar({ title: 'Failure', kind: 'error', subtitle: 'Error removing sample from the manifest' }); } }, }); } }; async function handlePrintSpecimenLabel(sample: LabManifestSample) { try { await printSpecimentLabel(sample.uuid); } catch (error) { showSnackbar({ title: 'Failure', subtitle: 'Error specimen label', kind: 'error' }); } } const tableRows = results?.map((sample) => { return { id: `${sample.uuid}`, sampleType: sample.sampleType ?? '--', status: sample.status, batchNumber: sample.batchNumber ?? '--', patientName: , cccKDODNumber: sample?.order?.patient ? ( ) : ( '--' ), dateRequested: sample.dateSent ? formatDate(parseDate(sample.dateSent)) : '--', resultDate: sample.resultDate ? formatDate(parseDate(sample.resultDate)) : '--', result: sample.result ?? '--', actions: ( {sampleRemovableManifestStatus.includes(manifest?.manifestStatus) && ( setSearchvalue(value)} /> {headers.map((header, i) => ( {header.header} ))} {rows.map((row, i) => ( {}}> {row.cells.map((cell) => ( {cell.value} ))} ))}
)} /> { goTo(page); setPageSize(pageSize); }} /> ); }; export default LabManifestSamples;