From 9f15df7c4025d90a6236e152bff1ef8b7a01e4e6 Mon Sep 17 00:00:00 2001 From: Vineet Sharma Date: Wed, 6 Dec 2023 16:46:54 +0530 Subject: [PATCH] (fix) Workspace window should have the size of the preferred window state (#1444) --- .../workspace/workspace-window.component.tsx | 2 +- .../src/workspaces/workspaces.test.ts | 42 +++++++++++++++++++ .../src/workspaces/workspaces.ts | 29 +++++++++---- 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/packages/esm-patient-chart-app/src/workspace/workspace-window.component.tsx b/packages/esm-patient-chart-app/src/workspace/workspace-window.component.tsx index 0946d77d08..1cdd73c0b6 100644 --- a/packages/esm-patient-chart-app/src/workspace/workspace-window.component.tsx +++ b/packages/esm-patient-chart-app/src/workspace/workspace-window.component.tsx @@ -65,7 +65,7 @@ const WorkspaceWindow: React.FC = () => { {isDesktop(layout) && ( <> - {canMaximize && ( + {(canMaximize || maximized) && ( { expect(store.getState().openWorkspaces[0].name).toBe('vitals'); expect(store.getState().openWorkspaces[1].name).toBe('attachments'); }); + + it("should launch workspace in workspace's preferredSize", () => { + const store = getWorkspaceStore(); + registerWorkspace({ + name: 'allergies', + title: 'Allergies', + load: jest.fn(), + canHide: true, + type: 'form', + preferredWindowSize: 'maximized', + }); + registerWorkspace({ + name: 'attachments', + title: 'Attachements', + load: jest.fn(), + canHide: true, + type: 'attachments-form', + }); + registerWorkspace({ + name: 'conditions', + title: 'Conditions', + load: jest.fn(), + type: 'conditions-form', + preferredWindowSize: 'maximized', + }); + launchPatientWorkspace('allergies'); + expect(store.getState().openWorkspaces.length).toBe(1); + expect(store.getState().workspaceWindowState).toBe('maximized'); + launchPatientWorkspace('attachments'); + expect(store.getState().openWorkspaces.length).toBe(2); + expect(store.getState().workspaceWindowState).toBe('normal'); + launchPatientWorkspace('conditions'); + expect(store.getState().openWorkspaces.length).toBe(3); + expect(store.getState().workspaceWindowState).toBe('maximized'); + store.getState().openWorkspaces[0].closeWorkspace(false); + expect(store.getState().workspaceWindowState).toBe('normal'); + store.getState().openWorkspaces[0].closeWorkspace(false); + expect(store.getState().workspaceWindowState).toBe('maximized'); + store.getState().openWorkspaces[0].closeWorkspace(false); + expect(store.getState().workspaceWindowState).toBe('normal'); + }); }); test('coexisting and non-coexisting workspaces', () => { @@ -353,6 +394,7 @@ describe('workspace system', () => { ); expect(store.getState().prompt.confirmText).toBe('Discard'); store.getState().prompt.onConfirm(); + expect(store.getState().prompt).toBeNull(); expect(store.getState().openWorkspaces.length).toBe(0); }); }); diff --git a/packages/esm-patient-common-lib/src/workspaces/workspaces.ts b/packages/esm-patient-common-lib/src/workspaces/workspaces.ts index 754699d0d9..ee028ee265 100644 --- a/packages/esm-patient-common-lib/src/workspaces/workspaces.ts +++ b/packages/esm-patient-common-lib/src/workspaces/workspaces.ts @@ -167,10 +167,8 @@ export function launchPatientWorkspace(name: string, additionalProps?: object) { const updateStoreWithNewWorkspace = (workspaceToBeAdded: OpenWorkspace, restWorkspaces = null) => { store.setState((state) => { const openWorkspaces = [workspaceToBeAdded, ...(restWorkspaces ?? state.openWorkspaces)]; - let workspaceWindowState = state.workspaceWindowState; - if (workspaceWindowState === 'hidden') { - workspaceWindowState = workspaceToBeAdded.preferredWindowSize === 'maximized' ? 'maximized' : 'normal'; - } + let workspaceWindowState = getUpdatedWorkspaceWindowState(openWorkspaces[0]); + return { ...state, openWorkspaces, @@ -242,6 +240,19 @@ export function cancelPrompt() { export function closeWorkspace(name: string, ignoreChanges: boolean) { const store = getWorkspaceStore(); const promptCheckFcn = getPromptBeforeClosingFcn(name); + + const updateStoreWithClosedWorkspace = () => { + const state = store.getState(); + const newOpenWorkspaces = state.openWorkspaces.filter((w) => w.name != name); + + store.setState({ + ...state, + prompt: null, + openWorkspaces: newOpenWorkspaces, + workspaceWindowState: getUpdatedWorkspaceWindowState(newOpenWorkspaces?.[0]), + }); + }; + if (!ignoreChanges && promptCheckFcn && promptCheckFcn()) { const prompt: Prompt = { title: translateFrom('@openmrs/esm-patient-chart-app', 'unsavedChangesTitleText', 'Unsaved Changes'), @@ -251,15 +262,13 @@ export function closeWorkspace(name: string, ignoreChanges: boolean) { `You have unsaved changes in the side panel. Do you want to discard these changes?`, ), onConfirm: () => { - const state = store.getState(); - store.setState({ ...state, prompt: null, openWorkspaces: state.openWorkspaces.filter((w) => w.name != name) }); + updateStoreWithClosedWorkspace(); }, 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) }); + updateStoreWithClosedWorkspace(); } } @@ -298,6 +307,10 @@ export function updateWorkspaceWindowState(value: WorkspaceWindowState) { store.setState({ ...state, workspaceWindowState: value }); } +function getUpdatedWorkspaceWindowState(workspaceAtTop: OpenWorkspace) { + return workspaceAtTop?.preferredWindowSize ?? 'normal'; +} + /** * @internal * Just for testing.