From 54f1bd9122f24da12ebff6e899660ae2995da272 Mon Sep 17 00:00:00 2001 From: Christian Taylor Date: Sat, 17 Aug 2024 11:40:07 -0500 Subject: [PATCH] Re-enable the submitter when a request is aborted --- package.json | 2 +- src/index.js | 11 +++++++---- tests/form.cy.js | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 0e228a8..b14534b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@imacrayon/alpine-ajax", "description": "An Alpine.js plugin for building server-powered frontends.", - "version": "0.8.0", + "version": "0.8.1", "license": "MIT", "author": "Christian Taylor", "homepage": "https://alpine-ajax.js.org", diff --git a/src/index.js b/src/index.js index c2fba34..3dbdf87 100644 --- a/src/index.js +++ b/src/index.js @@ -282,10 +282,13 @@ async function withSubmitter(submitter, callback) { submitter.setAttribute('aria-disabled', 'true') submitter.addEventListener('click', disableEvent) - let result = await callback() - - submitter.removeAttribute('aria-disabled') - submitter.removeEventListener('click', disableEvent) + let result + try { + result = await callback() + } finally { + submitter.removeAttribute('aria-disabled') + submitter.removeEventListener('click', disableEvent) + } return result } diff --git a/tests/form.cy.js b/tests/form.cy.js index 57f3437..fe64f6c 100644 --- a/tests/form.cy.js +++ b/tests/form.cy.js @@ -295,3 +295,24 @@ test('performs a normal submit when a 500 status code is returned', }) } ) + +test('submitter is disabled while submitting', + html`
`, + ({ intercept, get, wait }) => { + intercept('POST', (req) => { + req.continue((res) => { + get('button').should('have.attr', 'aria-disabled') + }) + }).as('response') + get('button').click() + } +) + +test('submitter is reset when request is aborted', + html`
`, + ({ intercept, get, wait }) => { + get('button').click() + wait(300) + get('button').should('not.have.attr', 'aria-disabled') + } +)