Make request to create comment.

This commit is contained in:
Pierre 2018-01-30 06:49:50 +01:00
parent 2d883a4197
commit cdfbbc6c59
2 changed files with 53 additions and 3 deletions

View File

@ -33,9 +33,11 @@ ComunicWeb.components.comments.form = {
type: "input", type: "input",
elemType: "text", elemType: "text",
class: "form-control", class: "form-control",
placeholder: "New comment..." placeholder: "New comment...",
name: "content"
}); });
//Add button group //Add button group
var buttonsGroup = createElem2({ var buttonsGroup = createElem2({
appendTo: inputGroup, appendTo: inputGroup,
@ -53,7 +55,8 @@ ComunicWeb.components.comments.form = {
var imageFile = createElem2({ var imageFile = createElem2({
appendTo: addImageLabel, appendTo: addImageLabel,
type: "input", type: "input",
elemType: "file" elemType: "file",
name: "image"
}); });
var imageButton = createElem2({ var imageButton = createElem2({
@ -73,7 +76,36 @@ ComunicWeb.components.comments.form = {
//Catch form when submitted //Catch form when submitted
commentForm.onsubmit = function(){ 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; return false;
} }
}, },

View File

@ -6,6 +6,24 @@
ComunicWeb.components.comments.interface = { 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 * Get informations about a single comment
* *