diff --git a/assets/js/common/ws.js b/assets/js/common/ws.js index bc193dd3..4093a080 100644 --- a/assets/js/common/ws.js +++ b/assets/js/common/ws.js @@ -197,6 +197,10 @@ class UserWebSocket { case "new_conv_message": SendEvent("newConvMessage", msg.data); break; + + case "new_comment": + SendEvent("new_comment", msg.data); + break; default: console.error("WS Unspported kind of message!", msg); diff --git a/assets/js/components/comments/form.js b/assets/js/components/comments/form.js index bcef0f5f..7a15dfd9 100644 --- a/assets/js/components/comments/form.js +++ b/assets/js/components/comments/form.js @@ -146,14 +146,6 @@ ComunicWeb.components.comments.form = { //Reset the creation form ComunicWeb.components.comments.form.display(postID, commentForm); - - //Load the new comment before the form element - var newCommentTarget = createElem2({ - insertBefore: commentForm, - type: "div", - class: "box-comment" - }); - ComunicWeb.components.comments.actions.reload(result.commentID, newCommentTarget); }); diff --git a/assets/js/components/comments/ui.js b/assets/js/components/comments/ui.js index 37c94580..34752e1d 100644 --- a/assets/js/components/comments/ui.js +++ b/assets/js/components/comments/ui.js @@ -4,7 +4,7 @@ * @author Pierre HUBERT */ -ComunicWeb.components.comments.ui = { +const CommentsUI = { /** * Display a list comments @@ -19,7 +19,7 @@ ComunicWeb.components.comments.ui = { var usersID = ComunicWeb.components.comments.utils.get_users_id(infos); //Get informations about the users - var usersInfo = ComunicWeb.user.userInfos.getMultipleUsersInfo(usersID, function(result){ + ComunicWeb.user.userInfos.getMultipleUsersInfo(usersID, function(result){ //Check for errors if(result.error){ @@ -44,11 +44,12 @@ ComunicWeb.components.comments.ui = { _process_comments: function(infos, usersInfos, postID, target){ //Create comments container - var container = createElem2({ + const container = createElem2({ appendTo: target, type: "div", class: "box-comments post-comments" }); + container.setAttribute("data-for-post-id", postID); //Process the list of comments for(i in infos){ @@ -287,4 +288,28 @@ ComunicWeb.components.comments.ui = { }, -} \ No newline at end of file +} + +ComunicWeb.components.comments.ui = CommentsUI; + +// Register to new comments events +document.addEventListener("new_comment", async (e) => { + const comment = e.detail; + + const target = document.querySelector("[data-for-post-id='"+comment.postID+"'].post-comments"); + + if(target == null) + return; + + // Check if there is comment form to avoid or not + const insertBefore = target.querySelector(".comment-creation-form"); + + const newCommentTarget = createElem2({ + insertBefore: insertBefore, + appendTo: insertBefore ? null : target, + type: "div", + class: "box-comment" + }); + + CommentsUI._show_comment(comment, await userInfo(comment.userID), newCommentTarget) +}) \ No newline at end of file