Skip to content

Commit

Permalink
(fix)03-2426: Closing drug search panel should send user back to orde…
Browse files Browse the repository at this point in the history
…r basket if they previously had it open
  • Loading branch information
jwamalwa committed Nov 3, 2023
1 parent 1c37485 commit b4feaaa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React, { useMemo } from 'react';
import { ExtensionSlot, useBodyScrollLock, useLayoutType, usePatient } from '@openmrs/esm-framework';
import { type OpenWorkspace, useWorkspaces, updateWorkspaceWindowState } from '@openmrs/esm-patient-common-lib';
import {
type OpenWorkspace,
useWorkspaces,
updateWorkspaceWindowState,
launchPatientWorkspace,
} from '@openmrs/esm-patient-common-lib';
import { Header, HeaderGlobalBar, HeaderName, HeaderMenuButton, HeaderGlobalAction, IconButton } from '@carbon/react';
import { ArrowLeft, ArrowRight, Close, DownToBottom, Maximize, Minimize } from '@carbon/react/icons';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -47,12 +52,7 @@ const WorkspaceWindow: React.FC<ContextWorkspaceParams> = () => {
<aside
className={`${styles.container} ${width === 'narrow' ? styles.narrowWorkspace : styles.widerWorkspace} ${
maximized ? `${styles.maximized}` : undefined
} ${
isWorkspaceWindowOpen
? `${styles.show}`
: `${styles.hide}
}`
}`}
} ${isWorkspaceWindowOpen ? `${styles.show}` : `${styles.hide}`}`}
>
<Header
aria-label="Workspace Title"
Expand Down Expand Up @@ -89,7 +89,10 @@ const WorkspaceWindow: React.FC<ContextWorkspaceParams> = () => {
<HeaderGlobalAction
align="bottom-right"
label={t('close', 'Close')}
onClick={() => closeWorkspace?.()}
onClick={() => {
closeWorkspace?.();
launchPatientWorkspace('order-basket');
}}
size="lg"
>
<Close />
Expand Down
22 changes: 19 additions & 3 deletions packages/esm-patient-common-lib/src/workspaces/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,26 @@ function promptBeforeLaunchingWorkspace(
* @param name The name of the workspace to launch
* @param additionalProps Props to pass to the workspace component being launched
*/
export function launchPatientWorkspace(name: string, additionalProps?: object) {

const myFunction = () => {
const orderBasketOpen = true;
if (orderBasketOpen) {
navigate({ to: '/order-basket' });
}
};

launchPatientWorkspace('drug-search-workspace', {
onClosingWorkspace: myFunction,
});

export function launchPatientWorkspace(name: string, additionalProps?: Record<string, any>) {
const store = getWorkspaceStore();
const workspace = getWorkspaceRegistration(name);
const newWorkspace = {
...workspace,
closeWorkspace: (ignoreChanges = true) => closeWorkspace(name, ignoreChanges),
closeWorkspace: (ignoreChanges = true) => {
closeWorkspace(name, ignoreChanges, additionalProps?.onClosingWorkspace);
},
promptBeforeClosing: (testFcn) => promptBeforeClosing(name, testFcn),
additionalProps,
};
Expand Down Expand Up @@ -239,7 +253,7 @@ export function cancelPrompt() {
store.setState({ ...state, prompt: null });
}

export function closeWorkspace(name: string, ignoreChanges: boolean) {
export function closeWorkspace(name: string, ignoreChanges: boolean, onClosingWorkspace: () => void) {
const store = getWorkspaceStore();
const promptCheckFcn = getPromptBeforeClosingFcn(name);
if (!ignoreChanges && promptCheckFcn && promptCheckFcn()) {
Expand All @@ -253,13 +267,15 @@ export function closeWorkspace(name: string, ignoreChanges: boolean) {
onConfirm: () => {
const state = store.getState();
store.setState({ ...state, prompt: null, openWorkspaces: state.openWorkspaces.filter((w) => w.name != name) });
onClosingWorkspace?.();
},
confirmText: translateFrom('@openmrs/esm-patient-chart-app', 'discard', 'Discard'),
};
store.setState({ ...store.getState(), prompt });
} else {
const state = store.getState();
store.setState({ ...state, openWorkspaces: state.openWorkspaces.filter((w) => w.name != name) });
onClosingWorkspace?.();
}
}

Expand Down

0 comments on commit b4feaaa

Please sign in to comment.