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

O3-1023 Should be possible to launch patient chart with workspace open #546

Merged
merged 1 commit into from
Feb 10, 2022
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useMemo } from 'react';
import { ExtensionSlot, useSessionUser } from '@openmrs/esm-framework';
import { closeAllWorkspaces, useWorkspaceWindowSize, WorkspaceWindowState } from '@openmrs/esm-patient-common-lib';
import { changeWorkspaceContext, useWorkspaceWindowSize, WorkspaceWindowState } from '@openmrs/esm-patient-common-lib';
import { RouteComponentProps } from 'react-router-dom';
import { useOfflineVisitForPatient, usePatientOrOfflineRegisteredPatient } from '../offline';
import ChartReview from '../view-components/chart-review.component';
Expand All @@ -24,7 +24,7 @@ const PatientChart: React.FC<RouteComponentProps<PatientChartParams>> = ({ match
const { windowSize, active } = useWorkspaceWindowSize();

useEffect(() => {
closeAllWorkspaces();
changeWorkspaceContext(patientUuid);
}, [patientUuid]);

useOfflineVisitForPatient(patientUuid, sessionUser?.sessionLocation?.uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
OpenWorkspace,
WorkspaceWindowState,
} from '@openmrs/esm-patient-common-lib';
import { WorkspaceStoreState } from '.';

export interface WorkspacesInfo {
active: boolean;
Expand All @@ -18,10 +19,12 @@ export function useWorkspaces(): WorkspacesInfo {
const [workspaceNeedingConfirmationToOpen, setWorkspaceNeedingConfirmationToOpen] = useState<OpenWorkspace>(null);

useEffect(() => {
getWorkspaceStore().subscribe((state) => {
function update(state: WorkspaceStoreState) {
setWorkspaces(state.openWorkspaces.map((w) => ({ ...w, closeWorkspace: () => closeWorkspace(w.name) })));
setWorkspaceNeedingConfirmationToOpen(state.workspaceNeedingConfirmationToOpen);
});
}
update(getWorkspaceStore().getState());
getWorkspaceStore().subscribe(update);
}, []);

const windowState = useMemo(() => {
Expand Down
20 changes: 15 additions & 5 deletions packages/esm-patient-common-lib/src/workspaces/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ExtensionRegistration, getExtensionRegistration, getGlobalStore, transl
import { WorkspaceWindowState } from '..';

export interface WorkspaceStoreState {
patientUuid: string | null;
openWorkspaces: Array<OpenWorkspace>;
workspaceNeedingConfirmationToOpen: OpenWorkspace | null;
}
Expand Down Expand Up @@ -134,15 +135,24 @@ export function closeWorkspace(name: string) {

export function closeAllWorkspaces() {
const store = getWorkspaceStore();
store.setState({ openWorkspaces: [] });
const state = store.getState();
store.setState({ ...state, openWorkspaces: [] });
}

export interface WorkspaceParcel {
unmount: () => void;
update: (props) => Promise<any>;
/**
* The set of workspaces is specific to a particular patient. This function
* should be used when setting up workspaces for a new patient. If the current
* workspace data is for a different patient, the workspace state is cleared.
*/
export function changeWorkspaceContext(patientUuid) {
const store = getWorkspaceStore();
const state = store.getState();
if (state.patientUuid != patientUuid) {
store.setState({ patientUuid, openWorkspaces: [], workspaceNeedingConfirmationToOpen: null });
}
}

const initialState = { openWorkspaces: [], workspaceNeedingConfirmationToOpen: null };
const initialState = { patientUuid: null, openWorkspaces: [], workspaceNeedingConfirmationToOpen: null };
export function getWorkspaceStore() {
return getGlobalStore<WorkspaceStoreState>('workspace', initialState);
}
Expand Down