Skip to content

Commit

Permalink
Remove the dependency of gettting patient uuid from the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
vasharma05 committed Oct 29, 2024
1 parent f8937d4 commit aa5c953
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function AddDrugOrderWorkspace({
closeWorkspace,
closeWorkspaceWithSavedChanges,
promptBeforeClosing,
patientUuid,
}: AddDrugOrderWorkspace) {
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
Expand Down Expand Up @@ -89,7 +90,7 @@ export default function AddDrugOrderWorkspace({
</Button>
</div>
)}
<DrugSearch openOrderForm={openOrderForm} />
<DrugSearch patientUuid={patientUuid} openOrderForm={openOrderForm} />
</>
);
} else {
Expand All @@ -99,6 +100,7 @@ export default function AddDrugOrderWorkspace({
onSave={saveDrugOrder}
onCancel={cancelDrugOrder}
promptBeforeClosing={promptBeforeClosing}
patientUuid={patientUuid}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface DrugOrderFormProps {
onSave: (finalizedOrder: DrugOrderBasketItem) => void;
onCancel: () => void;
promptBeforeClosing: (testFcn: () => boolean) => void;
patientUuid: string;
}

const createMedicationOrderFormSchema = (requireOutpatientQuantity: boolean, t: TFunction) => {
Expand Down Expand Up @@ -215,7 +216,13 @@ function InputWrapper({ children }) {
);
}

export function DrugOrderForm({ initialOrderBasketItem, onSave, onCancel, promptBeforeClosing }: DrugOrderFormProps) {
export function DrugOrderForm({
initialOrderBasketItem,
onSave,
onCancel,
promptBeforeClosing,
patientUuid,
}: DrugOrderFormProps) {
const { t } = useTranslation();
const config = useConfig<ConfigObject>();
const isTablet = useLayoutType() === 'tablet';
Expand Down Expand Up @@ -354,7 +361,7 @@ export function DrugOrderForm({ initialOrderBasketItem, onSave, onCancel, prompt
}, []);

const [showStickyMedicationHeader, setShowMedicationHeader] = useState(false);
const { patient, isLoading: isLoadingPatientDetails } = usePatient();
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 @@ -10,9 +10,10 @@ import styles from './order-basket-search.scss';

export interface DrugSearchProps {
openOrderForm: (searchResult: DrugOrderBasketItem) => void;
patientUuid: string;
}

export default function DrugSearch({ openOrderForm }: DrugSearchProps) {
export default function DrugSearch({ openOrderForm, patientUuid }: DrugSearchProps) {
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
const [searchTerm, setSearchTerm] = useState('');
Expand Down Expand Up @@ -51,6 +52,7 @@ export default function DrugSearch({ openOrderForm }: DrugSearchProps) {
searchTerm={debouncedSearchTerm}
openOrderForm={openOrderForm}
focusAndClearSearchInput={focusAndClearSearchInput}
patientUuid={patientUuid}
/>
{isTablet && (
<div className={styles.separatorContainer}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ export interface OrderBasketSearchResultsProps {
searchTerm: string;
openOrderForm: (searchResult: DrugOrderBasketItem) => void;
focusAndClearSearchInput: () => void;
patientUuid: string;
}

interface DrugSearchResultItemProps {
drug: DrugSearchResult;
openOrderForm: (searchResult: DrugOrderBasketItem) => void;
patientUuid: string;
}

export default function OrderBasketSearchResults({
searchTerm,
openOrderForm,
focusAndClearSearchInput,
patientUuid,
}: OrderBasketSearchResultsProps) {
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
Expand Down Expand Up @@ -105,17 +108,18 @@ export default function OrderBasketSearchResults({
</Button>
</div>
<div className={styles.resultsContainer}>
{drugs?.map((drug) => <DrugSearchResultItem key={drug.uuid} drug={drug} openOrderForm={openOrderForm} />)}
{drugs?.map((drug) => (
<DrugSearchResultItem key={drug.uuid} drug={drug} openOrderForm={openOrderForm} patientUuid={patientUuid} />
))}
</div>
</div>
);
}

const DrugSearchResultItem: React.FC<DrugSearchResultItemProps> = ({ drug, openOrderForm }) => {
const DrugSearchResultItem: React.FC<DrugSearchResultItemProps> = ({ drug, openOrderForm, patientUuid }) => {
const isTablet = useLayoutType() === 'tablet';
const { orders, setOrders } = useOrderBasket<DrugOrderBasketItem>('medications', prepMedicationOrderPostData);
const patient = usePatient();
const { data: activeOrders, isLoading: isLoadingActiveOrders } = usePatientOrders(patient.patientUuid, 'ACTIVE');
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 @@ -34,10 +34,11 @@ export default function AddLabOrderWorkspace({
closeWorkspace,
closeWorkspaceWithSavedChanges,
promptBeforeClosing,
patientUuid,
}: AddLabOrderWorkspace) {
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
const { patient, isLoading: isLoadingPatient, patientUuid } = usePatient();
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 @@ -42,8 +42,7 @@ const augmentObstreeData = (node: ObsTreeNode, prefix: string | undefined) => {
return { ...outData } as ObsTreeNode;
};

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

const useGetManyObstreeData = (uuidArray: Array<string>) => {
const { patientUuid } = usePatient();
const useGetManyObstreeData = (patientUuid: string, uuidArray: Array<string>) => {
const getObstreeUrl = (index: number) => {
if (index < uuidArray.length && patientUuid) {
return `${restBaseUrl}/obstree?patient=${patientUuid}&concept=${uuidArray[index]}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const PanelView: React.FC<PanelViewProps> = ({ expanded, testUuid, basePath, typ
const layout = useLayoutType();
const isTablet = layout === 'tablet';
const trendlineView = testUuid && type === 'trendline';
const { panels, isLoading, groupedObservations } = usePanelData();
const { panels, isLoading, groupedObservations } = usePanelData(patientUuid);
const [searchTerm, setSearchTerm] = useState('');
const [activePanel, setActivePanel] = useState<ObsRecord>(null);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo } from 'react';
import { type FetchResponse, openmrsFetch, usePatient, restBaseUrl } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import useSWRInfinite from 'swr/infinite';
import { extractMetaInformation, getConceptUuid } from './helper';
import {
Expand All @@ -10,8 +10,7 @@ import {
type ObsRecord,
} from '../../types';

export function useObservations() {
const { patientUuid } = usePatient();
export function useObservations(patientUuid: string) {
const getUrl = useCallback(
(pageIndex: number, prevPageData: FetchResponse<FhirResponse<FHIRObservationResource>>) => {
if (prevPageData && !prevPageData?.data?.link.some(({ relation }) => relation === 'next')) {
Expand Down Expand Up @@ -93,8 +92,12 @@ function useConcepts(conceptUuids: Array<string>) {
return results;
}

export default function usePanelData() {
const { observations: fhirObservations, conceptUuids, isLoading: isLoadingObservations } = useObservations();
export default function usePanelData(patientUuid: string) {
const {
observations: fhirObservations,
conceptUuids,
isLoading: isLoadingObservations,
} = useObservations(patientUuid);
const { concepts } = useConcepts(conceptUuids);

const conceptData: Record<string, ConceptMeta> = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function PrintModal({ patientUuid, closeDialog }) {
const { t } = useTranslation();
const isTablet = useLayoutType() === 'tablet';
const session = useSession();
const { panels } = usePanelData();
const { panels } = usePanelData(patientUuid);
const printContainerRef = useRef(null);
const [selectedFromDate, setSelectedFromDate] = useState(null);
const [selectedToDate, setSelectedToDate] = useState(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const RoutedResultsViewer: React.FC<ResultsViewerProps> = ({ basePath, patientUu
const { t } = useTranslation();
const config = useConfig<ConfigObject>();
const conceptUuids = config.resultsViewerConcepts.map((concept) => concept.conceptUuid) ?? [];
const { roots, isLoading, error } = useGetManyObstreeData(conceptUuids);
const { roots, isLoading, error } = useGetManyObstreeData(patientUuid, conceptUuids);

if (error) {
return <ErrorState error={error} headerTitle={t('dataLoadError', 'Data Load Error')} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const TreeViewWrapper: React.FC<TreeViewWrapperProps> = (props) => {
const { t } = useTranslation();
const config = useConfig();
const conceptUuids = config?.resultsViewerConcepts?.map((c) => c.conceptUuid) ?? [];
const { roots, isLoading, error } = useGetManyObstreeData(conceptUuids);
const { roots, isLoading, error } = useGetManyObstreeData(props.patientUuid, conceptUuids);

if (error) return <ErrorState error={error} headerTitle={t('dataLoadError', 'Data load error')} />;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const TreeView: React.FC<TreeViewProps> = ({ patientUuid, basePath, testUuid, is
const { t } = useTranslation();

const { timelineData, resetTree, someChecked } = useContext(FilterContext);
const { panels, isLoading: isLoadingPanelData, groupedObservations } = usePanelData();
const { panels, isLoading: isLoadingPanelData, groupedObservations } = usePanelData(patientUuid);

if (tablet) {
return (
Expand Down

0 comments on commit aa5c953

Please sign in to comment.