From c19d935c5e3b4b5f1629e917f7f11f0dc8677235 Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 16 Jan 2018 19:08:45 +0100 Subject: [PATCH] Make a request on the server to cancel survey response. --- assets/css/components/posts/ui.css | 8 ++++ assets/js/components/posts/interface.js | 19 +++++++++ assets/js/components/posts/ui.js | 57 +++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/assets/css/components/posts/ui.css b/assets/css/components/posts/ui.css index 24c3ff6b..a7eed3bf 100644 --- a/assets/css/components/posts/ui.css +++ b/assets/css/components/posts/ui.css @@ -73,10 +73,18 @@ padding: 10px; } + +/** + * Survey specific rules + */ .post .post-survey-question { text-align: center; } .post .post-survey-chart-contener { margin-bottom: 10px; +} + +.post .survey-given-response { + text-align: center; } \ No newline at end of file diff --git a/assets/js/components/posts/interface.js b/assets/js/components/posts/interface.js index 0f737d78..c2028f34 100644 --- a/assets/js/components/posts/interface.js +++ b/assets/js/components/posts/interface.js @@ -127,4 +127,23 @@ ComunicWeb.components.posts.interface = { }, + /** + * Cancel a response to a survey + * + * @param {int} postID The ID of the target post + * @param {function} callback This function is called once we got a response + */ + cancel_survey_response: function(postID, callback){ + + //Prepare an API request + apiURI = "surveys/cancel_response"; + params = { + postID: postID + }; + + //Perform the request + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); + + } + } \ No newline at end of file diff --git a/assets/js/components/posts/ui.js b/assets/js/components/posts/ui.js index 4447cd1a..d627fe04 100644 --- a/assets/js/components/posts/ui.js +++ b/assets/js/components/posts/ui.js @@ -439,6 +439,12 @@ ComunicWeb.components.posts.ui = { class: "post-survey-question" }); + //Answer contener + var surveyResponse = createElem2({ + appendTo: postRoot, + type: "div", + }); + //Create row var row = createElem2({ appendTo: postRoot, @@ -574,6 +580,57 @@ ComunicWeb.components.posts.ui = { } + //Display survey response options if the user is signed in + if(signed_in()){ + + //Check if the user gave a response to the survey + if(infos.data_survey.user_choice != 0){ + + //Create a text to display user choice + var choosedResponseElem = createElem2({ + appendTo: surveyResponse, + class: "survey-given-response", + type: "p", + innerHTML: "Your response: " + infos.data_survey.choices[infos.data_survey.user_choice].name + " " + }); + + //Offer the user to cancel his choice + var cancelReponseLink = createElem2({ + appendTo: choosedResponseElem, + type: "a", + innerHTML: "Cancel" + }); + + //Make cancel button lives + cancelReponseLink.onclick = function(){ + + ComunicWeb.common.messages.confirm("Do you really want to cancel your response to the survey ?", function(confirm){ + + //Check if the user cancelled + if(!confirm) + return; + + //Make a request on the server + ComunicWeb.components.posts.interface.cancel_survey_response(infos.ID, function(response){ + + //Check for errors + if(response.error){ + ComunicWeb.common.notificationSystem.showNotification("Could not cancel response to survey !", "danger"); + return; + } + + //Reload post + ComunicWeb.components.posts.actions.reload_post(infos.ID, postRoot); + + }); + + }); + + } + } + + + } } //If the kind of post was not implemented