From d0cb3738c47c17d747cafa9e19e4f4ee4ed1f908 Mon Sep 17 00:00:00 2001 From: Charles Prichard Date: Thu, 28 Sep 2023 17:13:52 +0100 Subject: [PATCH] overriding gds data-prevent-double-click --- common/static/common/js/application.js | 4 +-- common/static/common/js/buttonDebounce.js | 33 +++++++++++++------- measures/jinja2/measures/create-review.jinja | 3 +- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/common/static/common/js/application.js b/common/static/common/js/application.js index 41eb88614..df472c8ac 100644 --- a/common/static/common/js/application.js +++ b/common/static/common/js/application.js @@ -13,7 +13,7 @@ import initCheckboxes from './checkboxes'; import initConditionalMeasureConditions from './conditionalMeasureConditions'; import initFilterDisabledToggleForComCode from './conditionalDisablingFilters' import initOpenCloseAccordionSection from './openCloseAccordion'; -import debounceButton from './buttonDebounce' +import tapDebounce from './buttonDebounce'; showHideCheckboxes(); // Initialise accessible-autocomplete components without a `name` attr in order // to avoid the "dummy" autocomplete field being submitted as part of the form @@ -28,4 +28,4 @@ initConditionalMeasureConditions(); initAutocompleteProgressiveEnhancement(); initFilterDisabledToggleForComCode(); initOpenCloseAccordionSection(); -debounceButton(); \ No newline at end of file +tapDebounce(); \ No newline at end of file diff --git a/common/static/common/js/buttonDebounce.js b/common/static/common/js/buttonDebounce.js index 2f35c8fd1..f92dd3590 100644 --- a/common/static/common/js/buttonDebounce.js +++ b/common/static/common/js/buttonDebounce.js @@ -1,13 +1,24 @@ -let button = document.getElementById('add-debounce') -let infoText = document.getElementById('add-debounce-inset-text') - -const debounceButton = () => { - if (button) { - addEventListener('submit', function() { - button.disabled = true, - infoText.classList.remove('js-hidden') - }) +const tapDebounce = function(event) { + console.log('b fires') + let DEBOUNCE_TIMEOUT_IN_SECONDS = 5; + + if (this.tapDebounceFormSubmitTimer) { + console.log('c fires') + event.preventDefault(); + button.disabled = true + return false + } + + + this.tapDebounceFormSubmitTimer = setTimeout(function () { + this.tapDebounceFormSubmitTimer = null; + }.bind(this), DEBOUNCE_TIMEOUT_IN_SECONDS * 1000); +}; + +document.querySelectorAll("[data-prevent-double-click").forEach( + (element) => { + element.addEventListener('click', tapDebounce); } -} +) -export default debounceButton; \ No newline at end of file +export default tapDebounce; \ No newline at end of file diff --git a/measures/jinja2/measures/create-review.jinja b/measures/jinja2/measures/create-review.jinja index a24d8bc68..7d4dbdf73 100644 --- a/measures/jinja2/measures/create-review.jinja +++ b/measures/jinja2/measures/create-review.jinja @@ -129,6 +129,5 @@ {% endfor %} {% endif %} {% endcall %} - {{ govukInsetText({"text": "Your measures are being processed and the submit button is now disabled. You will be taken to a confirmation screen once the measures have been created.", "classes": "js-hidden", "attributes": {"id": "add-debounce-inset-text"},})}} - {{ govukButton({"text": "Create", "preventDoubleClick": true, "attributes": {"id": "add-debounce"},}) }} + {{ govukButton({"text": "Create", "preventDoubleClick": true}) }} {% endblock %} \ No newline at end of file