import React, { useMemo, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, DataTable, ModalBody, ModalFooter, ModalHeader, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, Tile, } from '@carbon/react'; import { useReactToPrint } from 'react-to-print'; import dayjs from 'dayjs'; import isBetween from 'dayjs/plugin/isBetween'; dayjs.extend(isBetween); import { age, getPatientName, formatDate, useConfig, useLayoutType, useSession, ResponsiveWrapper, getCoreTranslation, OpenmrsDatePicker, } from '@openmrs/esm-framework'; import usePanelData from '../individual-results-table-tablet/usePanelData'; import styles from './print-modal.scss'; function PrintModal({ patientUuid, patient, closeDialog, }: { patientUuid: string; patient: fhir.Patient; closeDialog: () => void; }) { const { t } = useTranslation(); const isTablet = useLayoutType() === 'tablet'; const session = useSession(); const { panels } = usePanelData(patientUuid); const printContainerRef = useRef(null); const [selectedFromDate, setSelectedFromDate] = useState(null); const [selectedToDate, setSelectedToDate] = useState(null); const { excludePatientIdentifierCodeTypes } = useConfig<{ excludePatientIdentifierCodeTypes: { uuids: string[]; }; }>({ externalModuleName: '@openmrs/esm-patient-banner-app', }); const headerTitle = t('testResults_title', 'Test Results'); const tableHeaders = [ { key: 'testType', header: 'Test Type' }, { key: 'date', header: 'Date' }, { key: 'result', header: 'Result' }, { key: 'normalRange', header: 'Normal Range' }, ]; const handlePrint = useReactToPrint({ content: () => printContainerRef.current, }); const patientDetails = useMemo(() => { const getGender = (gender: string): string => { switch (gender) { case 'male': return getCoreTranslation('male'); case 'female': return getCoreTranslation('female'); case 'other': return getCoreTranslation('other'); case 'unknown': return getCoreTranslation('unknown'); default: return gender; } }; const identifiers = patient?.identifier?.filter( (identifier) => !excludePatientIdentifierCodeTypes?.uuids.includes(identifier.type?.coding?.[0]?.code), ) ?? []; return { name: patient ? getPatientName(patient) : '', age: age(patient?.birthDate), gender: getGender(patient?.gender), location: patient?.address?.[0].city, identifiers: identifiers?.length ? identifiers.map(({ value }) => value) : [], }; }, [patient, excludePatientIdentifierCodeTypes?.uuids]); const testResults = useMemo(() => { if (selectedFromDate && selectedToDate) { const fromDate = dayjs(selectedFromDate).startOf('day'); const toDate = dayjs(selectedToDate).startOf('day'); return panels .filter((panel) => { const panelDate = dayjs(panel.effectiveDateTime).startOf('day'); return panelDate.isBetween(fromDate, toDate, null, '[]'); }) .map((panel) => formatPanelForDisplay(panel)); } return []; }, [panels, selectedFromDate, selectedToDate]); return ( <> {/* OpenMRS Logo */} {patientDetails?.name} {patientDetails?.gender}, {patientDetails?.age}, {patientDetails?.identifiers}{' '} {t('printedBy', 'Printed by')} {session.user.display} {t('onDate', 'on')} {formatDate(new Date(), { noToday: true })} {headerTitle} {testResults?.length > 0 && ( {({ rows, headers, getHeaderProps, getTableProps }) => ( {headers.map((header) => ( {header.header} ))} {rows.map((row) => ( {row.cells.map((cell) => ( {cell.value?.content ?? cell.value} ))} ))} )} )} {testResults.length === 0 && ( {t('noTestResultsWithinSpecifiedRange', 'No test results found within the specified range')} {t('checkFilters', 'Check the filters above')} )} {t('cancel', 'Cancel')} {t('print', 'Print')} > ); } const formatPanelForDisplay = (panel) => { return { id: panel.id, testType: panel.name, date: formatDate(new Date(panel.effectiveDateTime)), result: panel.value, normalRange: panel?.meta?.range ?? '--', }; }; export default PrintModal;
{t('noTestResultsWithinSpecifiedRange', 'No test results found within the specified range')}
{t('checkFilters', 'Check the filters above')}