mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-12 16:14:58 +08:00
Fix comments counter [SCI-5371] (#3063)
This commit is contained in:
parent
f478ad91c3
commit
e88848cd75
4 changed files with 15 additions and 16 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
var CommentsSidebar = (function() {
|
||||
const SIDEBAR = '.comments-sidebar';
|
||||
var openBtn = null;
|
||||
var commentsCounter;
|
||||
|
||||
function loadCommentsList() {
|
||||
var commentsUrl = $(SIDEBAR).data('comments-url');
|
||||
|
@ -21,17 +21,17 @@ var CommentsSidebar = (function() {
|
|||
});
|
||||
}
|
||||
|
||||
function updateCounter(commentCount) {
|
||||
var commentCountElem = openBtn.find('.comment-count');
|
||||
if (commentCountElem.length !== 0) {
|
||||
function updateCounter() {
|
||||
var commentsAmount = $(SIDEBAR).find('.comments-list .comment-container').length;
|
||||
if (commentsCounter.length) {
|
||||
// Replace the number in comment element
|
||||
commentCountElem.text(commentCountElem.text().replace(/\d+/g, commentCount));
|
||||
commentsCounter.text(commentsCounter.text().replace(/\d+/g, commentsAmount));
|
||||
}
|
||||
}
|
||||
|
||||
function initOpenButton() {
|
||||
$(document).on('click', '.open-comments-sidebar', function() {
|
||||
openBtn = $(this);
|
||||
commentsCounter = $(`#comment-count-${$(this).data('objectId')}`);
|
||||
CommentsSidebar.open($(this).data('objectType'), $(this).data('objectId'));
|
||||
});
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ var CommentsSidebar = (function() {
|
|||
$(SIDEBAR).find('.comment-input-field').val('');
|
||||
$(SIDEBAR).find('.sidebar-footer').removeClass('update');
|
||||
$('.error-container').empty();
|
||||
updateCounter(result.comment_count);
|
||||
updateCounter();
|
||||
},
|
||||
error: (result) => {
|
||||
$('.error-container').text(result.responseJSON.errors.message);
|
||||
|
@ -98,9 +98,9 @@ var CommentsSidebar = (function() {
|
|||
url: deleteUrl,
|
||||
type: 'DELETE',
|
||||
dataType: 'json',
|
||||
success: (result) => {
|
||||
success: () => {
|
||||
commentContainer.remove();
|
||||
updateCounter(result.comment_count);
|
||||
updateCounter();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -29,7 +29,7 @@ class CommentsController < ApplicationController
|
|||
user: current_user,
|
||||
associated_id: @commentable.id
|
||||
)
|
||||
comment_create_helper(@comment, 'comment', @commentable.comments.size)
|
||||
comment_create_helper(@comment, 'comment')
|
||||
end
|
||||
|
||||
def update
|
||||
|
@ -39,7 +39,7 @@ class CommentsController < ApplicationController
|
|||
end
|
||||
|
||||
def destroy
|
||||
comment_destroy_helper(@comment, @commentable.comments.size - 1)
|
||||
comment_destroy_helper(@comment)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -26,7 +26,7 @@ module CommentHelper
|
|||
}
|
||||
end
|
||||
|
||||
def comment_create_helper(comment, partial = 'item', comment_count)
|
||||
def comment_create_helper(comment, partial = 'item')
|
||||
if comment.save
|
||||
case comment.type
|
||||
when 'StepComment'
|
||||
|
@ -44,7 +44,6 @@ module CommentHelper
|
|||
end
|
||||
|
||||
render json: {
|
||||
comment_count: comment_count,
|
||||
html: render_to_string(
|
||||
partial: "/shared/comments/#{partial}.html.erb",
|
||||
locals: {
|
||||
|
@ -117,7 +116,7 @@ module CommentHelper
|
|||
end
|
||||
end
|
||||
|
||||
def comment_destroy_helper(comment, comment_count)
|
||||
def comment_destroy_helper(comment)
|
||||
if comment.destroy
|
||||
case comment.type
|
||||
when 'StepComment'
|
||||
|
@ -129,7 +128,7 @@ module CommentHelper
|
|||
when 'TaskComment'
|
||||
log_my_module_activity(:delete_module_comment, comment)
|
||||
end
|
||||
render json: { comment_count: comment_count }, status: :ok
|
||||
render json: {}, status: :ok
|
||||
else
|
||||
render json: { message: I18n.t('comments.delete_error') },
|
||||
status: :unprocessable_entity
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
data-object-type="Project"
|
||||
data-object-id="<%= project.id %>">
|
||||
<i class="fas fa-comment"></i>
|
||||
<span class="comment-count">
|
||||
<span id="comment-count-<%= project.id %>">
|
||||
<%= t('projects.index.comments_option', comments_count: project.comment_count) %>
|
||||
</span>
|
||||
</a>
|
||||
|
|
Loading…
Add table
Reference in a new issue