Skip to content

Commit c452803

Browse files
committed
fix post comment invalidation
1 parent 0e7d502 commit c452803

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

apps/0-json-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"dev0": "pnpm serve",
8-
"serve": "json-server --watch db.json --delay 600 --port 3333"
8+
"serve": "json-server --watch db.json --delay 800 --port 3333"
99
},
1010
"dependencies": {
1111
"json-server": "0.17.4"

apps/3-vite-reactquery/src/pages/PostPage.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const PostPage = () => {
4747
queryKey: ["users", post?.userId],
4848
queryFn: async () => {
4949
const response = await fetch(
50-
`http://localhost:3333/users/${post?.userId}`,
50+
`http://localhost:3333/users/${post?.userId}`
5151
);
5252
return response.json() as Promise<IUser>;
5353
},
@@ -64,7 +64,7 @@ export const PostPage = () => {
6464
queryKey: ["posts", postId, "comments"],
6565
queryFn: async () => {
6666
const response = await fetch(
67-
`http://localhost:3333/posts/${postId}/comments`,
67+
`http://localhost:3333/posts/${postId}/comments`
6868
);
6969
return response.json() as Promise<IComment[]>;
7070
},
@@ -82,7 +82,7 @@ export const PostPage = () => {
8282
`http://localhost:3333/comments/${commentId}`,
8383
{
8484
method: "DELETE",
85-
},
85+
}
8686
);
8787
return response.json() as Promise<IComment>;
8888
},
@@ -91,7 +91,7 @@ export const PostPage = () => {
9191
onError: (err, commentId) => {
9292
console.error(
9393
`Error deleting comment ${commentId}. Rolling UI back`,
94-
err,
94+
err
9595
);
9696
alert("Error deleting comment");
9797
},
@@ -133,7 +133,7 @@ export const PostPage = () => {
133133
// Optimistically update to the new value
134134
queryClient.setQueryData(
135135
["posts", postId, "comments"],
136-
(oldComments: any) => [...oldComments, newComment],
136+
(oldComments: any) => [...oldComments, newComment]
137137
);
138138

139139
// Return a context object with the snapshot value
@@ -144,7 +144,7 @@ export const PostPage = () => {
144144
onError: (err, _newComment, context) => {
145145
queryClient.setQueryData(
146146
["posts", postId, "comments"],
147-
context?.previousComments,
147+
context?.previousComments
148148
);
149149
console.error("Error posting comment. Rolling UI back", err);
150150
},
@@ -153,7 +153,9 @@ export const PostPage = () => {
153153
},
154154
// Always refetch after error or success:
155155
onSettled: () => {
156-
queryClient.invalidateQueries({ queryKey: ["comments"] });
156+
queryClient.invalidateQueries({
157+
queryKey: ["posts", postId, "comments"],
158+
});
157159
},
158160
});
159161

0 commit comments

Comments
 (0)