import React from 'react'; import { MedicationDispense } from 'fhir/r4'; import { FhirCodeableConceptDisplay } from '../../utils/fhirCodeableConcept'; import { FhirCodeDisplay } from '../../utils/fhirCode'; import { FhirQuantityDisplay } from '../../utils/fhirQuantity'; import { FhirAnnotationDisplay } from '../../utils/fhirAnnotation'; import { FhirDosageDisplay } from '../../utils/fhirDosage'; import { formatDateTime } from '../../utils/dateUtils'; import { FhirReferenceDisplay } from '../../utils/fhirReference'; import { renderField } from '../../utils/renderField'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '../ui/dialog'; import { Card, CardHeader, CardContent } from '../ui/card'; import { FieldHistoryProvider, FieldHistoryScope } from '../FieldHistory/FieldHistoryProvider'; import { ShowFieldHistoryToggle } from '../FieldHistory/ShowFieldHistoryToggle'; import { PrintRecord } from '../ui/print-record'; import { RecordEditedMarker } from '../FieldHistory/RecordEditedMarker'; import { FieldGroup, Section } from '../FieldHistory/FieldLayout'; interface FhirMedicationDispenseDisplayProps { medicationDispense: MedicationDispense; isOpen: boolean; onClose: () => void; title?: string; } export const FhirMedicationDispenseDisplay: React.FC = ({ medicationDispense, isOpen, onClose, title = "Medication Dispense Details" }) => { const renderMedicationValue = () => { if (medicationDispense.medicationCodeableConcept) { return ; } if (medicationDispense.medicationReference) { return ; } return '-'; }; const renderStatusReasonValue = () => { if (medicationDispense.statusReasonCodeableConcept) { return ; } if (medicationDispense.statusReasonReference) { return ; } return '-'; }; const renderPerformers = () => { if (!medicationDispense.performer || medicationDispense.performer.length === 0) { return (

No performers recorded

); } return (
{medicationDispense.performer.map((performer, index) => (
Performer {index + 1}
{renderField('Who performed the dispense and what they did', )} {renderField('Individual who was performing', )}
))}
); }; const renderSubstitution = () => { if (!medicationDispense.substitution) { return (

No substitution information

); } const substitution = medicationDispense.substitution; return (
Substitution Information
{renderField('Was Substituted', substitution.wasSubstituted === true ? 'Yes' : 'No', 'substitution.wasSubstituted')} {substitution.wasSubstituted && ( <> {renderField('Code signifying whether a different drug was dispensed from what was prescribed', , 'substitution.type.coding[0].display')} {renderField('Why was substitution made', )} {renderField('Who is responsible for the substitution', )} )}
); }; const renderDosageInstructions = () => { if (!medicationDispense.dosageInstruction || medicationDispense.dosageInstruction.length === 0) { return (

No dosage instructions recorded

); } return ; }; // `medicationDispense` is frequently a forced cast over a union of resource types (several // callers keep one `selectedItem`/`selected.resource` and multiplex it across many `Fhir*Display` // modals, gating only via `isOpen`) -- it can be the wrong resource shape, or undefined, whenever // `isOpen` is false. This guard must run before anything below touches `medicationDispense.*` // so a non-MedicationDispense/absent value while closed never throws (mirrors the original // `if (!isOpen) return null;` that ran before WP's `` was ever constructed). if (!isOpen) return null; return ( { if (!next) onClose(); }} >
{title}
{renderField('Medication to be taken', renderMedicationValue(), medicationDispense.medicationCodeableConcept ? 'medicationCodeableConcept.coding[0].display' : undefined)} {renderField('Status', , 'status')} {renderField('Why a dispense was not performed', renderStatusReasonValue(), medicationDispense.statusReasonCodeableConcept ? 'statusReasonCodeableConcept.coding[0].display' : undefined)} {renderField('Type of medication dispense', , 'category.coding[0].display')} {renderField('Trial fill, partial fill, emergency fill, etc.', , 'type.coding[0].display')} {renderField('Medication order that authorizes the dispense', )} {renderField('Where the dispense occurred', )} {renderField('Where the medication was sent', )} {renderField('When product was packaged and reviewed', formatDateTime(medicationDispense.whenPrepared), 'whenPrepared')} {renderField('When product was given out', formatDateTime(medicationDispense.whenHandedOver), 'whenHandedOver')} {renderField('Clinical issue with action', )} {renderField('Event that dispense is part of', )} {renderField('Encounter / Episode associated with event', )}
{renderField('Amount dispensed', , 'quantity.value')} {renderField('Amount of medication expressed as a timing amount', , 'daysSupply.value')}
{renderDosageInstructions()}
{renderSubstitution()}
{renderPerformers()}
{renderField('Information about the dispense', )}
); }; export default FhirMedicationDispenseDisplay;