From 34f77c31ff52341f32d6ed993d5815b87332bfe9 Mon Sep 17 00:00:00 2001 From: Jovan Ssebaggala Date: Tue, 25 Jun 2024 11:41:13 +0300 Subject: [PATCH] Add encounter list refactors --- .../encounter-list.component.tsx | 28 +++++++++++++------ .../src/hooks/useEncounterRows.ts | 11 ++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/esm-commons-lib/src/components/encounter-list/encounter-list.component.tsx b/packages/esm-commons-lib/src/components/encounter-list/encounter-list.component.tsx index 3a8b3d053..4c4352a33 100644 --- a/packages/esm-commons-lib/src/components/encounter-list/encounter-list.component.tsx +++ b/packages/esm-commons-lib/src/components/encounter-list/encounter-list.component.tsx @@ -13,7 +13,6 @@ 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; @@ -42,6 +41,7 @@ export interface EncounterListProps { workspaceWindowSize?: 'minimized' | 'maximized'; }; filter?: (encounter: any) => boolean; + afterFormSaveAction?: () => void; } export const EncounterList: React.FC = ({ @@ -53,6 +53,7 @@ export const EncounterList: React.FC = ({ formList, filter, launchOptions, + afterFormSaveAction, }) => { const { t } = useTranslation(); const [paginatedRows, setPaginatedRows] = useState([]); @@ -63,7 +64,12 @@ export const EncounterList: React.FC = ({ const { isDead } = usePatientDeathStatus(patientUuid); const formNames = useMemo(() => formList.map((form) => form.name), []); const { formsJson, isLoading: isLoadingFormsJson } = useFormsJson(formNames); - const { encounters, isLoading, onFormSave } = useEncounterRows(patientUuid, encounterType, filter); + const { encounters, isLoading, onFormSave } = useEncounterRows( + patientUuid, + encounterType, + filter, + afterFormSaveAction, + ); const { moduleName, workspaceWindowSize, displayText, hideFormLauncher } = launchOptions; const defaultActions = useMemo( @@ -120,10 +126,14 @@ export const EncounterList: React.FC = ({ kind: 'error', }); }); + + // Update encounters after deletion + const updatedEncounters = encounters.filter((enc) => enc.uuid !== encounterUuid); + constructPaginatedTableRows(updatedEncounters, currentPage, pageSize); close(); }, }); - }, []) + }, []); useEffect(() => { if (!isLoadingFormsJson) { @@ -218,15 +228,15 @@ export const EncounterList: React.FC = ({ // If custom config is available, generate actions accordingly; otherwise, fallback to the default actions. const actions = tableRow.actions?.length ? tableRow.actions : defaultActions; tableRow['actions'] = ( - + {actions.map((actionItem, index) => ( { e.preventDefault(); - actionItem.mode == 'delete' ? - handleDeleteEncounter(encounter.uuid, encounter.encounterType.name) + actionItem.mode == 'delete' + ? handleDeleteEncounter(encounter.uuid, encounter.encounterType.name) : launchEncounterForm( forms.find((form) => form.name == actionItem?.form?.name), moduleName, @@ -237,7 +247,7 @@ export const EncounterList: React.FC = ({ actionItem.intent, workspaceWindowSize, patientUuid, - ); + ); }} /> ))} @@ -247,7 +257,7 @@ export const EncounterList: React.FC = ({ }); setPaginatedRows(rows); }, - [columns, defaultActions, forms, moduleName, workspaceWindowSize, patientUuid, onFormSave], + [columns, defaultActions, forms, moduleName, onFormSave, workspaceWindowSize, patientUuid, handleDeleteEncounter], ); useEffect(() => { @@ -260,7 +270,7 @@ export const EncounterList: React.FC = ({ if (forms.length == 1 && !forms[0]['availableIntents']?.length) { // we only have one form with no intents // just return the "Add" button - return ( + return (