-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat) O3-3276: Add support for printing patient identification stickers #1847
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
packages/esm-patient-banner-app/src/banner-tags/print-identifier-sticker-modal.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { useTranslation, type TFunction } from 'react-i18next'; | ||
import { useReactToPrint } from 'react-to-print'; | ||
import { Button, InlineLoading, ModalBody, ModalFooter, ModalHeader } from '@carbon/react'; | ||
import { age, displayName, showSnackbar, useConfig } from '@openmrs/esm-framework'; | ||
import { type ConfigObject } from '../config-schema'; | ||
import styles from './print-identifier-sticker-modal.scss'; | ||
|
||
interface PrintIdentifierStickerProps { | ||
closeModal: () => void; | ||
patient: fhir.Patient; | ||
} | ||
|
||
interface PrintComponentProps extends Partial<ConfigObject> { | ||
patientDetails: { | ||
address?: fhir.Address[]; | ||
age?: string; | ||
dateOfBirth?: string; | ||
gender?: string; | ||
id?: string; | ||
identifiers?: fhir.Identifier[]; | ||
name?: string; | ||
photo?: fhir.Attachment[]; | ||
}; | ||
printRef: React.RefObject<HTMLDivElement>; | ||
t: TFunction; | ||
} | ||
|
||
const PrintIdentifierSticker: React.FC<PrintIdentifierStickerProps> = ({ closeModal, patient }) => { | ||
const { t } = useTranslation(); | ||
const { printIdentifierStickerFields, printIdentifierStickerSize, excludePatientIdentifierCodeTypes } = | ||
useConfig<ConfigObject>(); | ||
const contentToPrintRef = useRef(null); | ||
const onBeforeGetContentResolve = useRef<() => void | null>(null); | ||
const [isPrinting, setIsPrinting] = useState(false); | ||
const headerTitle = t('patientIdentifierSticker', 'Patient identifier sticker'); | ||
|
||
useEffect(() => { | ||
if (isPrinting && onBeforeGetContentResolve.current) { | ||
onBeforeGetContentResolve.current(); | ||
} | ||
}, [isPrinting, printIdentifierStickerSize]); | ||
|
||
const patientDetails = useMemo(() => { | ||
if (!patient) { | ||
return {}; | ||
} | ||
|
||
const getGender = (gender: string): string => { | ||
switch (gender) { | ||
case 'male': | ||
return t('male', 'Male'); | ||
case 'female': | ||
return t('female', 'Female'); | ||
case 'other': | ||
return t('other', 'Other'); | ||
case 'unknown': | ||
return t('unknown', 'Unknown'); | ||
default: | ||
return gender; | ||
} | ||
}; | ||
|
||
const identifiers = | ||
patient.identifier?.filter( | ||
(identifier) => !excludePatientIdentifierCodeTypes?.uuids.includes(identifier.type.coding[0].code), | ||
) ?? []; | ||
|
||
return { | ||
address: patient.address, | ||
age: age(patient.birthDate), | ||
dateOfBirth: patient.birthDate, | ||
gender: getGender(patient.gender), | ||
id: patient.id, | ||
identifiers: [...identifiers], | ||
name: patient ? displayName(patient) : '', | ||
photo: patient.photo, | ||
}; | ||
}, [excludePatientIdentifierCodeTypes?.uuids, patient, t]); | ||
|
||
const handleBeforeGetContent = useCallback( | ||
() => | ||
new Promise<void>((resolve) => { | ||
if (patient && headerTitle) { | ||
onBeforeGetContentResolve.current = resolve; | ||
setIsPrinting(true); | ||
} | ||
}), | ||
[headerTitle, patient], | ||
); | ||
|
||
const handleAfterPrint = useCallback(() => { | ||
onBeforeGetContentResolve.current = null; | ||
setIsPrinting(false); | ||
closeModal(); | ||
}, [closeModal]); | ||
|
||
const handlePrintError = useCallback( | ||
(errorLocation, error) => { | ||
onBeforeGetContentResolve.current = null; | ||
|
||
showSnackbar({ | ||
isLowContrast: false, | ||
kind: 'error', | ||
title: t('printError', 'Print error'), | ||
subtitle: t('printErrorExplainer', 'An error occurred in "{{errorLocation}}": ', { errorLocation }) + error, | ||
}); | ||
|
||
setIsPrinting(false); | ||
}, | ||
[t], | ||
); | ||
|
||
const handlePrint = useReactToPrint({ | ||
content: () => contentToPrintRef.current, | ||
documentTitle: `${patientDetails.name} - ${headerTitle}`, | ||
onAfterPrint: handleAfterPrint, | ||
onBeforeGetContent: handleBeforeGetContent, | ||
onPrintError: handlePrintError, | ||
}); | ||
|
||
return ( | ||
<> | ||
<ModalHeader closeModal={closeModal} title={t('printIdentifierSticker', 'Print identifier sticker')} /> | ||
<ModalBody> | ||
<PrintComponent | ||
patientDetails={patientDetails} | ||
printIdentifierStickerFields={printIdentifierStickerFields} | ||
printIdentifierStickerSize={printIdentifierStickerSize} | ||
printRef={contentToPrintRef} | ||
t={t} | ||
/> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button kind="secondary" onClick={closeModal}> | ||
{t('cancel', 'Cancel')} | ||
</Button> | ||
<Button className={styles.button} disabled={isPrinting} onClick={handlePrint} kind="primary"> | ||
{isPrinting ? ( | ||
<InlineLoading className={styles.loader} description={t('printing', 'Printing') + '...'} /> | ||
) : ( | ||
t('print', 'Print') | ||
)} | ||
</Button> | ||
</ModalFooter> | ||
</> | ||
); | ||
}; | ||
|
||
const PrintComponent = ({ | ||
patientDetails, | ||
printIdentifierStickerFields, | ||
printIdentifierStickerSize, | ||
printRef, | ||
t, | ||
}: PrintComponentProps) => { | ||
return ( | ||
<div className={styles.stickerContainer} ref={printRef}> | ||
<style type="text/css" media="print"> | ||
{` | ||
@page { | ||
size: ${printIdentifierStickerSize}; | ||
} | ||
`} | ||
</style> | ||
{printIdentifierStickerFields.includes('name') && <div className={styles.patientName}>{patientDetails.name}</div>} | ||
<div className={styles.detailsGrid}> | ||
{patientDetails.identifiers.map((identifier) => { | ||
return ( | ||
<p key={identifier?.id}> | ||
{identifier?.type?.text}: <strong>{identifier?.value}</strong> | ||
</p> | ||
); | ||
})} | ||
<p> | ||
{t('sex', 'Sex')}: <strong>{patientDetails.gender}</strong> | ||
</p> | ||
<p> | ||
{t('dob', 'DOB')}: <strong>{patientDetails.dateOfBirth}</strong> | ||
</p> | ||
<p> | ||
{t('age', 'Age')}: <strong>{patientDetails.age}</strong> | ||
</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default PrintIdentifierSticker; |
50 changes: 50 additions & 0 deletions
50
packages/esm-patient-banner-app/src/banner-tags/print-identifier-sticker-modal.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
@use '@carbon/layout'; | ||
@use '@carbon/type'; | ||
|
||
.stickerContainer { | ||
padding: 0.5rem 1rem; | ||
} | ||
|
||
.row { | ||
margin: layout.$spacing-03 0rem 0rem; | ||
display: flex; | ||
flex-flow: row wrap; | ||
gap: layout.$spacing-05; | ||
} | ||
|
||
.gridRow { | ||
padding: 0px; | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
|
||
div { | ||
margin-left: 0px; | ||
margin-bottom: 5px; | ||
} | ||
} | ||
|
||
.patientName { | ||
font-size: 1.5rem; | ||
font-weight: bolder; | ||
margin-bottom: 5px; | ||
} | ||
|
||
.button { | ||
:global(.cds--inline-loading) { | ||
min-height: 1rem !important; | ||
} | ||
} | ||
|
||
.detailsGrid { | ||
display: grid; | ||
grid-template-columns: repeat(2, minmax(0, 1fr)); | ||
gap: 0.5rem 0; | ||
margin-top: 0.5rem; | ||
} | ||
|
||
.loader { | ||
:global(.cds--inline-loading__text) { | ||
@include type.type-style('body-01'); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/esm-patient-banner-app/src/banner-tags/print-identifier-sticker-modal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { useReactToPrint } from 'react-to-print'; | ||
import { mockPatient } from 'tools'; | ||
import PrintIdentifierSticker from './print-identifier-sticker-modal.component'; | ||
|
||
const mockedCloseModal = jest.fn(); | ||
const mockedUseReactToPrint = jest.mocked(useReactToPrint); | ||
|
||
jest.mock('react-to-print', () => { | ||
const originalModule = jest.requireActual('react-to-print'); | ||
|
||
return { | ||
...originalModule, | ||
useReactToPrint: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest.mock('@openmrs/esm-framework', () => { | ||
const originalModule = jest.requireActual('@openmrs/esm-framework'); | ||
|
||
return { | ||
...originalModule, | ||
useConfig: jest.fn().mockImplementation(() => ({ | ||
printIdentifierStickerFields: ['name', 'identifier', 'age', 'dateOfBirth', 'gender'], | ||
})), | ||
}; | ||
}); | ||
|
||
describe('PrintIdentifierSticker', () => { | ||
test('renders the component', () => { | ||
renderPrintIdentifierSticker(); | ||
|
||
expect(screen.getByText(/Print Identifier Sticker/i)).toBeInTheDocument(); | ||
expect(screen.getByText('John Wilson')).toBeInTheDocument(); | ||
expect(screen.getByText('100GEJ')).toBeInTheDocument(); | ||
expect(screen.getByText('1972-04-04')).toBeInTheDocument(); | ||
}); | ||
|
||
test('calls closeModal when cancel button is clicked', async () => { | ||
const user = userEvent.setup(); | ||
|
||
renderPrintIdentifierSticker(); | ||
|
||
const cancelButton = screen.getByRole('button', { name: /Cancel/i }); | ||
expect(cancelButton).toBeInTheDocument(); | ||
|
||
await user.click(cancelButton); | ||
expect(mockedCloseModal).toHaveBeenCalled(); | ||
}); | ||
|
||
test('calls the print function when print button is clicked', async () => { | ||
const handlePrint = jest.fn(); | ||
mockedUseReactToPrint.mockReturnValue(handlePrint); | ||
|
||
const user = userEvent.setup(); | ||
|
||
renderPrintIdentifierSticker(); | ||
|
||
const printButton = screen.getByRole('button', { name: /Print/i }); | ||
expect(printButton).toBeInTheDocument(); | ||
|
||
await user.click(printButton); | ||
expect(handlePrint).toHaveBeenCalled(); | ||
}); | ||
}); | ||
function renderPrintIdentifierSticker() { | ||
render(<PrintIdentifierSticker patient={mockPatient} closeModal={mockedCloseModal} />); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ibacher it's trivial to add a config property that governs which orientation to default to and then it would leverage this approach. Should I add one and make it default to
portrait
? (@jnsereko is there a specific requirement for a default orientation for this feature?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch that, it appears that it's not so straightforward to change the orientation for custom sizes (like the default
4in 6in
value). For standard page sizes, something like this works well:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is something else worth looking into when evaluating whether to move away from this library. I'm proceeding to merge to unblock Madiro for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to create a follow up ticket to investigate what's breaking the Library and how to move on. Thank you so much @denniskigen