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

(fix) Improve encounter list behaviour #1886

Merged
merged 4 commits into from
Jun 27, 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
7 changes: 4 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ const config = {
'^@carbon/charts-react$': path.resolve(__dirname, '__mocks__', '@carbon__charts-react.ts'),
'^dexie$': require.resolve('dexie'),
'^lodash-es/(.*)$': 'lodash/$1',
'^lodash-es$': 'lodash',
'^react-i18next$': path.resolve(__dirname, '__mocks__', 'react-i18next.js'),
},
testEnvironment: 'jsdom',
testPathIgnorePatterns: [
"/node_modules/",
"/e2e/" // Ignore the e2e directory containing Playwright tests
]
'/node_modules/',
'/e2e/', // Ignore the e2e directory containing Playwright tests
],
};

module.exports = config;
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import localizedFormat from 'dayjs/plugin/localizedFormat';
import relativeTime from 'dayjs/plugin/relativeTime';
import { AddPatientToListOverflowMenuItem } from '../modals/add-patient-to-list-modal.component';
import { fetchPatientLastEncounter } from '../../api/api';
import { changeWorkspaceContext } from '@openmrs/esm-patient-common-lib';
import { launchForm } from '../../utils/ohri-forms-commons';
import { navigate } from '@openmrs/esm-framework';
import { navigate, WorkspaceWindow } from '@openmrs/esm-framework';

interface PatientMetaConfig {
location: { name: string };
Expand Down Expand Up @@ -55,21 +54,23 @@ export const LaunchableFormMenuItem = ({
} else {
setIsLoading(false);
}
}, []);
}, [continueEncounterActionText, encounterType, encounterUuid, launchableForm.editLatestEncounter, patientUuid]);

return (
<>
{isLoading ? (
<InlineLoading style={{ margin: '0 auto', width: '16px' }} />
) : (
<OverflowMenuItem
itemText={actionText}
onClick={() => {
changeWorkspaceContext(patientUuid);
launchForm(form, encounterUuid ? 'edit' : 'enter', moduleName, form.name, encounterUuid, null, null);
navigate({ to: patientUrl });
}}
/>
<>
<OverflowMenuItem
itemText={actionText}
onClick={() => {
launchForm(form, encounterUuid ? 'edit' : 'enter', moduleName, form.name, encounterUuid, null, null);
navigate({ to: patientUrl });
}}
/>
<WorkspaceWindow contextKey={`patient/${patientUuid}`} />
</>
)}
</>
);
Expand All @@ -94,7 +95,7 @@ export const ViewSummaryMenuItem = ({ patientUuid, ViewSummary, encounterType })
} else {
setIsLoading(false);
}
}, []);
}, [ViewSummary.editLatestEncounter, encounterType, encounterUuid, patientUuid, viewSummaryActionText]);

return (
<>
Expand Down Expand Up @@ -132,7 +133,7 @@ export const ViewTptSummaryMenuItem = ({ patientUuid, ViewTptSummary, encounterT
} else {
setIsLoading(false);
}
}, []);
}, [ViewTptSummary.editLatestEncounter, encounterType, patientUuid, encounterUuid, viewTptSummaryActionText]);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +41,7 @@ export interface EncounterListProps {
workspaceWindowSize?: 'minimized' | 'maximized';
};
filter?: (encounter: any) => boolean;
afterFormSaveAction?: () => void;
}

export const EncounterList: React.FC<EncounterListProps> = ({
Expand All @@ -53,6 +53,7 @@ export const EncounterList: React.FC<EncounterListProps> = ({
formList,
filter,
launchOptions,
afterFormSaveAction,
}) => {
const { t } = useTranslation();
const [paginatedRows, setPaginatedRows] = useState([]);
Expand All @@ -63,7 +64,12 @@ export const EncounterList: React.FC<EncounterListProps> = ({
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(
Expand Down Expand Up @@ -104,12 +110,7 @@ export const EncounterList: React.FC<EncounterListProps> = ({
const abortController = new AbortController();
deleteEncounter(encounterUuid, abortController)
.then(() => {
mutate(
(key) =>
typeof key === "string" && key.startsWith("/ws/rest/v1/encounter"),
undefined,
{ revalidate: true }
);
onFormSave();
showSnackbar({
isLowContrast: true,
title: t('encounterDeleted', 'Encounter deleted'),
Expand All @@ -125,10 +126,14 @@ export const EncounterList: React.FC<EncounterListProps> = ({
kind: 'error',
});
});

// Update encounters after deletion
const updatedEncounters = encounters.filter((enc) => enc.uuid !== encounterUuid);
constructPaginatedTableRows(updatedEncounters, currentPage, pageSize);
close();
},
});
}, [])
}, []);

useEffect(() => {
if (!isLoadingFormsJson) {
Expand Down Expand Up @@ -223,15 +228,15 @@ 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} data-testid='actions-id'>
<OverflowMenu flipped className={styles.flippedOverflowMenu} data-testid="actions-id">
{actions.map((actionItem, index) => (
<OverflowMenuItem
index={index}
itemText={actionItem.label}
onClick={(e) => {
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,
Expand All @@ -242,7 +247,7 @@ export const EncounterList: React.FC<EncounterListProps> = ({
actionItem.intent,
workspaceWindowSize,
patientUuid,
);
);
}}
/>
))}
Expand All @@ -252,7 +257,7 @@ export const EncounterList: React.FC<EncounterListProps> = ({
});
setPaginatedRows(rows);
},
[columns, defaultActions, forms, moduleName, workspaceWindowSize, patientUuid, onFormSave],
[columns, defaultActions, forms, moduleName, onFormSave, workspaceWindowSize, patientUuid, handleDeleteEncounter],
);

useEffect(() => {
Expand All @@ -265,7 +270,7 @@ export const EncounterList: React.FC<EncounterListProps> = ({
if (forms.length == 1 && !forms[0]['availableIntents']?.length) {
// we only have one form with no intents
// just return the "Add" button
return (
return (
<Button
kind="ghost"
renderIcon={Add}
Expand Down
10 changes: 8 additions & 2 deletions packages/esm-commons-lib/src/hooks/useEncounterRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { openmrsFetch } from '@openmrs/esm-framework';
import { encounterRepresentation } from '../constants';

export function useEncounterRows(patientUuid: string, encounterType: string, encounterFilter: (encounter) => boolean) {
export function useEncounterRows(
patientUuid: string,
encounterType: string,
encounterFilter: (encounter) => boolean,
afterFormSaveAction: () => void,
) {
const [encounters, setEncounters] = useState([]);
const url = `/ws/rest/v1/encounter?encounterType=${encounterType}&patient=${patientUuid}&v=${encounterRepresentation}`;

Expand Down Expand Up @@ -32,7 +37,8 @@ export function useEncounterRows(patientUuid: string, encounterType: string, enc

const onFormSave = useCallback(() => {
mutate();
}, [mutate]);
afterFormSaveAction && afterFormSaveAction();
}, [afterFormSaveAction, mutate]);

return {
encounters,
Expand Down
21 changes: 5 additions & 16 deletions packages/esm-commons-lib/src/workspace/ohri-workspace-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getSyncLifecycle } from '@openmrs/esm-framework';
import { BehaviorSubject } from 'rxjs';
import { closeWorkspace, launchPatientWorkspace, registerWorkspace } from '@openmrs/esm-patient-common-lib';
import { FormEngine, SessionMode } from '@openmrs/openmrs-form-engine-lib';
import { closeWorkspace } from '@openmrs/esm-framework';
import { launchPatientWorkspace } from '@openmrs/esm-patient-common-lib';
import { SessionMode } from '@openmrs/openmrs-form-engine-lib';

export interface WorkspaceContextProps {
title: string;
encounterUuid?: string;
Expand All @@ -18,26 +19,14 @@ let counter = 0;

export const launchOHRIWorkSpace = (props: WorkspaceContextProps) => {
const workspaceName = props.workspaceName || 'ohri-forms-' + counter++;

const close = () => {
return closeWorkspace(workspaceName, { ignoreChanges: true });
};

const onFormSubmit = () => {
props.state?.updateParent?.();
close();
};
registerWorkspace({
name: workspaceName,
title: props.title,
preferredWindowSize: <any>props.screenSize,
load: getSyncLifecycle(FormEngine, {
featureName: 'ohri-forms-workspace-item',
moduleName: props.moduleName,
}),
canMaximize: true,
canHide: true,
width: 'wider',
});
launchPatientWorkspace(workspaceName, {
...props.state,
mode: props.mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { launchFormWithCustomTitle } from '@ohri/openmrs-esm-ohri-commons-lib';
import { getForm, applyFormIntent } from '@openmrs/openmrs-form-engine-lib';
import styles from './tabs/patient-list.scss';
import { OverflowMenu, OverflowMenuItem } from '@carbon/react';
import { changeWorkspaceContext, closeAllWorkspaces, resetWorkspaceStore } from '@openmrs/esm-patient-common-lib';
import { navigate } from '@openmrs/esm-framework';
import { useTranslation } from 'react-i18next';
import { moduleName } from '../../../index';
Expand Down Expand Up @@ -47,7 +46,6 @@ export const LabresultsFormViewer: React.FC<LabresultsFormViewerProps> = ({
itemText={t('viewResult', 'View Result')}
onClick={(e) => {
e.preventDefault();
changeWorkspaceContext(patientUuid);
launchEncounterForm(applyFormIntent('*', getForm(form.package, form.name)), '*', 'view', encounterUuid);
navigate({ to: patientUrl });
}}
Expand Down
Loading
Loading