Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Adding delete functionality to the encounter list component #1826

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { navigate } from '@openmrs/esm-framework';
import { navigate, showModal, showSnackbar } from '@openmrs/esm-framework';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { EmptyState } from '../empty-state/empty-state.component';
Expand All @@ -8,11 +8,12 @@ import { OTable } from '../data-table/o-table.component';
import { Button, Link, OverflowMenu, OverflowMenuItem, Pagination, DataTableSkeleton } from '@carbon/react';
import { Add } from '@carbon/react/icons';
import { FormSchema } from '@openmrs/openmrs-form-engine-lib';
import { launchEncounterForm } from './helpers';
import { deleteEncounter, launchEncounterForm } from './helpers';
import { useEncounterRows } from '../../hooks/useEncounterRows';
import { OpenmrsEncounter } from '../../api/types';
import { useFormsJson } from '../../hooks/useFormsJson';
import { usePatientDeathStatus } from '../../hooks/usePatientDeathStatus';
import { mutate } from 'swr';

export interface EncounterListColumn {
key: string;
Expand Down Expand Up @@ -83,10 +84,52 @@ export const EncounterList: React.FC<EncounterListProps> = ({
mode: 'edit',
intent: '*',
},
{
label: t('deleteEncounter', 'Delete'),
form: {
name: forms[0]?.name,
},
mode: 'delete',
intent: '*',
},
],
[forms, t],
);

const handleDeleteEncounter = useCallback((encounterUuid, encounterTypeName) => {
const close = showModal('delete-encounter-modal', {
close: () => close(),
encounterTypeName: encounterTypeName || '',
onConfirmation: () => {
const abortController = new AbortController();
deleteEncounter(encounterUuid, abortController)
.then(() => {
mutate(
(key) =>
typeof key === "string" && key.startsWith("/ws/rest/v1/encounter"),
undefined,
{ revalidate: true }
);
showSnackbar({
isLowContrast: true,
title: t('encounterDeleted', 'Encounter deleted'),
subtitle: `Encounter ${t('successfullyDeleted', 'successfully deleted')}`,
kind: 'success',
});
})
.catch(() => {
showSnackbar({
isLowContrast: false,
title: t('error', 'Error'),
subtitle: `Encounter ${t('failedDeleting', "couldn't be deleted")}`,
kind: 'error',
});
});
close();
},
});
}, [])

useEffect(() => {
if (!isLoadingFormsJson) {
const formsWithFilteredIntents = formsJson.map((form) => {
Expand Down Expand Up @@ -180,24 +223,26 @@ export const EncounterList: React.FC<EncounterListProps> = ({
// If custom config is available, generate actions accordingly; otherwise, fallback to the default actions.
const actions = tableRow.actions?.length ? tableRow.actions : defaultActions;
tableRow['actions'] = (
<OverflowMenu flipped className={styles.flippedOverflowMenu}>
<OverflowMenu flipped className={styles.flippedOverflowMenu} data-testid='actions-id'>
{actions.map((actionItem, index) => (
<OverflowMenuItem
index={index}
itemText={actionItem.label}
onClick={(e) => {
e.preventDefault();
launchEncounterForm(
forms.find((form) => form.name == actionItem?.form?.name),
moduleName,
actionItem.mode == 'enter' ? 'add' : actionItem.mode,
onFormSave,
null,
encounter.uuid,
actionItem.intent,
workspaceWindowSize,
patientUuid,
);
actionItem.mode == 'delete' ?
handleDeleteEncounter(encounter.uuid, encounter.encounterType.name)
: launchEncounterForm(
forms.find((form) => form.name == actionItem?.form?.name),
moduleName,
actionItem.mode == 'enter' ? 'add' : actionItem.mode,
onFormSave,
null,
encounter.uuid,
actionItem.intent,
workspaceWindowSize,
patientUuid,
);
}}
/>
))}
Expand Down
11 changes: 11 additions & 0 deletions packages/esm-commons-lib/src/components/encounter-list/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib';
import { FormSchema } from '@openmrs/openmrs-form-engine-lib';

Expand Down Expand Up @@ -33,3 +34,13 @@ export function launchEncounterForm(
},
});
}

export function deleteEncounter(encounterUuid: string, abortController: AbortController) {
return openmrsFetch(`${restBaseUrl}/encounter/${encounterUuid}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
signal: abortController.signal,
});
}
Loading