From cdfbbc6c5994a0ba0c62022f2bd9ca7ade1aaa4c Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 30 Jan 2018 06:49:50 +0100 Subject: [PATCH] Make request to create comment. --- assets/js/components/comments/form.js | 38 ++++++++++++++++++++-- assets/js/components/comments/interface.js | 18 ++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/assets/js/components/comments/form.js b/assets/js/components/comments/form.js index cb7cb991..f54b26a5 100644 --- a/assets/js/components/comments/form.js +++ b/assets/js/components/comments/form.js @@ -33,8 +33,10 @@ ComunicWeb.components.comments.form = { type: "input", elemType: "text", class: "form-control", - placeholder: "New comment..." + placeholder: "New comment...", + name: "content" }); + //Add button group var buttonsGroup = createElem2({ @@ -53,7 +55,8 @@ ComunicWeb.components.comments.form = { var imageFile = createElem2({ appendTo: addImageLabel, type: "input", - elemType: "file" + elemType: "file", + name: "image" }); var imageButton = createElem2({ @@ -73,7 +76,36 @@ ComunicWeb.components.comments.form = { //Catch form when submitted commentForm.onsubmit = function(){ - alert("Send !"); + + //Check for image + var hasImage = imageFile.files.length > 0; + + //Check the comment + if(!hasImage && newCommentText.value < 5){ + ComunicWeb.common.notificationSystem.showNotification("Please type a valid comment! (at least 5 characters)", "danger"); + return false; + } + + //Lock send button + sendButton.disabled = true; + + //Try to create the comment + var formData = new FormData(commentForm); + ComunicWeb.components.comments.interface.create(postID, formData, function(result){ + + //Unlock send button + sendButton.disabled = false; + + //Check for errors + if(result.error){ + ComunicWeb.common.notificationSystem.showNotification("Couldn't create comment! (check its content)", "danger"); + return; + } + + //Perform next actions + }); + + return false; } }, diff --git a/assets/js/components/comments/interface.js b/assets/js/components/comments/interface.js index 6774bddb..e5b96bd1 100644 --- a/assets/js/components/comments/interface.js +++ b/assets/js/components/comments/interface.js @@ -6,6 +6,24 @@ ComunicWeb.components.comments.interface = { + /** + * Create a new comment + * + * @param {number} postID The ID of the target post + * @param {FormData} data The data of the new comment + * @param {function} callback + */ + create: function(postID, data, callback){ + + //Prepare the request + var apiURI = "comments/create"; + data.append("postID", postID); + + //Make it + ComunicWeb.common.api.makeFormDatarequest(apiURI, data, true, callback); + + }, + /** * Get informations about a single comment *