Skip to content

Commit

Permalink
Merge pull request #4841 from thematters/fix/article-comment-count-cache
Browse files Browse the repository at this point in the history
fix(ArticleComment): fix comment count cache
  • Loading branch information
wlliaml authored Sep 4, 2024
2 parents 4e44772 + d01d6f1 commit 270638b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 57 deletions.
1 change: 1 addition & 0 deletions src/components/Comment/FooterActions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const fragments = {
id
isBlocking
}
commentCount
}
... on Moment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import {
SpinnerBlock,
useEventListener,
useMutation,
useRoute,
ViewerContext,
} from '~/components'
import { PUT_ARTICLE_COMMENT } from '~/components/GQL/mutations/putComment'
import {
updateArticleComments,
updateArticlePublic,
updateCommentDetail,
} from '~/components/GQL/updates'
import { PutArticleCommentMutation } from '~/gql/graphql'
Expand Down Expand Up @@ -54,8 +52,6 @@ const CommentForm: React.FC<CommentFormProps> = ({
}) => {
const viewer = useContext(ViewerContext)
const formRef = useRef<HTMLFormElement>(null)
const { getQuery, routerLang } = useRoute()
const shortHash = getQuery('shortHash')

const [putComment] =
useMutation<PutArticleCommentMutation>(PUT_ARTICLE_COMMENT)
Expand Down Expand Up @@ -120,22 +116,6 @@ const CommentForm: React.FC<CommentFormProps> = ({
comment: mutationResult.data?.putComment,
})
}

if (!!parentId) {
updateArticlePublic({
cache,
shortHash,
routerLang,
type: 'addSecondaryComment',
})
} else {
updateArticlePublic({
cache,
shortHash,
routerLang,
type: 'addComment',
})
}
},
})

Expand Down
25 changes: 1 addition & 24 deletions src/components/Forms/ArticleCommentForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ import {
useCommentEditorContext,
useEventListener,
useMutation,
useRoute,
ViewerContext,
} from '~/components'
import CommentEditor from '~/components/Editor/Comment'
import {
updateArticleComments,
updateArticlePublic,
updateCommentDetail,
} from '~/components/GQL'
import { updateArticleComments, updateCommentDetail } from '~/components/GQL'
import { PUT_ARTICLE_COMMENT } from '~/components/GQL/mutations/putComment'
import { PutArticleCommentMutation } from '~/gql/graphql'

Expand Down Expand Up @@ -63,14 +58,12 @@ export const ArticleCommentForm: React.FC<ArticleCommentFormProps> = ({
}) => {
const intl = useIntl()
const viewer = useContext(ViewerContext)
const { getQuery, routerLang } = useRoute()
const { setActiveEditor } = useCommentEditorContext()
const [editor, localSetEditor] = useState<Editor | null>(null)
const setEditor = (editor: Editor | null) => {
localSetEditor(editor)
propsSetEditor?.(editor)
}
const shortHash = getQuery('shortHash')

const [putComment] =
useMutation<PutArticleCommentMutation>(PUT_ARTICLE_COMMENT)
Expand Down Expand Up @@ -136,22 +129,6 @@ export const ArticleCommentForm: React.FC<ArticleCommentFormProps> = ({
comment: mutationResult.data?.putComment,
})
}

if (!!parentId) {
updateArticlePublic({
cache,
shortHash,
routerLang,
type: 'addSecondaryComment',
})
} else {
updateArticlePublic({
cache,
shortHash,
routerLang,
type: 'addComment',
})
}
},
})

Expand Down
14 changes: 1 addition & 13 deletions src/components/GQL/updates/articlePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ export const updateArticlePublic = ({
routerLang: UserLanguage
viewer?: Viewer
txId?: string
type:
| 'deleteComment'
| 'addComment'
| 'addSecondaryComment'
| 'deleteSecondaryComment'
| 'updateDonation'
type: 'deleteComment' | 'deleteSecondaryComment' | 'updateDonation'
}) => {
// FIXME: circular dependencies
const {
Expand Down Expand Up @@ -68,17 +63,10 @@ export const updateArticlePublic = ({
let commentCount = data.article.commentCount
let totalCount = data.article.comments.totalCount
switch (type) {
case 'addComment':
totalCount += 1
commentCount += 1
break
case 'deleteComment':
totalCount -= 1
commentCount -= 1
break
case 'addSecondaryComment':
commentCount += 1
break
case 'deleteSecondaryComment':
commentCount -= 1
break
Expand Down

0 comments on commit 270638b

Please sign in to comment.