Skip to content

Commit 49a0a61

Browse files
committed
fix: ncomments not updating for all comment levels; refactor: share Reply update ancestors' ncomments function with ShowNewComments
1 parent 4a777dc commit 49a0a61

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

components/reply.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useRoot } from './root'
1313
import { CREATE_COMMENT } from '@/fragments/paidAction'
1414
import useItemSubmit from './use-item-submit'
1515
import gql from 'graphql-tag'
16+
import { updateAncestorsCommentCount } from '@/lib/comments'
1617

1718
export default forwardRef(function Reply ({
1819
item,
@@ -82,17 +83,7 @@ export default forwardRef(function Reply ({
8283
const ancestors = item.path.split('.')
8384

8485
// update all ancestors
85-
ancestors.forEach(id => {
86-
cache.modify({
87-
id: `Item:${id}`,
88-
fields: {
89-
ncomments (existingNComments = 0) {
90-
return existingNComments + 1
91-
}
92-
},
93-
optimistic: true
94-
})
95-
})
86+
updateAncestorsCommentCount(cache, ancestors, 1)
9687

9788
// so that we don't see indicator for our own comments, we record this comments as the latest time
9889
// but we also have record num comments, in case someone else commented when we did

components/show-new-comments.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useApolloClient } from '@apollo/client'
33
import { COMMENT_WITH_NEW } from '../fragments/comments'
44
import styles from './comment.module.css'
55
import { itemUpdateQuery, commentUpdateFragment } from './use-live-comments'
6+
import { updateAncestorsCommentCount } from '@/lib/comments'
67

78
function prepareComments (client, newComments) {
89
return (data) => {
@@ -28,6 +29,10 @@ function prepareComments (client, newComments) {
2829
ncomments += (comment.ncomments || 0)
2930
}
3031

32+
// update all ancestors
33+
const ancestors = data.path.split('.')
34+
updateAncestorsCommentCount(client, ancestors, ncomments)
35+
3136
return {
3237
...data,
3338
comments: { ...data.comments, comments: [...freshNewComments, ...data.comments.comments] },

lib/comments.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function updateAncestorsCommentCount (cache, ancestors, increment) {
2+
// update all ancestors
3+
ancestors.forEach(id => {
4+
cache.modify({
5+
id: `Item:${id}`,
6+
fields: {
7+
ncomments (existingNComments = 0) {
8+
return existingNComments + increment
9+
}
10+
},
11+
optimistic: true
12+
})
13+
})
14+
}

0 commit comments

Comments
 (0)