diff --git a/assets/js/common/api.js b/assets/js/common/api.js index 13c12b8a..835312ca 100644 --- a/assets/js/common/api.js +++ b/assets/js/common/api.js @@ -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){ diff --git a/assets/js/components/posts/form.js b/assets/js/components/posts/form.js index 11dda2f2..170bf12f 100644 --- a/assets/js/components/posts/form.js +++ b/assets/js/components/posts/form.js @@ -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; + }); } }, diff --git a/assets/js/components/posts/interface.js b/assets/js/components/posts/interface.js index a12958f2..341d9dbc 100644 --- a/assets/js/components/posts/interface.js +++ b/assets/js/components/posts/interface.js @@ -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); + + }, + } \ No newline at end of file