Skip to content

Commit

Permalink
fix: update interventions and packages hooks to use new API endpoints…
Browse files Browse the repository at this point in the history
… and response structure (#538)
  • Loading branch information
Omoshlawi authored Jan 10, 2025
1 parent a4948cd commit 35009a6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
44 changes: 32 additions & 12 deletions packages/esm-billing-app/src/hooks/useInterventions.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import { FetchResponse, openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import useSWR from 'swr';
import { BillingConfig } from '../config-schema';
import { SHAIntervension } from '../types';
import { interventions } from './benefits.mock';

export const useInterventions = (code: string) => {
const { hieBaseUrl } = useConfig<BillingConfig>();

const url = `${hieBaseUrl}/master/benefit-master/code?searchKey=${code}`;
const { isLoading, error, data } = useSWR<Array<SHAIntervension>>(url, async (key) => {
await new Promise((resolve, reject) => {
setTimeout(resolve, 2000);
const fetcher = (url: string) => {
return openmrsFetch(url, {
method: 'POST',
body: {
searchKeyAndValues: {
// scheme_code: 'UHC',
package_code: code,
},
},
});
return interventions?.filter((inter) => inter.interventionCode.includes(code));
});
};
const url = `${restBaseUrl}/insuranceclaims/claims/interventions/query?code=${code}`;
const { isLoading, error, data } = useSWR<
FetchResponse<{
status: string;
data: Array<{
interventionName: string;
interventionCode: string;
interventionPackage: string;
interventionSubPackage: string;
interventionDescription?: string;
}>;
}>
>(url, fetcher);
return {
isLoading,
interventions: data ?? [],
interventions: (data?.data?.data ?? []).map(
({ interventionCode, interventionName, interventionPackage, interventionSubPackage, interventionDescription }) =>
({
interventionCode,
subCategoryBenefitsPackage: interventionSubPackage,
interventionName,
} as SHAIntervension),
),
error,
};
};
22 changes: 7 additions & 15 deletions packages/esm-billing-app/src/hooks/usePackages.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import { FetchResponse, openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import useSWR from 'swr';
import { BillingConfig } from '../config-schema';
import { Package } from '../types';
import { category } from './benefits.mock';

/**
* Hook that return a list of sha benefits category/packages
* @returns
*/
const usePackages = () => {
const { hieBaseUrl } = useConfig<BillingConfig>();
const url = `${hieBaseUrl}/master/category/all-`;
const url = `${restBaseUrl}/insuranceclaims/claims/benefit-package`;

const { data, isLoading, error } = useSWR<Array<{ id: number; code: string; categoryName: string }>>(
const { data, isLoading, error } = useSWR<FetchResponse<Array<{ code: string; name: string; description: string }>>>(
url,
async () => {
await new Promise((resolve, reject) => {
setTimeout(resolve, 2000);
});
return category;
},
openmrsFetch,
);

return {
isLoading,
packages: (data ?? []).map(
packages: (data?.data ?? []).map(
(category) =>
({
uuid: `${category.id}`,
uuid: `${category.code}`,
packageCode: category.code,
packageName: category.categoryName,
packageName: category.name,
} as Package),
),
error,
Expand Down

0 comments on commit 35009a6

Please sign in to comment.