Skip to content

Commit 8564893

Browse files
committed
docs: Add pagination to getting-started page
1 parent 63d4b65 commit 8564893

File tree

6 files changed

+175
-124
lines changed

6 files changed

+175
-124
lines changed

docs/core/getting-started/data-dependency.md

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@ which guarantees data like [await](https://developer.mozilla.org/en-US/docs/Web/
2828
```ts title="Resources" collapsed
2929
import { Entity, createResource } from '@data-client/rest';
3030

31-
export class Post extends Entity {
32-
id = 0;
33-
userId = 0;
34-
title = '';
35-
body = '';
36-
37-
pk() {
38-
return this.id?.toString();
39-
}
40-
static key = 'Post';
41-
}
42-
export const PostResource = createResource({
43-
path: '/posts/:id',
44-
schema: Post,
45-
});
46-
4731
export class User extends Entity {
4832
id = 0;
4933
name = '';
@@ -57,7 +41,7 @@ export class User extends Entity {
5741
}
5842

5943
pk() {
60-
return `${this.id}`;
44+
return this.id;
6145
}
6246
static key = 'User';
6347
}
@@ -66,22 +50,42 @@ export const UserResource = createResource({
6650
path: '/users/:id',
6751
schema: User,
6852
});
53+
54+
export class Post extends Entity {
55+
id = 0;
56+
author = User.fromJS();
57+
title = '';
58+
body = '';
59+
60+
pk() {
61+
return this.id;
62+
}
63+
static key = 'Post';
64+
65+
static schema = {
66+
author: User,
67+
};
68+
}
69+
export const PostResource = createResource({
70+
path: '/posts/:id',
71+
schema: Post,
72+
paginationField: 'page',
73+
});
6974
```
7075

71-
```tsx title="PostDetail" {5-6} collapsed
76+
```tsx title="PostDetail" {5} collapsed
7277
import { useSuspense } from '@data-client/react';
73-
import { UserResource, PostResource } from './Resources';
78+
import { PostResource } from './Resources';
7479

7580
export default function PostDetail({ setRoute, id }) {
7681
const post = useSuspense(PostResource.get, { id });
77-
const author = useSuspense(UserResource.get, { id: post.userId });
7882
return (
7983
<div>
8084
<header>
8185
<div className="listItem spaced">
8286
<div className="author">
83-
<Avatar src={author.profileImage} />
84-
<small>{author.name}</small>
87+
<Avatar src={post.author.profileImage} />
88+
<small>{post.author.name}</small>
8589
</div>
8690
<h4>{post.title}</h4>
8791
</div>
@@ -101,15 +105,13 @@ export default function PostDetail({ setRoute, id }) {
101105
}
102106
```
103107

104-
```tsx title="PostItem" {5} collapsed
105-
import { useSuspense } from '@data-client/react';
106-
import { UserResource, type Post } from './Resources';
108+
```tsx title="PostItem" collapsed
109+
import { type Post } from './Resources';
107110

108111
export default function PostItem({ post, setRoute }: Props) {
109-
const author = useSuspense(UserResource.get, { id: post.userId });
110112
return (
111113
<div className="listItem spaced">
112-
<Avatar src={author.profileImage} />
114+
<Avatar src={post.author.profileImage} />
113115
<div>
114116
<h4>
115117
<a
@@ -122,7 +124,7 @@ export default function PostItem({ post, setRoute }: Props) {
122124
{post.title}
123125
</a>
124126
</h4>
125-
<small>by {author.name}</small>
127+
<small>by {post.author.name}</small>
126128
</div>
127129
</div>
128130
);
@@ -152,6 +154,8 @@ export default function PostList({ setRoute }) {
152154
```
153155

154156
```tsx title="Navigation" collapsed
157+
import { useController, useLoading } from '@data-client/react';
158+
import { PostResource } from './Resources';
155159
import PostList from './PostList';
156160
import PostDetail from './PostDetail';
157161

@@ -160,7 +164,21 @@ function Navigation() {
160164
if (route.startsWith('detail'))
161165
return <PostDetail setRoute={setRoute} id={route.split('/')[1]} />;
162166

163-
return <PostList setRoute={setRoute} />;
167+
return <><PostList setRoute={setRoute} /><LoadMore /></>;
168+
}
169+
170+
function LoadMore() {
171+
const ctrl = useController();
172+
const posts = useQuery(PostResource.getList.schema);
173+
const [nextPage, isPending] = useLoading(
174+
() => ctrl.fetch(PostResource.getList.getPage, { page: 2 }),
175+
);
176+
if (posts?.length % 3 !== 0) return null;
177+
return (
178+
<center>
179+
<button onClick={nextPage}>{isPending ? "..." : 'Load more'}</button>
180+
</center>
181+
)
164182
}
165183
render(<Navigation />);
166184
```

docs/core/shared/_VoteDemo.mdx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,43 @@ import HooksPlayground from '@site/src/components/HooksPlayground';
44
<HooksPlayground fixtures={postFixtures} getInitialInterceptorData={() => ({votes: {}})} row defaultTab={props.defaultTab}>
55

66
```ts title="Post" collapsed
7-
import { Entity } from '@data-client/rest';
7+
import { Entity, schema } from '@data-client/rest';
88

99
export class Post extends Entity {
1010
id = 0;
11-
userId = 0;
11+
author = { id: 0 };
1212
title = '';
1313
body = '';
1414
votes = 0;
1515

1616
pk() {
17-
return this.id?.toString();
17+
return this.id;
1818
}
1919
static key = 'Post';
2020

21+
static schema = {
22+
author: schema.Entity(
23+
class User {
24+
id = 0;
25+
},
26+
),
27+
};
28+
2129
get img() {
2230
return `//loremflickr.com/96/72/kitten,cat?lock=${this.id % 16}`;
2331
}
2432
}
2533
```
2634

27-
```ts title="PostResource" {14-21}
35+
```ts title="PostResource" {15-22}
2836
import { createResource } from '@data-client/rest';
2937
import { Post } from './Post';
3038

3139
export { Post };
3240

3341
export const PostResource = createResource({
3442
path: '/posts/:id',
43+
searchParams: {} as { userId?: string | number } | undefined,
3544
schema: Post,
3645
}).extend('vote', {
3746
path: '/posts/:id/vote',
@@ -76,7 +85,9 @@ export default function PostItem({ post }: Props) {
7685
</div>
7786
);
7887
}
79-
interface Props { post: Post }
88+
interface Props {
89+
post: Post;
90+
}
8091
```
8192

8293
```tsx title="TotalVotes" collapsed {15}
@@ -88,7 +99,7 @@ const queryTotalVotes = new schema.Query(
8899
new schema.All(Post),
89100
(posts, { userId } = {}) => {
90101
if (userId !== undefined)
91-
posts = posts.filter(post => post.userId === userId);
102+
posts = posts.filter(post => post.author.id === userId);
92103
return posts.reduce((total, post) => total + post.votes, 0);
93104
},
94105
);
@@ -101,7 +112,9 @@ export default function TotalVotes({ userId }: Props) {
101112
</center>
102113
);
103114
}
104-
interface Props { userId: number }
115+
interface Props {
116+
userId: number;
117+
}
105118
```
106119

107120
```tsx title="PostList" collapsed

docs/core/shared/_pagination.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class User extends Entity {
1919
}
2020

2121
pk() {
22-
return `${this.id}`;
22+
return this.id;
2323
}
2424
static key = 'User';
2525
}
@@ -36,7 +36,7 @@ export class Post extends Entity {
3636
body = '';
3737

3838
pk() {
39-
return this.id?.toString();
39+
return this.id;
4040
}
4141
static key = 'Post';
4242

docs/core/shared/_useLoading.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { Entity, createResource } from '@data-client/rest';
88

99
export class Post extends Entity {
1010
id = 0;
11-
userId = 0;
11+
author = 0;
1212
title = '';
1313
body = '';
1414
votes = 0;
1515

1616
pk() {
17-
return this.id?.toString();
17+
return this.id;
1818
}
1919
static key = 'Post';
2020

docs/core/shared/_useTransition.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import { Entity, createResource } from '@data-client/rest';
1111

1212
export class Post extends Entity {
1313
id = 0;
14-
userId = 0;
14+
author = 0;
1515
title = '';
1616
body = '';
1717
votes = 0;
1818

1919
pk() {
20-
return this.id?.toString();
20+
return this.id;
2121
}
2222
static key = 'Post';
2323

0 commit comments

Comments
 (0)