Skip to content

Commit

Permalink
fix drug-search-workspace only to close
Browse files Browse the repository at this point in the history
  • Loading branch information
jwamalwa committed Nov 6, 2023
1 parent 7de3865 commit ddd0e30
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import React, { useMemo } from 'react';
import { ExtensionSlot, useBodyScrollLock, useLayoutType, usePatient } from '@openmrs/esm-framework';
import {
type OpenWorkspace,
useWorkspaces,
updateWorkspaceWindowState,
launchPatientWorkspace,
} from '@openmrs/esm-patient-common-lib';
import { type OpenWorkspace, useWorkspaces, updateWorkspaceWindowState } 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 @@ -41,6 +36,7 @@ const WorkspaceWindow: React.FC<ContextWorkspaceParams> = () => {
}, [workspaces, patientUuid]);

const workspaceTitle = workspaces[0]?.additionalProps?.['workspaceTitle'] ?? workspaces[0]?.title ?? '';
const onCloseWorkspace = workspaces[0]?.additionalProps?.['onCloseWorkspace'] ?? null;
const {
canHide = false,
canMaximize = false,
Expand All @@ -52,14 +48,19 @@ 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"
className={`${styles.header} ${maximized ? `${styles.fullWidth}` : `${styles.dynamicWidth}`}`}
>
{layout === 'tablet' && !canHide && (
<HeaderMenuButton renderMenuIcon={<ArrowLeft />} onClick={closeWorkspace} />
<HeaderMenuButton renderMenuIcon={<ArrowLeft />} onClick={onCloseWorkspace ?? closeWorkspace} />
)}
<HeaderName prefix="">{workspaceTitle}</HeaderName>
<HeaderGlobalBar className={styles.headerGlobalBar}>
Expand Down Expand Up @@ -89,10 +90,7 @@ const WorkspaceWindow: React.FC<ContextWorkspaceParams> = () => {
<HeaderGlobalAction
align="bottom-right"
label={t('close', 'Close')}
onClick={() => {
closeWorkspace?.();
launchPatientWorkspace('order-basket');
}}
onClick={() => onCloseWorkspace?.() ?? closeWorkspace?.()}
size="lg"
>
<Close />
Expand All @@ -101,7 +99,11 @@ const WorkspaceWindow: React.FC<ContextWorkspaceParams> = () => {
</>
)}
{layout === 'tablet' && canHide && (
<HeaderGlobalAction align="bottom-right" label={t('close', 'Close')} onClick={() => closeWorkspace?.()}>
<HeaderGlobalAction
align="bottom-right"
label={t('close', 'Close')}
onClick={() => onCloseWorkspace?.() ?? closeWorkspace?.()}
>
<DownToBottom />
</HeaderGlobalAction>
)}
Expand Down
22 changes: 3 additions & 19 deletions packages/esm-patient-common-lib/src/workspaces/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,12 @@ function promptBeforeLaunchingWorkspace(
* @param name The name of the workspace to launch
* @param additionalProps Props to pass to the workspace component being launched
*/

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

export function closeWorkspace(name: string, ignoreChanges: boolean, onClosingWorkspace: () => void) {
export function closeWorkspace(name: string, ignoreChanges: boolean) {
const store = getWorkspaceStore();
const promptCheckFcn = getPromptBeforeClosingFcn(name);
if (!ignoreChanges && promptCheckFcn && promptCheckFcn()) {
Expand All @@ -267,15 +253,13 @@ export function closeWorkspace(name: string, ignoreChanges: boolean, onClosingWo
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next';
import { Button, Tile } from '@carbon/react';
import { Add, ChevronDown, ChevronUp } from '@carbon/react/icons';
import { useLayoutType } from '@openmrs/esm-framework';
import { launchPatientWorkspace, useOrderBasket } from '@openmrs/esm-patient-common-lib';
import { closeWorkspace, launchPatientWorkspace, useOrderBasket } from '@openmrs/esm-patient-common-lib';
import { prepMedicationOrderPostData } from '../api/api';
import type { DrugOrderBasketItem } from '../types';
import OrderBasketItemTile from './order-basket-item-tile.component';
Expand All @@ -23,8 +23,15 @@ export default function DrugOrderBasketPanelExtension() {
const revisedOrderBasketItems = orders.filter((x) => x.action === 'REVISE');
const discontinuedOrderBasketItems = orders.filter((x) => x.action === 'DISCONTINUE');

const onClose = useCallback(() => {
closeWorkspace('add-drug-order', true);
launchPatientWorkspace('order-basket');
}, []);

const openDrugSearch = () => {
launchPatientWorkspace('add-drug-order');
launchPatientWorkspace('add-drug-order', {
onCloseWorkspace: onClose,
});
};

const openDrugForm = (order: DrugOrderBasketItem) => {
Expand Down

0 comments on commit ddd0e30

Please sign in to comment.