Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

add infinite pagination on notesIterator mobile

ThieryMichele3be61a32b7f
diff --git a/src/components/atomic-crm/contacts/ContactShow.tsx b/src/components/atomic-crm/contacts/ContactShow.tsx
index 100a6c44..40db5731 100644
--- a/src/components/atomic-crm/contacts/ContactShow.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.tsx
@@ -159,6 +159,8 @@ const ContactShowContentMobile = () => {
onError: () => {
/** override to hide notification as error case is handled by NotesIteratorMobile */
},
+ // We want infinite pagination so we need to disable placeHolder data to avoid flicker duplicating previous page before showing the new one
+ placeholderData: null,
} as any // fixme: remove once https://github.com/marmelab/react-admin/pull/11166 is released
}
>
diff --git a/src/components/atomic-crm/notes/NotesIteratorMobile.tsx b/src/components/atomic-crm/notes/NotesIteratorMobile.tsx
index f40b9ed3..72fe2b63 100644
--- a/src/components/atomic-crm/notes/NotesIteratorMobile.tsx
+++ b/src/components/atomic-crm/notes/NotesIteratorMobile.tsx
@@ -1,5 +1,10 @@
import type { Identifier } from "ra-core";
-import { useListContext, useTimeout, WithRecord } from "ra-core";
+import {
+ InfinitePaginationContext,
+ useListContext,
+ useTimeout,
+ WithRecord,
+} from "ra-core";
import { Link } from "react-router";
import { ReferenceField } from "@/components/admin/reference-field";
import { Button } from "@/components/ui/button";
@@ -11,6 +16,8 @@ import { RelativeDate } from "../misc/RelativeDate";
import { Status } from "../misc/Status";
import { SaleName } from "../sales/SaleName";
import type { ContactNote } from "../types";
+import { useEffect, useMemo, useState } from "react";
+import { InfinitePagination } from "../misc/InfinitePagination";
export const NotesIteratorMobile = ({
contactId,
@@ -19,7 +26,24 @@ export const NotesIteratorMobile = ({
contactId: Identifier;
showStatus?: boolean;
}) => {
- const { data, error, isPending, refetch } = useListContext();
+ const { data, error, isPending, refetch, setPage, page, total } =
+ useListContext();
+
+ const [allPages, setAllPages] = useState<Record<number, any[]>>({});
+
+ useEffect(() => {
+ if (!data) {
+ return;
+ }
+ setAllPages((prev) => ({ ...prev, [page]: data }));
+ }, [page, setAllPages, data]);
+
+ const allLoadedData = useMemo(() => {
+ return Object.entries(allPages)
+ .sort(([pageA], [pageB]) => Number(pageA) - Number(pageB))
+ .flatMap(([, pageData]) => pageData);
+ }, [allPages]);
+
const oneSecondHasPassed = useTimeout(1000);
if (isPending) {
if (!oneSecondHasPassed) {
@@ -39,7 +63,7 @@ export const NotesIteratorMobile = ({
</div>
);
}
- if (error && !data) {
+ if (error && !allLoadedData.length) {
return (
<div className="p-4">
<div className="text-center text-muted-foreground mb-4">
@@ -60,16 +84,34 @@ export const NotesIteratorMobile = ({
}
return (
- <div className="divide-y">
- {data?.map((note) => (
- <NoteMobile
- key={note.id}
- note={note}
- contactId={contactId}
- showStatus={showStatus}
- />
- ))}
- </div>
+ <InfinitePaginationContext.Provider
+ value={{
+ fetchNextPage: async (): Promise<any> => {
+ setPage(page + 1);
+ },
+ hasNextPage: allLoadedData.length < total!,
+ isFetchingNextPage: isPending,
+ fetchPreviousPage: async (): Promise<any> => {
+ setPage(page - 1);
+ },
+ hasPreviousPage: page > 1,
+ isFetchingPreviousPage: false,
+ }}
+ >
+ <div>
+ <div className="divide-y">
+ {allLoadedData?.map((note) => (
+ <NoteMobile
+ key={note.id}
+ note={note}
+ contactId={contactId}
+ showStatus={showStatus}
+ />
+ ))}
+ </div>
+ <InfinitePagination />
+ </div>
+ </InfinitePaginationContext.Provider>
);
};