From cc0d405de8ac2c3228c3f93b493ec26a1b3ea31e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 17 May 2020 19:22:04 +0200 Subject: [PATCH] Can create a new choice for a survey --- assets/js/common/shorcuts.js | 19 ++++++++++++++ assets/js/components/posts/interface.js | 13 ++++++++++ assets/js/components/posts/ui.js | 33 +++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/assets/js/common/shorcuts.js b/assets/js/common/shorcuts.js index 361f85d5..1514ff78 100644 --- a/assets/js/common/shorcuts.js +++ b/assets/js/common/shorcuts.js @@ -305,6 +305,25 @@ async function showConfirmDialog(msg) { }) } + +/** + * Ask the user to enter a string + * + * @param {String} title The dialog of the dialog to show + * @param {String} message Helper message to show to the user + * @param {String} defaultValue The default value of the message + */ +async function showInputTextDialog(title, message, defaultValue = "") { + return new Promise((res, rej) => + ComunicWeb.common.messages.inputString( + title, + message, + defaultValue, + (msg) => msg === false ? rej : res(msg) + ) + ) +} + /** * Prepare for potential future translation system * diff --git a/assets/js/components/posts/interface.js b/assets/js/components/posts/interface.js index 750d5765..6b970d1f 100644 --- a/assets/js/components/posts/interface.js +++ b/assets/js/components/posts/interface.js @@ -229,6 +229,19 @@ const PostsInterface = { }, + /** + * Create a new choice for this survey + * + * @param {number} postID The ID of the associated post + * @param {String} choice The value of the choice + */ + createSurveyChoice: async function(postID, choice) { + await api("survey/create_new_choice", { + postID: postID, + choice: choice + }, true); + }, + /** * Prevent new choices from being created * diff --git a/assets/js/components/posts/ui.js b/assets/js/components/posts/ui.js index 2f8ddaea..e9ebf5e9 100644 --- a/assets/js/components/posts/ui.js +++ b/assets/js/components/posts/ui.js @@ -881,6 +881,38 @@ ComunicWeb.components.posts.ui = { } + // Offer the user to create a new response, if possible + if(info.data_survey.user_choice == 0 && info.data_survey.allowNewChoices) { + + const link = createElem2({ + appendTo: surveyResponse, + type: "div", + class: "a txt-center", + innerHTML: tr("Add a new choice to this survey") + }); + + link.addEventListener("click", async () => { + + try { + const choice = await showInputTextDialog( + tr("Create a choice"), + tr("Please specify the new choice you would like to create for this survey:"), + "" + ); + + await PostsInterface.createSurveyChoice(info.ID, choice); + + //Reload post + ComunicWeb.components.posts.actions.reload_post(info.ID, postRoot); + + } catch(e) { + console.error(e); + notify(tr("Could not create a new choice for this survey!"), "danger"); + } + }); + } + + // If the user is the owner of the survey, offer him to close it if(info.data_survey.userID == userID() && info.data_survey.allowNewChoices) { @@ -903,6 +935,7 @@ ComunicWeb.components.posts.ui = { ComunicWeb.components.posts.actions.reload_post(info.ID, postRoot); } catch(e) { + console.error(e); notify(tr("Could not block new choices from being created!"), "danger"); } });