import { Button, DataTable, DataTableSkeleton, InlineLoading, OverflowMenu, OverflowMenuItem, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow, Tag, Tile, } from '@carbon/react'; import { Close, DocumentAdd } from '@carbon/react/icons'; import { formatDate, launchWorkspace, restBaseUrl, useConfig, useLayoutType, useVisit } from '@openmrs/esm-framework'; import { CardHeader, EmptyState, ErrorState, launchStartVisitPrompt } from '@openmrs/esm-patient-common-lib'; import capitalize from 'lodash/capitalize'; import React, { useCallback, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { mutate } from 'swr'; import { PatientCarePrograms, useCarePrograms } from '../hooks/useCarePrograms'; import { CarePanelConfig } from '../config-schema'; import { launchDeleteProgramDialog, launchProgramForm, usePatientEnrolledPrograms } from './care-program.resource'; import styles from './care-programs.scss'; import ProgramFormOverflowMenuItem from './program-form-overflow-menu-item.component'; import useCareProgramForms from './useCareProgramForms'; type CareProgramsProps = { patientUuid: string; }; const CarePrograms: React.FC = ({ patientUuid }) => { const { t } = useTranslation(); const { peerCalendarOutreactForm } = useConfig(); const { getProgramForms, getProgramEnrollmentForm } = useCareProgramForms(); const { currentVisit, mutate: mutateVisit } = useVisit(patientUuid); const { eligibleCarePrograms, isLoading, isValidating, error, mutateEligiblePrograms } = useCarePrograms(patientUuid); const { enrollments, isLoading: isLoadingEnrollments, error: enrollmentsError, mutate: mutateEnrollments, } = usePatientEnrolledPrograms(patientUuid); const isTablet = useLayoutType() === 'tablet'; const handleMutations = useCallback(() => { mutateEligiblePrograms(); mutate( (key) => typeof key === 'string' && key.startsWith(`${restBaseUrl}/kenyaemr/patientHistoricalEnrollment?patientUuid=`), undefined, { revalidate: true }, ); mutate( (key) => typeof key === 'string' && key.startsWith(`${restBaseUrl}/programenrollment?patient=${patientUuid}`), undefined, { revalidate: true }, ); mutateVisit(); mutateEnrollments(); }, [mutateEligiblePrograms, mutateEnrollments, mutateVisit, patientUuid]); const handleCareProgramClick = useCallback( (careProgram: PatientCarePrograms) => { const isEnrolled = careProgram.enrollmentStatus === 'active'; const formUuid = isEnrolled ? careProgram.discontinuationFormUuid : careProgram.enrollmentFormUuid; const workspaceTitle = isEnrolled ? `${careProgram.display} Discontinuation form` : `${careProgram.display} Enrollment form`; currentVisit ? launchWorkspace('patient-form-entry-workspace', { workspaceTitle: workspaceTitle, mutateForm: handleMutations, formInfo: { encounterUuid: '', formUuid, additionalProps: { enrollmenrDetails: careProgram.enrollmentDetails ?? {} }, }, }) : launchStartVisitPrompt(); }, [currentVisit, handleMutations], ); const rows = useMemo( () => [ ...enrollments.map((enrollment) => { const forms = getProgramForms(enrollment.program.uuid).filter((form) => !form.tags.includes('enrollment')); return { id: enrollment.program.uuid, programName: enrollment.program.name, status: (
Enrolled {forms.map((form) => { return ( ); })} launchProgramForm(enrollment.program.uuid, patientUuid, enrollment, () => mutateEnrollments()) } /> launchDeleteProgramDialog(enrollment.uuid, patientUuid)} />
), }; }), ...eligibleCarePrograms.map((careProgram) => { const enrollmentForm = getProgramEnrollmentForm(careProgram.uuid); return { id: `${careProgram.uuid}`, programName: careProgram.display, status: (
{capitalize( `${careProgram.enrollmentStatus} ${ careProgram.enrollmentDetails?.dateEnrolled && careProgram.enrollmentStatus === 'active' ? `Since (${formatDate(new Date(careProgram.enrollmentDetails.dateEnrolled))})` : '' }`, )}
), }; }), ], [ enrollments, eligibleCarePrograms, getProgramForms, t, currentVisit, patientUuid, handleMutations, mutateEnrollments, getProgramEnrollmentForm, mutateEligiblePrograms, ], ); const headers = [ { key: 'programName', header: 'Program name', }, { key: 'status', header: 'Status', }, ]; if (isLoading) { return ; } if (error) { return ; } if (eligibleCarePrograms.length === 0) { return ; } return ( {isValidating && ( )} {({ rows, headers, getTableProps, getHeaderProps, getRowProps }) => ( {headers.map((header) => ( {header.header} ))} {rows.map((row) => ( {row.cells.map((cell) => ( {cell.value} ))} ))}
)}
); }; export default CarePrograms;