Skip to content

Commit

Permalink
comments_async: include child_comments in comment count
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk committed Jul 27, 2023
1 parent 458b061 commit 641e0a4
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions adhocracy4/comments_async/static/comments_async/comment_box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const CommentBox = (props) => {
translated.entries = django.ngettext('entry', 'entries', data.count)
setComments(data.results)
setNextComments(data.next)
setCommentCount(data.count)
setCommentCount(calculateCommentCount(data.results))
setHasCommentingPermission(data.has_commenting_permission)
setProjectIsPublic(data.project_is_public)
setUseTermsOfUse(data.use_org_terms_of_use)
Expand Down Expand Up @@ -149,7 +149,6 @@ export const CommentBox = (props) => {

function addComment (parentIndex, comment) {
let diff = {}
let newCommentCount = commentCount
if (parentIndex !== undefined) {
diff[parentIndex] = {
child_comments: { $push: [comment] },
Expand All @@ -160,11 +159,10 @@ export const CommentBox = (props) => {
}
} else {
diff = { $unshift: [comment] }
newCommentCount++
setMainError(undefined)
}
setComments(update(comments, diff))
setCommentCount(newCommentCount)
setCommentCount(commentCount + 1)
}

function setReplyError (parentIndex, index, message) {
Expand Down Expand Up @@ -295,7 +293,7 @@ export const CommentBox = (props) => {
const data = result
setComments(data.results)
setNextComments(data.next)
setCommentCount(data.count)
setCommentCount(calculateCommentCount(data.results))
setFilter(filter)
setFilterDisplay(displayFilter)
setLoadingFilter(false)
Expand Down Expand Up @@ -324,7 +322,7 @@ export const CommentBox = (props) => {
const data = result
setComments(data.results)
setNextComments(data.next)
setCommentCount(data.count)
setCommentCount(calculateCommentCount(data.results))
setSort(order)
setLoadingFilter(false)
})
Expand All @@ -350,7 +348,7 @@ export const CommentBox = (props) => {
const data = result
setComments(data.results)
setNextComments(data.next)
setCommentCount(data.count)
setCommentCount(calculateCommentCount(data.results))
setSearch(search)
setLoadingFilter(false)
})
Expand Down Expand Up @@ -379,7 +377,7 @@ export const CommentBox = (props) => {
const newComments = comments.concat(data.results)
setComments(newComments)
setNextComments(data.next)
setCommentCount(data.count)
setCommentCount(commentCount + calculateCommentCount(data.results))
if (findAnchoredComment(newComments, anchoredCommentParentId)) {
setLoading(false)
} else {
Expand All @@ -391,6 +389,17 @@ export const CommentBox = (props) => {
})
}

function calculateCommentCount (comments) {
let count = 0
comments.forEach((comment) => {
count++
if (comment.child_comments.length > 0) {
count += comment.child_comments.length
}
})
return count
}

function handleScroll () {
const html = document.documentElement
if (
Expand Down

0 comments on commit 641e0a4

Please sign in to comment.