( client: QueryClient, post: Post, commentId: string, parentId: string, )
| 50 | }; |
| 51 | |
| 52 | export const removePostComments = ( |
| 53 | client: QueryClient, |
| 54 | post: Post, |
| 55 | commentId: string, |
| 56 | parentId: string, |
| 57 | ): void => { |
| 58 | const keys = getAllCommentsQuery(post.id); |
| 59 | const removeCachedComment = (data: PostCommentsData) => { |
| 60 | if (!data) { |
| 61 | return data; |
| 62 | } |
| 63 | const copy = { ...data }; |
| 64 | |
| 65 | if (commentId !== parentId) { |
| 66 | const parent = copy.postComments.edges.find( |
| 67 | ({ node }) => node.id === parentId, |
| 68 | ); |
| 69 | parent.node.children.edges = parent.node.children.edges.filter( |
| 70 | ({ node }) => node.id !== commentId, |
| 71 | ); |
| 72 | updatePostCache(client, post.id, { numComments: post.numComments - 1 }); |
| 73 | return copy; |
| 74 | } |
| 75 | |
| 76 | const parent = copy.postComments.edges.find( |
| 77 | ({ node }) => node.id === commentId, |
| 78 | ); |
| 79 | const count = parent.node.children.edges.length + 1; |
| 80 | const numComments = post.numComments - count; |
| 81 | updatePostCache(client, post.id, { numComments }); |
| 82 | copy.postComments.edges = data.postComments.edges.filter( |
| 83 | ({ node }) => node.id !== commentId, |
| 84 | ); |
| 85 | |
| 86 | return data; |
| 87 | }; |
| 88 | |
| 89 | keys.forEach((key) => { |
| 90 | client.setQueryData(key, removeCachedComment); |
| 91 | }); |
| 92 | }; |
| 93 | |
| 94 | export const usePostById = ({ |
| 95 | id, |
no test coverage detected