import { useAppContext } from '@openmrs/esm-framework'; import React from 'react'; import WardBed from '../../beds/ward-bed.component'; import { type MotherChildRelationships, type WardPatient, type WardViewContext } from '../../types'; import { bedLayoutToBed } from '../ward-view.resource'; import MaternalWardPatientCard from './maternal-ward-patient-card.component'; const MaternalWardBeds: React.FC = (motherChildRelationships) => { const { motherByChildUuid, isLoading: isLoadingMotherChildRelationships } = motherChildRelationships ?? {}; const { wardPatientGroupDetails } = useAppContext('ward-view-context') ?? {}; const { bedLayouts, wardAdmittedPatientsWithBed, inpatientAdmissionsByPatientUuid } = wardPatientGroupDetails ?? {}; const wardBeds = bedLayouts?.map((bedLayout) => { const { patients: patientsInCurrentBed } = bedLayout; const bed = bedLayoutToBed(bedLayout); const childrenInSameBedByMotherUuid = new Map(); const wardPatients: WardPatient[] = patientsInCurrentBed .map((patient): WardPatient => { const inpatientAdmission = wardAdmittedPatientsWithBed?.get(patient.uuid); if (inpatientAdmission) { const { patient, visit, currentInpatientRequest } = inpatientAdmission; return { patient, visit, bed, inpatientAdmission, inpatientRequest: currentInpatientRequest || null }; } else { const admissionElsewhere = inpatientAdmissionsByPatientUuid.get(patient.uuid); // for some reason this patient is in a bed but not in the list of admitted patients, // so we need to use the patient data from the bed endpoint return { patient: patient, visit: admissionElsewhere?.visit, bed, inpatientAdmission: admissionElsewhere, inpatientRequest: null, }; } }) .filter((wardPatient) => { // filter out any child patient whose mother is also assigned to the same bed // and put the child in childrenInSameBedByMotherUuid const patientUuid = wardPatient.patient.uuid; const { patient: mother } = motherByChildUuid?.get(patientUuid) ?? {}; const motherInSameBed = patientsInCurrentBed.some((p) => p.uuid === mother?.uuid); if (motherInSameBed) { if (!childrenInSameBedByMotherUuid.has(mother.uuid)) { childrenInSameBedByMotherUuid.set(mother.uuid, []); } childrenInSameBedByMotherUuid.get(mother.uuid).push(wardPatient); } return !motherInSameBed; }); const patientCards = wardPatients.map((wardPatient) => ( )); return ( ); }); return <>{wardBeds}; }; export default MaternalWardBeds;