Skip to content

Commit

Permalink
remove cohort members whose end date is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
jwamalwa committed Aug 10, 2023
1 parent fae03ae commit a450115
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 25 deletions.
14 changes: 12 additions & 2 deletions packages/esm-patient-list-app/src/api/api-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,24 @@ export async function getPatientListMembers(cohortUuid: string, ac = new AbortCo
throw error;
}

const currentDate = new Date();
const searchQuery = results.map((p) => p.patient.uuid).join(',');
const result = await openmrsFetch(`/ws/fhir2/R4/Patient/_search?_id=${searchQuery}`, {
method: 'POST',
signal: ac.signal,
});

const patients: Array<PatientListMember> = result.data.entry.map((e) => e.resource);
return patients;
const validPatients = patients.filter((patient) => {
if (!patient.endDate) {
return true;
}

const endDate = new Date(patient.endDate);
return endDate >= currentDate;
});

return validPatients;
}

export async function getPatientListIdsForPatient(patientUuid: string, ac = new AbortController()) {
Expand Down Expand Up @@ -140,7 +150,7 @@ export async function removePatientFromList(cohortMembershipUuid: string, ac = n
return postData(
`${cohortUrl}/cohortmember/${cohortMembershipUuid}`,
{
endDate: new Date(),
voidReason: '',
},
ac,
);
Expand Down
1 change: 1 addition & 0 deletions packages/esm-patient-list-app/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface PatientListOption {
}

export interface PatientListMember {
endDate: string | number | Date;
id: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const PatientListDetailComponent = () => {
columns={headers}
isLoading={!patientListMembers && !patients}
isFetching={!patientListMembers}
cohortName={patientListDetails?.name}
search={{
onSearch: handleSearch,
placeHolder: 'Search',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,69 @@
import { isDesktop, showToast, useLayoutType, ExtensionSlot, showNotification } from '@openmrs/esm-framework';
import React, { useCallback } from 'react';
import React, { useCallback, useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { removePatientFromList } from '../api/api-remote';
import { removePatientFromList, getPatientListMembers, getPatientListName } from '../api/api-remote';
import { isDesktop, showToast, useLayoutType, showNotification, ExtensionSlot } from '@openmrs/esm-framework';
import { OverflowMenu, Layer, OverflowMenuItem } from '@carbon/react';
import styles from './patient-table.scss';

interface OverflowMenuCellProps {
cohortMembershipUuid: string;
cohortName: string;
}

const OverflowMenuComponent: React.FC<OverflowMenuCellProps> = ({ cohortMembershipUuid }) => {
const OverflowMenuComponent: React.FC<OverflowMenuCellProps> = ({ cohortMembershipUuid, cohortName }) => {
const { t } = useTranslation();
const layoutType = useLayoutType();
const desktopLayout = isDesktop(layoutType);

const handleRemovePatientFromList = useCallback(() => {
removePatientFromList(cohortMembershipUuid)
.then(() =>
showToast({
title: t('removed', 'Removed'),
description: t('removePatientFromList', 'Successfully removed patient from list'),
}),
)
.catch((err) =>
showNotification({
title: t('errorRemovePatientFromList', "Couldn't remove patient from this list"),
kind: 'error',
critical: true,
description: err?.message,
}),
);
}, [cohortMembershipUuid, t]);
const [patientList, setPatientList] = useState([]);
const [patientListName, setPatientListName] = useState('');

useEffect(() => {
getPatientListName(cohortMembershipUuid)
.then((name) => {
setPatientListName(name);
})
.catch((error) => {
console.error('Error fetching patient list name:', error);
});

fetchAndUpdatePatientList();
}, [cohortMembershipUuid]);

const fetchAndUpdatePatientList = () => {
getPatientListMembers(cohortMembershipUuid)
.then((updatedList) => {
setPatientList(updatedList);
})
.catch((error) => {
console.error('Error fetching updated patient list:', error);
});
};

const handleRemovePatientFromList = useCallback(async () => {
try {
await removePatientFromList(cohortMembershipUuid);

showToast({
title: t('removed', 'Removed'),
description: `${t('successRemovePatientFromList', 'Patient removed from list')}: ${cohortName}`,
});

fetchAndUpdatePatientList();
} catch (error) {
showNotification({
title: t('error', 'Error'),
kind: 'error',
description: `${t('errorRemovePatientFromList', 'Patient not removed from list')}: ${cohortName}`,
});
}
}, [cohortMembershipUuid, cohortName, t]);

return (
<Layer className={styles.layer}>
<OverflowMenu
data-floating-menu-container
ariaLabel="Remove patient from the list"
ariaLabel={`Remove patient from the ${patientListName} list`}
size={desktopLayout ? 'sm' : 'lg'}
flipped>
<ExtensionSlot name={'cohort-member-action-items'} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface PatientTableProps {
autoFocus?: boolean;
isLoading: boolean;
isFetching?: boolean;
cohortName: string;
search: {
onSearch(searchTerm: string): any;
placeHolder: string;
Expand Down Expand Up @@ -61,6 +62,7 @@ const PatientTable: React.FC<PatientTableProps> = ({
isLoading,
autoFocus,
isFetching,
cohortName,
}) => {
const layout = useLayoutType();
const rows: Array<any> = useMemo(
Expand Down Expand Up @@ -138,7 +140,7 @@ const PatientTable: React.FC<PatientTableProps> = ({
<TableCell key={cell.id}>{cell.value?.content ?? cell.value}</TableCell>
))}
<TableCell>
<PatientListOverflowMenuComponent cohortMembershipUuid={row.id} />
<PatientListOverflowMenuComponent cohortMembershipUuid={row.id} cohortName={cohortName} />
</TableCell>
</TableRow>
))}
Expand Down

0 comments on commit a450115

Please sign in to comment.