Skip to content

Commit

Permalink
Disable Submit Button to Prevent Duplicate Submissions (#4275)
Browse files Browse the repository at this point in the history
* A JS util to disable a submit button after click

* Add a spinny boi, change button text while loading
  • Loading branch information
jperson1 authored Sep 18, 2024
1 parent 182735d commit baabe97
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion backend/audit/templates/audit/submission.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ <h1 class="font-sans-2xl" id="submission">Single Audit Submission</h1>
Please save copies of all submitted documents to your local drive. The FAC doesn't maintain these files, and can't return them for future review.
</p>
<div class="margin-top-6">
<button class="usa-button margin-right-2" id="continue">Submit Single Audit</button>
<div hidden id="loader" class="cross-validation-loader margin-top-6">
<img src="{% static 'img/loader.svg' %}" alt="spinner for processing upload" />
</div>
<button class="usa-button margin-right-2" id="continue" disable-submit-after-click>Submit Single Audit</button>
<a href="{% url 'audit:SubmissionProgress' report_id %}">Cancel</a>
</div>
</fieldset>
</form>
</div>
</div>
<script src="{% static 'compiled/js/util.js' %}"></script>
{% endblock content %}
30 changes: 30 additions & 0 deletions backend/static/js/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Attach an event handler to every button with attribute "disable-submit-after-click".
* This will disable the button after clicking it, enable the sibling spinning icon
* (if it exists), and submit the form.
*/
function disableSubmitAfterClick() {
const matching_buttons = document.querySelectorAll(
'button[disable-submit-after-click]'
);

matching_buttons.forEach((button) => {
const form = button.closest('form'); // Parent form
const loader = button.parentElement.querySelector(`div[id='loader']`); // Sibling loader

button.addEventListener('click', () => {
button.disabled = true;
button.textContent = 'Submitting...';
if (loader) {
loader.hidden = false;
}
form.submit();
});
});
}

function init() {
disableSubmitAfterClick();
}

window.onload = init;

0 comments on commit baabe97

Please sign in to comment.