Skip to content

Commit

Permalink
Introduce patient chart store for handling patient chart related state
Browse files Browse the repository at this point in the history
  • Loading branch information
vasharma05 committed Oct 30, 2024
1 parent f9a6f9f commit 40bc433
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Loader from '../loader/loader.component';
import styles from './patient-chart.scss';
import VisitHeader from '../visit-header/visit-header.component';
import SideMenuPanel from '../side-nav/side-menu.component';
import { getPatientChartStore } from '@openmrs/esm-patient-common-lib';

const PatientChart: React.FC = () => {
const { patientUuid, view: encodedView } = useParams();
Expand All @@ -41,6 +42,17 @@ const PatientChart: React.FC = () => {
};
}, [patientUuid]);

useEffect(() => {
getPatientChartStore().setState({
patientUuid,
});
return () => {
getPatientChartStore().setState({
patientUuid: null,
});
};
}, [patientUuid]);

const leftNavBasePath = useMemo(() => spaBasePath.replace(':patientUuid', patientUuid), [patientUuid]);
useEffect(() => {
setLeftNav({ name: 'patient-chart-dashboard-slot', basePath: leftNavBasePath });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getPatientChartStore } from './store/patient-chart-store';

export function getPatientUuidFromUrl(): string {
const match = /\/patient\/([a-zA-Z0-9\-]+)\/?/.exec(location.pathname);
return match && match[1];
const patientUuidFromUrl = match && match[1];
return patientUuidFromUrl || getPatientChartStore().getState().patientUuid;
}
1 change: 1 addition & 0 deletions packages/esm-patient-common-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export * from './types';
export * from './useAllowedFileExtensions';
export * from './useSystemVisitSetting';
export * from './workspaces';
export * from './store/patient-chart-store';
19 changes: 19 additions & 0 deletions packages/esm-patient-common-lib/src/store/patient-chart-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createGlobalStore, useStore } from '@openmrs/esm-framework';

export interface PatientChartStore {
patientUuid: string;
}

const patientChartStoreName = 'patient-chart-global-store';

const patientChartStore = createGlobalStore<PatientChartStore>(patientChartStoreName, {
patientUuid: '',
});

export function getPatientChartStore() {
return patientChartStore;
}

export function usePatientChartStore() {
return useStore(patientChartStore);
}
3 changes: 2 additions & 1 deletion packages/esm-patient-common-lib/src/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useSystemVisitSetting } from './useSystemVisitSetting';
import { useVisitOrOfflineVisit } from './offline/visit';
import { useCallback } from 'react';
import { launchStartVisitPrompt } from './launchStartVisitPrompt';
import { usePatientChartStore } from './store/patient-chart-store';

export interface DefaultPatientWorkspaceProps extends DefaultWorkspaceProps {
patientUuid: string;
Expand Down Expand Up @@ -42,7 +43,7 @@ export function launchPatientChartWithWorkspaceOpen({
}

export function useLaunchWorkspaceRequiringVisit<T extends object>(workspaceName: string) {
const { patientUuid } = usePatient();
const { patientUuid } = usePatientChartStore();
const { systemVisitEnabled } = useSystemVisitSetting();
const { currentVisit } = useVisitOrOfflineVisit(patientUuid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type {
} from '../types';
import { useRequireOutpatientQuantity } from '../api';
import styles from './drug-order-form.scss';
import { usePatientChartStore } from '@openmrs/esm-patient-common-lib';

export interface DrugOrderFormProps {
initialOrderBasketItem: DrugOrderBasketItem;
Expand Down Expand Up @@ -354,7 +355,8 @@ export function DrugOrderForm({ initialOrderBasketItem, onSave, onCancel, prompt
}, []);

const [showStickyMedicationHeader, setShowMedicationHeader] = useState(false);
const { patient, isLoading: isLoadingPatientDetails } = usePatient();
const { patientUuid } = usePatientChartStore();
const { patient, isLoading: isLoadingPatientDetails } = usePatient(patientUuid);
const patientName = patient ? getPatientName(patient) : '';
const { maxDispenseDurationInDays } = useConfig<ConfigObject>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from './drug-search.resource';
import type { DrugOrderBasketItem } from '../../types';
import styles from './order-basket-search-results.scss';
import { usePatientChartStore } from '@openmrs/esm-patient-common-lib';

export interface OrderBasketSearchResultsProps {
searchTerm: string;
Expand Down Expand Up @@ -114,8 +115,8 @@ export default function OrderBasketSearchResults({
const DrugSearchResultItem: React.FC<DrugSearchResultItemProps> = ({ drug, openOrderForm }) => {
const isTablet = useLayoutType() === 'tablet';
const { orders, setOrders } = useOrderBasket<DrugOrderBasketItem>('medications', prepMedicationOrderPostData);
const patient = usePatient();
const { data: activeOrders, isLoading: isLoadingActiveOrders } = usePatientOrders(patient.patientUuid, 'ACTIVE');
const { patientUuid } = usePatientChartStore();
const { data: activeOrders, isLoading: isLoadingActiveOrders } = usePatientOrders(patientUuid, 'ACTIVE');
const drugAlreadyInBasket = useMemo(
() => orders?.some((order) => ordersEqual(order, getTemplateOrderBasketItem(drug))),
[orders, drug],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
type OrderBasketItem,
type LabOrderBasketItem,
launchPatientWorkspace,
usePatientChartStore,
} from '@openmrs/esm-patient-common-lib';
import { LabOrderForm } from './lab-order-form.component';
import { TestTypeSearch } from './test-type-search.component';
Expand All @@ -37,7 +38,8 @@ export default function AddLabOrderWorkspace({
}: AddLabOrderWorkspace) {
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
const { patient, isLoading: isLoadingPatient, patientUuid } = usePatient();
const { patientUuid } = usePatientChartStore();
const { patient, isLoading: isLoadingPatient } = usePatient(patientUuid);
const [currentLabOrder, setCurrentLabOrder] = useState(initialOrder as LabOrderBasketItem);

const patientName = patient ? getPatientName(patient) : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import useSWR from 'swr';
import useSWRInfinite from 'swr/infinite';
import { usePatient, openmrsFetch, restBaseUrl, type FetchResponse } from '@openmrs/esm-framework';
import { assessValue, exist } from '../loadPatientTestData/helpers';
import { usePatientChartStore } from '@openmrs/esm-patient-common-lib';

export const getName = (prefix: string | undefined, name: string) => {
return prefix ? `${prefix}-${name}` : name;
Expand Down Expand Up @@ -43,7 +44,7 @@ const augmentObstreeData = (node: ObsTreeNode, prefix: string | undefined) => {
};

const useGetObstreeData = (conceptUuid: string) => {
const { patientUuid } = usePatient();
const { patientUuid } = usePatientChartStore();
const response = useSWR<FetchResponse<ObsTreeNode>, Error>(
`${restBaseUrl}/obstree?patient=${patientUuid}&concept=${conceptUuid}`,
openmrsFetch,
Expand All @@ -65,7 +66,7 @@ const useGetObstreeData = (conceptUuid: string) => {
};

const useGetManyObstreeData = (uuidArray: Array<string>) => {
const { patientUuid } = usePatient();
const { patientUuid } = usePatientChartStore();
const getObstreeUrl = (index: number) => {
if (index < uuidArray.length && patientUuid) {
return `${restBaseUrl}/obstree?patient=${patientUuid}&concept=${uuidArray[index]}`;
Expand Down

0 comments on commit 40bc433

Please sign in to comment.