File tree Expand file tree Collapse file tree 3 files changed +21
-11
lines changed Expand file tree Collapse file tree 3 files changed +21
-11
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import { useRoot } from './root'
13
13
import { CREATE_COMMENT } from '@/fragments/paidAction'
14
14
import useItemSubmit from './use-item-submit'
15
15
import gql from 'graphql-tag'
16
+ import { updateAncestorsCommentCount } from '@/lib/comments'
16
17
17
18
export default forwardRef ( function Reply ( {
18
19
item,
@@ -82,17 +83,7 @@ export default forwardRef(function Reply ({
82
83
const ancestors = item . path . split ( '.' )
83
84
84
85
// 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 )
96
87
97
88
// so that we don't see indicator for our own comments, we record this comments as the latest time
98
89
// but we also have record num comments, in case someone else commented when we did
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { useApolloClient } from '@apollo/client'
3
3
import { COMMENT_WITH_NEW } from '../fragments/comments'
4
4
import styles from './comment.module.css'
5
5
import { itemUpdateQuery , commentUpdateFragment } from './use-live-comments'
6
+ import { updateAncestorsCommentCount } from '@/lib/comments'
6
7
7
8
function prepareComments ( client , newComments ) {
8
9
return ( data ) => {
@@ -28,6 +29,10 @@ function prepareComments (client, newComments) {
28
29
ncomments += ( comment . ncomments || 0 )
29
30
}
30
31
32
+ // update all ancestors
33
+ const ancestors = data . path . split ( '.' )
34
+ updateAncestorsCommentCount ( client , ancestors , ncomments )
35
+
31
36
return {
32
37
...data ,
33
38
comments : { ...data . comments , comments : [ ...freshNewComments , ...data . comments . comments ] } ,
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments