import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { formatDate, formatDatetime, usePatient } from '@openmrs/esm-framework'; import { usePatientAppointmentHistory } from '../../hooks/usePatientAppointmentHistory'; import { getGender } from '../../helpers'; import { type Appointment } from '../../types'; import styles from './appointment-details.scss'; interface AppointmentDetailsProps { appointment: Appointment; } const AppointmentDetails: React.FC = ({ appointment }) => { const { t } = useTranslation(); const [isEnabledQuery, setIsEnabledQuery] = useState(false); const { appointmentsCount, isLoading } = usePatientAppointmentHistory(appointment.patient.uuid); const { patient } = usePatient(appointment.patient.uuid); useEffect(() => { if (!isLoading) { setIsEnabledQuery(true); } }, [appointmentsCount, isLoading]); const patientName = Array.isArray(patient?.name) && patient.name.length > 0 ? [...(Array.isArray(patient.name[0].given) ? patient.name[0].given : []), patient.name[0].family] .filter(Boolean) .join(' ') : appointment.patient?.name || '--'; const patientIdentifier = Array.isArray(patient?.identifier) && patient.identifier.length > 0 ? patient.identifier[0].value || '--' : appointment.patient?.identifiers?.[0]?.identifier || appointment.patient?.identifier || '--'; const getPatientAge = () => { if (appointment.patient?.person?.age !== undefined && appointment.patient.person.age !== null) { return appointment.patient.person.age; } if (appointment.patient?.age !== undefined && appointment.patient.age !== null) { return appointment.patient.age; } const birthdate = patient?.birthDate || appointment.patient?.person?.birthdate || appointment.patient?.birthdate; if (birthdate) { const birthDateObj = new Date(typeof birthdate === 'number' ? birthdate : birthdate); return Math.floor((Date.now() - birthDateObj.getTime()) / (365.25 * 24 * 60 * 60 * 1000)); } return null; }; const patientAge = getPatientAge(); const getPatientGender = () => { if (appointment.patient?.person?.gender) { return appointment.patient.person.gender; } if (appointment.patient?.gender) { return appointment.patient.gender; } if (patient?.gender) { return patient.gender; } return null; }; const patientGender = getPatientGender(); const getNotesValue = () => { const noteValue = appointment.note; if (noteValue && typeof noteValue === 'string' && noteValue.trim() !== '') { return noteValue.trim(); } const commentsValue = appointment.comments; if (commentsValue && typeof commentsValue === 'string' && commentsValue.trim() !== '') { return commentsValue.trim(); } return null; }; const appointmentNotes = getNotesValue(); return (

{appointment.service?.name || appointment.appointmentService?.name || '--'}

{appointment.appointmentDate ? formatDatetime(new Date(appointment.appointmentDate)) : '--'}

{t('patientDetails', 'Patient details')}

{t('patientName', 'Patient name')}:

{patientName}

{t('identifier', 'Identifier')}:

{patientIdentifier}

{t('age', 'Age')}:

{patientAge !== null ? patientAge : '--'}

{t('gender', 'Gender')}:

{patientGender ? getGender(patientGender, t) : '--'}

{patient && patient?.birthDate ? (

{t('dateOfBirth', 'Date of birth')}:

{formatDate(new Date(patient.birthDate))}

) : ( '' )} {patient && patient?.telecom ? patient.telecom.map((contact, i) => (

{t('Contact', 'Contact {{index}}', { index: i + 1 })}:

{contact.value}

)) : ''}

{t('appointmentNotes', 'Appointment Notes')}

{appointmentNotes || t('noNotesAvailable', 'No notes available')}

{t('appointmentHistory', 'Appointment History')}

{t('completed', 'Completed')}

{appointmentsCount.completedAppointments}

{t('missed', 'Missed')}

{appointmentsCount.missedAppointments}

{t('cancelled', 'Cancelled')}

{appointmentsCount.cancelledAppointments}

{t('upcoming', 'Upcoming')}

{appointmentsCount.upcomingAppointments}
); }; export default AppointmentDetails;