Skip to content

Commit d478cd7

Browse files
committed
Add max-pages example
1 parent 8e813c0 commit d478cd7

File tree

4 files changed

+133
-6
lines changed

4 files changed

+133
-6
lines changed

examples/query/react/infinite-queries/src/App.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
InfiniteScrollExample,
77
InfiniteScrollAbout,
88
} from "./features/infinite-scroll/InfiniteScrollExample"
9+
import { InfiniteScrollMaxPagesExample } from "./features/max-pages/InfiniteScrollMaxExample"
910

1011
const Menu = () => {
1112
return (
@@ -18,6 +19,9 @@ const Menu = () => {
1819
<li>
1920
<Link to="/infinite-scroll">Infinite Scroll</Link>
2021
</li>
22+
<li>
23+
<Link to="/infinite-scroll-max">Infinite Scroll + max pages</Link>
24+
</li>
2125
</ul>
2226
</div>
2327
)
@@ -27,7 +31,7 @@ const App = () => {
2731
return (
2832
<BrowserRouter>
2933
<div className="App">
30-
<h1>Infinite Query Examples</h1>
34+
<h1>RTKQ Infinite Query Example Showcase</h1>
3135
<Routes>
3236
<Route path="/" element={<Menu />} />
3337
<Route path="/pagination" element={<PaginationExample />} />
@@ -36,6 +40,10 @@ const App = () => {
3640
path="/infinite-scroll/about"
3741
element={<InfiniteScrollAbout />}
3842
/>
43+
<Route
44+
path="/infinite-scroll-max"
45+
element={<InfiniteScrollMaxPagesExample />}
46+
/>
3947
</Routes>
4048
</div>
4149
</BrowserRouter>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React from "react"
2+
import { apiWithInfiniteScrollMax } from "./infiniteScrollApi"
3+
4+
export const InfiniteScrollMaxPagesExample = () => {
5+
const {
6+
data,
7+
error,
8+
fetchNextPage,
9+
fetchPreviousPage,
10+
// hasNextPage,
11+
isFetchingNextPage,
12+
isFetching,
13+
isError,
14+
} =
15+
apiWithInfiniteScrollMax.endpoints.getProjectsCursorMax.useInfiniteQuery(
16+
"projects",
17+
)
18+
19+
// TODO This should be built in to RTKQ
20+
const hasNextPage = data?.pages[data.pages.length - 1].nextId !== null
21+
const hasPreviousPage = data?.pages[0].previousId !== null
22+
23+
return (
24+
<div>
25+
<h2>Infinite Query with max pages</h2>
26+
<h3>3 pages max</h3>
27+
{isFetching ? (
28+
<p>Loading...</p>
29+
) : isError ? (
30+
<span>Error: {error.message}</span>
31+
) : (
32+
<>
33+
<div>
34+
<button
35+
onClick={() => fetchPreviousPage()}
36+
disabled={!hasPreviousPage /* || isFetchingPreviousPage*/}
37+
>
38+
{
39+
/*isFetchingPreviousPage
40+
? 'Loading more...'
41+
:*/ hasPreviousPage ? "Load Older" : "Nothing more to load"
42+
}
43+
</button>
44+
</div>
45+
{data?.pages.map(page => (
46+
<React.Fragment key={page.nextId}>
47+
{page.projects.map(project => (
48+
<p
49+
style={{
50+
border: "1px solid gray",
51+
borderRadius: "5px",
52+
padding: "8px",
53+
fontSize: "14px",
54+
background: `hsla(${project.id * 30}, 60%, 80%, 1)`,
55+
}}
56+
key={project.id}
57+
>
58+
{project.name}
59+
</p>
60+
))}
61+
</React.Fragment>
62+
))}
63+
<div>
64+
<button
65+
onClick={() => fetchNextPage()}
66+
disabled={!hasNextPage /* || isFetchingNextPage*/}
67+
>
68+
{isFetchingNextPage
69+
? "Loading more..."
70+
: hasNextPage
71+
? "Load Newer"
72+
: "Nothing more to load"}
73+
</button>
74+
</div>
75+
<div>
76+
{isFetching && !isFetchingNextPage
77+
? "Background Updating..."
78+
: null}
79+
</div>
80+
</>
81+
)}
82+
<hr />
83+
</div>
84+
)
85+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { baseApi } from "../baseApi"
2+
3+
type Project = {
4+
id: number
5+
name: string
6+
}
7+
8+
type ProjectsPageCursor = {
9+
projects: Project[]
10+
nextId: number | null
11+
previousId: number | null
12+
}
13+
14+
export const apiWithInfiniteScrollMax = baseApi.injectEndpoints({
15+
endpoints: build => ({
16+
getProjectsCursorMax: build.infiniteQuery<
17+
ProjectsPageCursor,
18+
string,
19+
number
20+
>({
21+
query: page => `https://example.com/api/projectsCursor?cursor=${page}`,
22+
infiniteQueryOptions: {
23+
initialPageParam: 0,
24+
maxPages: 3,
25+
getPreviousPageParam: firstPage => firstPage.previousId ?? undefined,
26+
getNextPageParam: lastPage => lastPage.nextId ?? undefined,
27+
},
28+
}),
29+
}),
30+
})
31+
32+
// export const { useGetProjectsQuery } = apiWithPagination

examples/query/react/infinite-queries/src/mocks/handlers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ export const handlers = [
5353
}
5454
})
5555

56-
const nextId =
57-
cursor < totalItems - pageSize
58-
? projects[projects.length - 1].id + 1
59-
: null
56+
const hasNext = cursor < totalItems - pageSize
57+
const nextId = hasNext ? projects[projects.length - 1].id + 1 : null
58+
59+
// Prevent negative cursors
60+
const hasPrevious = cursor > -(totalItems - pageSize)
61+
const maybePrevCursor = projects[0].id - pageSize
6062
const previousId =
61-
cursor > -(totalItems - pageSize) ? projects[0].id - pageSize : null
63+
hasPrevious && maybePrevCursor >= 0 ? maybePrevCursor : null
6264

6365
return HttpResponse.json({
6466
projects,

0 commit comments

Comments
 (0)