Can send create post request on the API

This commit is contained in:
Pierre 2018-01-06 15:26:43 +01:00
parent 1b5678cdd1
commit f7ba871344
3 changed files with 82 additions and 3 deletions

View File

@ -53,7 +53,7 @@ ComunicWeb.common.api = {
//Prepare request response
apiXHR.onreadystatechange = function(){
ComunicWeb.common.api._on_state_change(apiXHR, nextAction);
ComunicWeb.common.api._on_state_change(requestURL, apiXHR, nextAction);
}
//Set request headers
@ -63,13 +63,56 @@ ComunicWeb.common.api = {
apiXHR.send(datas);
},
/**
* Make an API request with a prepared form data object
*
* @param {String} apiURI The URI to call in the API
* @param {FormData} data The form data object
* @param {Boolean} requireLoginTokens Specify if login tokens are required or not
* @param {Function} nextAction What to do next
*/
makeFormDatarequest: function(apiURI, data, requireLoginTokens, nextAction){
//Prepare the request URL
var requestURL = ComunicWeb.__config.apiURL + apiURI;
//Add API service tokens
data.append('serviceName', ComunicWeb.__config.apiServiceName);
data.append('serviceToken', ComunicWeb.__config.apiServiceToken);
//Add login tokens to params if required
if(requireLoginTokens){
//Get login tokens
tokens = ComunicWeb.user.loginTokens.getLoginTokens();
if(tokens){
//Add tokens
data.append('userToken1', tokens.token1);
data.append('userToken2', tokens.token2);
}
}
//Create request
var apiXHR = new XMLHttpRequest();
apiXHR.open("POST", requestURL);
//Prepare request response
apiXHR.onreadystatechange = function(){
ComunicWeb.common.api._on_state_change(requestURL, apiXHR, nextAction);
}
//Submit request
apiXHR.send(data);
},
/**
* Handle xhr request chnages
*
* @param {string} requestURL The URL of the request
* @param {XMLHttpRequest} apiXHR The request element
* @param {Function} nextAction What to do once the request is done
*/
_on_state_change: function(apiXHR, nextAction){
_on_state_change: function(requestURL, apiXHR, nextAction){
//We continue only if request is terminated
if(apiXHR.readyState == 4){

View File

@ -445,8 +445,22 @@ ComunicWeb.components.posts.form = {
var visibilityLevel = visibility_choices_contener.querySelector("input:checked").value;
datas.append("visibility", visibilityLevel);
//Lock the send button
sendButton.disabled = true;
//Try to perform the request
ComunicWeb.components.posts.interface.send_post(kind, id, datas, function(result){
//Check for errors
if(result.error){
ComunicWeb.common.notificationSystem.showNotification("An error occured while trying to send a new post !", "danger");
return;
}
//Else
//DEBUG - Temporary behaviour
sendButton.disabled = false;
});
}
},

View File

@ -25,4 +25,26 @@ ComunicWeb.components.posts.interface = {
},
/**
* Send a new post
*
* @param {string} kind The kind of page
* @param {string} id The ID of the kind of page
* @param {FormData} data The data of the new post
* @param {function} callback The function to call once the post is posted
*/
send_post: function(kind, id, data, callback){
//Prepare the request
var apiURI = "posts/create";
//Append the kind of post to the request
data.append("kind-page", kind);
data.append("kind-id", id);
//Perform the request
ComunicWeb.common.api.makeFormDatarequest(apiURI, data, true, callback);
},
}