Skip to content

Commit

Permalink
(fix) Workspace window should have the size of the preferred window s…
Browse files Browse the repository at this point in the history
…tate (#1444)
  • Loading branch information
vasharma05 authored Dec 6, 2023
1 parent a8bcf2b commit 9f15df7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const WorkspaceWindow: React.FC<ContextWorkspaceParams> = () => {
<ExtensionSlot name={patientChartWorkspaceHeaderSlot} />
{isDesktop(layout) && (
<>
{canMaximize && (
{(canMaximize || maximized) && (
<HeaderGlobalAction
align="bottom"
label={maximized ? t('minimize', 'Minimize') : t('maximize', 'Maximize')}
Expand Down
42 changes: 42 additions & 0 deletions packages/esm-patient-common-lib/src/workspaces/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,47 @@ describe('workspace system', () => {
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', () => {
Expand Down Expand Up @@ -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);
});
});
29 changes: 21 additions & 8 deletions packages/esm-patient-common-lib/src/workspaces/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9f15df7

Please sign in to comment.