Skip to content

Commit

Permalink
js: isso.js: Disable Postbox submit button on click, enable after res…
Browse files Browse the repository at this point in the history
…ponse

Disable the submit button in Postbox to prevent double posting upon click. Enable the button after receiving a response from the API endpoint.

Fixes isso-comments#913
  • Loading branch information
pkvach committed Apr 19, 2024
1 parent eb35b17 commit e532af7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ Bugfixes & Improvements
- Prevent auto creation of invalid links in comments (`#995`_, pkvach)
- Fix W3C Validation issues (`#999`_, pkvach)
- Handle deleted comments in Disqus migration (`#994`_, pkvach)
- 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
.. _#983: https://github.com/posativ/isso/pull/983
.. _#995: https://github.com/isso-comments/isso/pull/995
.. _#999: https://github.com/isso-comments/isso/pull/999
.. _#994: https://github.com/isso-comments/isso/pull/994
.. _#993: https://github.com/isso-comments/isso/pull/993

0.13.1.dev0 (2023-02-05)
------------------------
Expand Down
54 changes: 35 additions & 19 deletions isso/js/app/isso.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, true);

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, true);

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;
Expand Down
1 change: 1 addition & 0 deletions isso/js/tests/integration/highlight-comments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
45 changes: 44 additions & 1 deletion isso/js/tests/integration/puppet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ test("should fill Postbox with valid data and receive 201 reply", async () => {
await expect(elm.replace(/<time.*?>/, '<time>'))
.toMatchSnapshot();

await expect(page).not.toMatchElement('.isso-post-action > [type=submit]:disabled');

await page.waitForSelector('#isso-1 > .isso-text-wrapper > .isso-comment-footer > .isso-edit');

// Edit comment
Expand Down Expand Up @@ -238,7 +240,10 @@ test("should execute GET/PUT/POST/DELETE requests correctly", async () => {

await expect(page).toMatchElement(
'#isso-1 .isso-text',
{ text: 'New comment body' },
{
text: 'New comment body',
timeout: 1000,
},
);

// Delete comment via DELETE
Expand All @@ -264,3 +269,41 @@ test("should execute GET/PUT/POST/DELETE requests correctly", async () => {
{ text: 'New comment body' },
);
});

test("Postbox submit button should be disabled on submit click and enabled after response", async () => {

const delay = (duration) => new Promise(resolve => setTimeout(resolve, duration));

await page.setRequestInterception(true);
const submitButtonSelector = '.isso-post-action > [type=submit]';
let createHandler = async (request) => {
if (request.url().startsWith(ISSO_ENDPOINT + '/new')) {
// Disable request interception and remove the request handler
await page.setRequestInterception(false);
await page.off('request', createHandler);
delay(800).then(() => request.abort());
} else {
request.continue();
}
};
await page.on('request', createHandler);

await page.goto(
ISSO_ENDPOINT + '/demo',
{waitUntil: 'load'}
);

// Fill the textarea with the comment
await expect(page).toFill(
'.isso-textarea',
'A comment with *italics* and [a link](http://link.com)'
);

// Click the submit button
await expect(page).toClick(submitButtonSelector);

await page.waitForSelector(submitButtonSelector, {timeout: 1000});
await expect(page).toMatchElement(submitButtonSelector + ":disabled", {timeout: 1000});

await expect(page).not.toMatchElement(submitButtonSelector + ":disabled");
});

0 comments on commit e532af7

Please sign in to comment.