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

Ensure proper handling of pagination for Billable Services #67

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 4 additions & 3 deletions src/billable-services/billable-service.resource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import useSWR from 'swr';
import { type OpenmrsResource, openmrsFetch, restBaseUrl, useConfig } from '@openmrs/esm-framework';
import { type OpenmrsResource, openmrsFetch, restBaseUrl, useOpenmrsFetchAll, useConfig } from '@openmrs/esm-framework';
import { type ServiceConcept } from '../types';
import { apiBasePath } from '../constants';
import { type BillableService } from '../types/index';

type ResponseObject = {
results: Array<OpenmrsResource>;
Expand All @@ -10,10 +11,10 @@ type ResponseObject = {
export const useBillableServices = () => {
const url = `${apiBasePath}billableService?v=custom:(uuid,name,shortName,serviceStatus,serviceType:(display),servicePrices:(uuid,name,price))`;

const { data, isLoading, isValidating, error, mutate } = useSWR<{ data: ResponseObject }>(url, openmrsFetch);
const { data, isLoading, isValidating, error, mutate } = useOpenmrsFetchAll<BillableService[]>(url);

return {
billableServices: data?.data.results ?? [],
billableServices: data ?? [],
isLoading,
isValidating,
error,
Expand Down
13 changes: 8 additions & 5 deletions src/billable-services/billable-services.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useLayoutType, isDesktop, useConfig, usePagination, ErrorState, navigat
import { EmptyState } from '@openmrs/esm-patient-common-lib';
import { useBillableServices } from './billable-service.resource';
import styles from './billable-services.scss';
import { type BillableService } from '../types/index';

const BillableServices = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -68,21 +69,23 @@ const BillableServices = () => {
navigate({ to: window.getOpenmrsSpaBase() + 'billable-services/add-service' });
}, []);

const searchResults = useMemo(() => {
if (billableServices !== undefined && billableServices.length > 0) {
const searchResults: BillableService[] = useMemo(() => {
const flatBillableServices = Array.isArray(billableServices) ? billableServices.flat() : billableServices;

if (flatBillableServices !== undefined && flatBillableServices.length > 0) {
if (searchString && searchString.trim() !== '') {
const search = searchString.toLowerCase();
return billableServices?.filter((service) =>
return flatBillableServices.filter((service: BillableService) =>
Object.entries(service).some(([header, value]) => {
return header === 'uuid' ? false : `${value}`.toLowerCase().includes(search);
}),
);
}
}
return billableServices;
return flatBillableServices;
}, [searchString, billableServices]);

const { paginated, goTo, results, currentPage } = usePagination(searchResults, pageSize);
const { paginated, goTo, results, currentPage } = usePagination<BillableService>(searchResults, pageSize);

let rowData = [];
if (results) {
Expand Down
14 changes: 14 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,17 @@ export type ServicePrice = {
price: string;
uuid: string;
};

export interface BillableService {
uuid: string;
name: string;
shortName: string;
serviceStatus: string;
serviceType?: {
display: string;
};
servicePrices: Array<{
name: string;
price: number;
}>;
}
Loading
Loading