Skip to content

Commit 18368af

Browse files
authored
Fix mutations example (reduxjs#1179)
1 parent 82533ea commit 18368af

File tree

2 files changed

+25
-58
lines changed

2 files changed

+25
-58
lines changed

examples/query/react/mutations/src/features/posts/PostsManager.tsx

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -85,63 +85,25 @@ const AddPost = () => {
8585
}
8686

8787
const PostList = () => {
88-
const [page, setPage] = useState(1)
89-
const { data: posts, isLoading, isFetching } = hooks.listPosts.useQuery(page)
90-
const prefetchPage = usePrefetch('listPosts')
91-
92-
const prefetchNext = useCallback(() => {
93-
prefetchPage(page + 1)
94-
}, [prefetchPage, page])
95-
96-
useEffect(() => {
97-
if (page !== posts?.total_pages) {
98-
prefetchNext()
99-
}
100-
}, [posts, page, prefetchNext])
88+
const { data: posts, isLoading } = useGetPostsQuery()
89+
const { push } = useHistory()
10190

10291
if (isLoading) {
10392
return <div>Loading</div>
10493
}
10594

106-
if (!posts?.data) {
95+
if (!posts) {
10796
return <div>No posts :(</div>
10897
}
10998

11099
return (
111-
<Box>
112-
<HStack spacing="14px">
113-
<Button
114-
onClick={() => setPage((prev) => prev - 1)}
115-
isLoading={isFetching}
116-
disabled={page === 1}
117-
>
118-
<Icon as={MdArrowBack} />
119-
</Button>
120-
<Button
121-
onClick={() => setPage((prev) => prev + 1)}
122-
isLoading={isFetching}
123-
disabled={page === posts.total_pages}
124-
onMouseEnter={prefetchNext}
125-
>
126-
<Icon as={MdArrowForward} />
127-
</Button>
128-
<Box>{`${page} / ${posts.total_pages}`}</Box>
129-
</HStack>
130-
<List spacing={3} mt={6}>
131-
{posts?.data.map(({ id, title, status }) => (
132-
<ListItem key={id}>
133-
<ListIcon as={MdBook} color="green.500" /> {title}{' '}
134-
<Badge
135-
ml="1"
136-
fontSize="0.8em"
137-
colorScheme={getColorForStatus(status)}
138-
>
139-
{status}
140-
</Badge>
141-
</ListItem>
142-
))}
143-
</List>
144-
</Box>
100+
<List spacing={3}>
101+
{posts.map(({ id, name }) => (
102+
<ListItem key={id} onClick={() => push(`/posts/${id}`)}>
103+
<ListIcon as={MdBook} color="green.500" /> {name}
104+
</ListItem>
105+
))}
106+
</List>
145107
)
146108
}
147109

examples/query/react/mutations/src/mocks/db.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ const db = factory({
1010
},
1111
})
1212

13-
db.post.create({ id: nanoid(), name: 'A sample post' })
14-
db.post.create({ id: nanoid(), name: 'A post about rtk query' })
13+
;[
14+
'A sample post',
15+
'A post about RTK Query',
16+
'How to randomly throw errors, a novella',
17+
].forEach((name) => {
18+
db.post.create({ id: nanoid(), name })
19+
})
1520

1621
export const handlers = [
1722
rest.post('/posts', async (req, res, ctx) => {
1823
const { name } = req.body as Partial<Post>
1924

20-
if (Math.random() < 0.5) {
25+
if (Math.random() < 0.3) {
2126
return res(
22-
ctx.json({ error: 'Oh no, there was an error' }),
27+
ctx.json({ error: 'Oh no, there was an error, try again.' }),
2328
ctx.status(500),
24-
ctx.delay(400)
29+
ctx.delay(300)
2530
)
2631
}
2732

@@ -30,16 +35,16 @@ export const handlers = [
3035
name,
3136
})
3237

33-
return res(ctx.json(post), ctx.delay(400))
38+
return res(ctx.json(post), ctx.delay(300))
3439
}),
3540
rest.put('/posts/:id', (req, res, ctx) => {
3641
const { name } = req.body as Partial<Post>
3742

38-
if (Math.random() < 0.5) {
43+
if (Math.random() < 0.3) {
3944
return res(
40-
ctx.json({ error: 'Oh no, there was an error' }),
45+
ctx.json({ error: 'Oh no, there was an error, try again.' }),
4146
ctx.status(500),
42-
ctx.delay(400)
47+
ctx.delay(300)
4348
)
4449
}
4550

@@ -48,7 +53,7 @@ export const handlers = [
4853
data: { name },
4954
})
5055

51-
return res(ctx.json(post), ctx.delay(400))
56+
return res(ctx.json(post), ctx.delay(300))
5257
}),
5358
...db.post.toHandlers('rest'),
5459
] as const

0 commit comments

Comments
 (0)