import React, { type ReactNode } from 'react'; import classNames from 'classnames'; import { useTranslation } from 'react-i18next'; import { Tile } from '@carbon/react'; import { isDesktop, useLayoutType } from '@openmrs/esm-framework'; import { type MedicationDispense, type MedicationRequest, type Quantity } from '../types'; import { calculateIsFreeTextDosage, getDosageInstruction, getMedicationDisplay, getMedicationReferenceOrCodeableConcept, getQuantity, getRefillsAllowed, } from '../utils'; import styles from './medication-event.scss'; /** * Renders a prescription request of a prescript event (ex: ordered, dispensed) */ const MedicationEvent: React.FC<{ medicationEvent: MedicationRequest | MedicationDispense; status?: ReactNode; children?: ReactNode; isDispenseEvent?: boolean; }> = ({ medicationEvent, status = null, children, isDispenseEvent }) => { const { t } = useTranslation(); const dosageInstruction = getDosageInstruction(medicationEvent.dosageInstruction); const isFreeTextDosage = calculateIsFreeTextDosage(dosageInstruction); const quantity: Quantity = getQuantity(medicationEvent); const refillsAllowed: number = getRefillsAllowed(medicationEvent); const isTablet = !isDesktop(useLayoutType()); return (

{status} {status && ' '} {getMedicationDisplay(getMedicationReferenceOrCodeableConcept(medicationEvent))}

{!isFreeTextDosage && (

{t('dose', 'Dose').toUpperCase()}{' '} {dosageInstruction?.doseAndRate && dosageInstruction.doseAndRate.map((doseAndRate, index) => { return ( {doseAndRate?.doseQuantity?.value} {doseAndRate?.doseQuantity?.unit} ); })} {dosageInstruction?.route?.text && <> — {dosageInstruction.route.text}} {dosageInstruction?.timing?.code?.text && <> — {dosageInstruction.timing.code.text}} {dosageInstruction?.timing?.repeat?.duration && ( <> {' '} for {dosageInstruction.timing.repeat.duration} {dosageInstruction.timing.repeat.durationUnit} )}

)} {quantity && (

{t('quantity', 'Quantity').toUpperCase()}{' '} {quantity.value} {quantity.unit}

)} {(refillsAllowed || refillsAllowed === 0) && (

{t('refills', 'Refills').toUpperCase()}{' '} {refillsAllowed}

)} {dosageInstruction?.text &&

{dosageInstruction.text}

} {dosageInstruction?.additionalInstruction?.length > 0 && (

{dosageInstruction?.additionalInstruction[0].text}

)}
{children}
); }; export default MedicationEvent;