(comment: Comment)
| 93 | ); |
| 94 | |
| 95 | const onSuccess = (comment: Comment) => { |
| 96 | if (!comment) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | const updateQueryData = ( |
| 101 | data: PostCommentsData, |
| 102 | ): PostCommentsData | undefined => { |
| 103 | const copy = structuredClone(data); |
| 104 | const { edges } = copy.postComments; |
| 105 | |
| 106 | if (!editCommentId) { |
| 107 | const edge = generateCommentEdge(comment); |
| 108 | |
| 109 | if (!parentCommentId) { |
| 110 | edges.unshift(edge); |
| 111 | return copy; |
| 112 | } |
| 113 | |
| 114 | const parent = edges.find(({ node }) => node.id === parentCommentId); |
| 115 | |
| 116 | if (!parent) { |
| 117 | return undefined; |
| 118 | } |
| 119 | |
| 120 | parent.node.children.edges.push(edge); |
| 121 | return copy; |
| 122 | } |
| 123 | |
| 124 | const replies = edges.flatMap(({ node }) => node.children?.edges ?? []); |
| 125 | const edited = [...edges, ...replies].find( |
| 126 | ({ node }) => node.id === editCommentId, |
| 127 | ); |
| 128 | |
| 129 | if (!edited) { |
| 130 | return undefined; |
| 131 | } |
| 132 | |
| 133 | edited.node = { ...comment, children: edited.node.children }; |
| 134 | return copy; |
| 135 | }; |
| 136 | |
| 137 | const forInvalidation: QueryKey[] = []; |
| 138 | |
| 139 | getAllCommentsQuery(postId).forEach((queryKey) => { |
| 140 | client.setQueryData<PostCommentsData>(queryKey, (data) => { |
| 141 | if (!data) { |
| 142 | forInvalidation.push(queryKey); |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | const updated = updateQueryData(data); |
| 147 | |
| 148 | if (!updated) { |
| 149 | forInvalidation.push(queryKey); |
| 150 | return data; |
| 151 | } |
| 152 |
no test coverage detected