From cbb35a7df9fa8b5bf2b648a713e4028383aea180 Mon Sep 17 00:00:00 2001 From: Pavel Kvach Date: Thu, 22 Feb 2024 10:34:26 +0200 Subject: [PATCH] js: isso.js: Disable Postbox submit button on click, enable after response Disable the submit button in Postbox to prevent double posting upon click. Enable the button after receiving a response from the API endpoint. Fixes https://github.com/posativ/isso/issues/913 --- CHANGES.rst | 2 + isso/js/app/isso.js | 54 ++++++++++++------- .../integration/highlight-comments.test.js | 1 + isso/js/tests/integration/puppet.test.js | 45 +++++++++++++++- 4 files changed, 82 insertions(+), 20 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f85a6470..e8e4ee2c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -49,6 +49,7 @@ Bugfixes & Improvements - Change logging to include datetime and loglevel (`#1023`_, ix5) - Make 'text' field in 'comments' table NOT NULL and handling data migration (`#1019`_, pkvach) - Python 3.12 support (`#1015`_, ix5) +- Disable Postbox submit button on click, enable after response (`#993`_, pkvach) .. _#951: https://github.com/posativ/isso/pull/951 .. _#967: https://github.com/posativ/isso/pull/967 @@ -62,6 +63,7 @@ Bugfixes & Improvements .. _#1023: https://github.com/isso-comments/isso/pull/1023 .. _#1019: https://github.com/isso-comments/isso/pull/1019 .. _#1015: https://github.com/isso-comments/isso/pull/1015 +.. _#993: https://github.com/isso-comments/isso/pull/993 0.13.1.dev0 (2023-02-05) ------------------------ diff --git a/isso/js/app/isso.js b/isso/js/app/isso.js index 83ac00f9..c4b6a33f 100644 --- a/isso/js/app/isso.js +++ b/isso/js/app/isso.js @@ -92,34 +92,50 @@ var Postbox = function(parent) { // submit form, initialize optional fields with `null` and reset form. // If replied to a comment, remove form completely. - $("[type=submit]", el).on("click", function() { + $("[type=submit]", el).on("click", function(event) { edit(); if (! el.validate()) { return; } + const submitButton = event.target; + submitButton.disabled = true; // Disable the submit button to prevent double posting + var author = $("[name=author]", el).value || null, email = $("[name=email]", el).value || null, website = $("[name=website]", el).value || null; - localStorage.setItem("isso-author", JSON.stringify(author)); - localStorage.setItem("isso-email", JSON.stringify(email)); - localStorage.setItem("isso-website", JSON.stringify(website)); - - api.create($("#isso-thread").getAttribute("data-isso-id"), { - author: author, email: email, website: website, - text: $(".isso-textarea", el).value, - parent: parent || null, - title: $("#isso-thread").getAttribute("data-title") || null, - notification: $("[name=notification]", el).checked() ? 1 : 0, - }).then(function(comment) { - $(".isso-textarea", el).value = ""; - insert({ comment, scrollIntoView: true, offset: 0 }); - - if (parent !== null) { - el.onsuccess(); - } - }); + try { + localStorage.setItem("isso-author", JSON.stringify(author)); + localStorage.setItem("isso-email", JSON.stringify(email)); + localStorage.setItem("isso-website", JSON.stringify(website)); + + api.create($("#isso-thread").getAttribute("data-isso-id"), { + author: author, email: email, website: website, + text: $(".isso-textarea", el).value, + parent: parent || null, + title: $("#isso-thread").getAttribute("data-title") || null, + notification: $("[name=notification]", el).checked() ? 1 : 0, + }).then( + function(comment) { + $(".isso-textarea", el).value = ""; + insert({ comment, scrollIntoView: true, offset: 0 }); + + if (parent !== null) { + el.onsuccess(); + } + + submitButton.disabled = false; + }, + function(err) { + console.error(err); + submitButton.disabled = false; + } + ); + } catch (err) { + console.error(err); + submitButton.disabled = false; + } }); return el; diff --git a/isso/js/tests/integration/highlight-comments.test.js b/isso/js/tests/integration/highlight-comments.test.js index 0e176c11..4f21cbe9 100644 --- a/isso/js/tests/integration/highlight-comments.test.js +++ b/isso/js/tests/integration/highlight-comments.test.js @@ -40,6 +40,7 @@ test('Linked should be highlighted', async () => { // Cleanup // Need to click once to surface "confirm" and then again to confirm + await page.waitForSelector('#isso-1 > .isso-text-wrapper > .isso-comment-footer > .isso-delete'); await expect(page).toClick('#isso-1 > .isso-text-wrapper > .isso-comment-footer > .isso-delete'); await expect(page).toClick('#isso-1 > .isso-text-wrapper > .isso-comment-footer > .isso-delete'); }); diff --git a/isso/js/tests/integration/puppet.test.js b/isso/js/tests/integration/puppet.test.js index 5b55caa1..f7c3a5e5 100644 --- a/isso/js/tests/integration/puppet.test.js +++ b/isso/js/tests/integration/puppet.test.js @@ -148,6 +148,8 @@ test("should fill Postbox with valid data and receive 201 reply", async () => { await expect(elm.replace(//, '