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

Migration to useQuery: Removed PaginatedList.tsx and Update Dependent Components #10350

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
81 changes: 47 additions & 34 deletions src/CAREUI/misc/PaginatedList.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's delete this component altogether and instead directly implement things in the usages instead. building this component in the first place itself was a mistake 😅. so many things feels abstracted away and not very flexible.

Copy link
Contributor Author

@rajku-dev rajku-dev Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I am working on it.
will do it one by one in this PR itself.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UseQueryResult, useQuery } from "@tanstack/react-query";
import { createContext, useContext, useEffect, useState } from "react";

import CareIcon from "@/CAREUI/icons/CareIcon";
Expand All @@ -6,21 +7,22 @@

import Pagination from "@/components/Common/Pagination";

import { ApiRoute, PaginatedResponse } from "@/Utils/request/types";
import useTanStackQueryInstead, {
QueryOptions,
} from "@/Utils/request/useQuery";
import apiQuery from "@/Utils/request/query";
import {
ApiRoute,
PaginatedResponse,
QueryParams,
} from "@/Utils/request/types";
import { classNames } from "@/Utils/utils";

const DEFAULT_PER_PAGE_LIMIT = 14;

interface PaginatedListContext<TItem>
extends ReturnType<typeof useTanStackQueryInstead<PaginatedResponse<TItem>>> {
type PaginatedListContext<TItem> = UseQueryResult<PaginatedResponse<TItem>> & {
items: TItem[];
perPage: number;
currentPage: number;
setPage: (page: number) => void;
}
};

const context = createContext<PaginatedListContext<object> | null>(null);

Expand All @@ -34,41 +36,52 @@
return ctx as PaginatedListContext<TItem>;
}

interface Props<TItem> extends QueryOptions<PaginatedResponse<TItem>> {
interface Props<TItem> {
route: ApiRoute<PaginatedResponse<TItem>>;
perPage?: number;
initialPage?: number;
onPageChange?: (page: number) => void;
queryCB?: (
query: ReturnType<typeof useTanStackQueryInstead<PaginatedResponse<TItem>>>,
) => void;
queryCB?: (query: UseQueryResult<PaginatedResponse<TItem>>) => void;
pathParams?: Record<string, string>;
queryParams?: QueryParams;
silent?: boolean | ((response: Response) => boolean);
signal?: AbortSignal;
headers?: HeadersInit;
children: (
ctx: PaginatedListContext<TItem>,
query: ReturnType<typeof useTanStackQueryInstead<PaginatedResponse<TItem>>>,
query: UseQueryResult<PaginatedResponse<TItem>>,
) => JSX.Element | JSX.Element[];
}

export default function PaginatedList<TItem extends object>({
children,
route,
perPage = DEFAULT_PER_PAGE_LIMIT,
onPageChange,
initialPage,
pathParams,
queryParams,
queryCB,
...queryOptions
...apiCallOptions
}: Props<TItem>) {
const [currentPage, _setPage] = useState(queryOptions.initialPage ?? 1);
const [currentPage, _setPage] = useState(initialPage ?? 1);

const setPage = (page: number) => {
_setPage(page);
queryOptions.onPageChange?.(page);
onPageChange?.(page);
};

const query = useTanStackQueryInstead(route, {
...queryOptions,
query: {
...queryOptions.query,
limit: perPage,
offset: (currentPage - 1) * perPage,
},
const query = useQuery({
queryKey: [route.path, currentPage, perPage, queryParams],
queryFn: apiQuery(route, {
queryParams: {
limit: perPage,
offset: (currentPage - 1) * perPage,
...queryParams,
},
pathParams,
...apiCallOptions,
}),
});

const items = query.data?.results ?? [];
Expand All @@ -77,7 +90,7 @@
if (queryCB) {
queryCB(query);
}
}, [query]);

Check warning on line 93 in src/CAREUI/misc/PaginatedList.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (1)

React Hook useEffect has a missing dependency: 'queryCB'. Either include it or remove the dependency array. If 'queryCB' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<context.Provider
Expand All @@ -96,9 +109,9 @@
}

const WhenEmpty = <TItem extends object>(props: WhenEmptyProps) => {
const { items, loading } = useContextualized<TItem>();
const { items, isLoading } = useContextualized<TItem>();

if (loading || items.length > 0) {
if (isLoading || items.length > 0) {
return null;
}

Expand All @@ -108,9 +121,9 @@
PaginatedList.WhenEmpty = WhenEmpty;

const WhenLoading = <TItem extends object>(props: WhenEmptyProps) => {
const { loading } = useContextualized<TItem>();
const { isLoading } = useContextualized<TItem>();

if (!loading) {
if (!isLoading) {
return null;
}

Expand All @@ -124,18 +137,18 @@
}

const Refresh = ({ label = "Refresh", ...props }: CommonButtonProps) => {
const { loading, refetch } = useContextualized<object>();
const { isLoading, refetch } = useContextualized<object>();

return (
<Button
variant="secondary"
{...props}
onClick={() => refetch()}
disabled={loading}
disabled={isLoading}
>
<CareIcon
icon="l-sync"
className={classNames("text-lg", loading && "animate-spin")}
className={classNames("text-lg", isLoading && "animate-spin")}
/>
<span>{label}</span>
</Button>
Expand All @@ -152,15 +165,15 @@
}

const Items = <TItem extends object>(props: ItemsProps<TItem>) => {
const { loading, items } = useContextualized<TItem>();
const { isLoading, items } = useContextualized<TItem>();
rajku-dev marked this conversation as resolved.
Show resolved Hide resolved

if (loading || items.length === 0) {
if (isLoading || items.length === 0) {
return null;
}

return (
<ul className={props.className}>
{loading && props.shimmer
{isLoading && props.shimmer
? Array.from({ length: props.shimmerCount ?? 8 }).map((_, i) => (
<li key={i} className="w-full">
{props.shimmer}
Expand All @@ -187,9 +200,9 @@
hideIfSinglePage,
}: PaginatorProps) => {
const { data, perPage, currentPage, setPage } = useContextualized<object>();
const { loading } = useContextualized<TItem>();
const { isLoading } = useContextualized<TItem>();

if (loading) {
if (isLoading) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export default function QuestionnaireResponsesList({
pathParams={{
patientId: patientId,
}}
query={{
queryParams={{
...(encounter && { encounter: encounter.id }),
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const EncounterHistory = (props: PatientProps) => {
return (
<PaginatedList
route={routes.encounter.list}
query={{ patient: patientId }}
queryParams={{ patient: patientId }}
perPage={5}
>
{() => (
Expand Down
Loading