Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Disable the submit button on registration

Roland Geider45b000e1cbd6
diff --git a/wger/core/static/js/forms.js b/wger/core/static/js/forms.js
new file mode 100644
index 000000000..81982ec3a
--- /dev/null
+++ b/wger/core/static/js/forms.js
@@ -0,0 +1,48 @@
+/*
+ This file is part of wger Workout Manager.
+
+ wger Workout Manager is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ wger Workout Manager is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ */
+
+/*
+ * Disables the submit buttons of forms marked with `data-disable-on-submit`
+ * once they are submitted, so a double click cannot send the same request
+ * twice.
+ *
+ * The buttons are disabled even when another script cancels the submit event:
+ * the reCAPTCHA v3 widget intercepts the event to fetch a token first and then
+ * submits the form programmatically, which does not fire a second event.
+ */
+
+document.addEventListener('submit', (event) => {
+ const form = event.target;
+ if (!(form instanceof HTMLFormElement) || !form.hasAttribute('data-disable-on-submit')) {
+ return;
+ }
+ form.querySelectorAll('button[type="submit"], input[type="submit"]').forEach((button) => {
+ button.disabled = true;
+ });
+});
+
+// Browsers restore the page from the back-forward cache with the buttons still
+// disabled, so re-enable them when the page is shown again
+window.addEventListener('pageshow', (event) => {
+ if (!event.persisted) {
+ return;
+ }
+ document
+ .querySelectorAll('form[data-disable-on-submit] [type="submit"]')
+ .forEach((button) => {
+ button.disabled = false;
+ });
+});
diff --git a/wger/core/templates/account/signup.html b/wger/core/templates/account/signup.html
index 1e58328f4..7ab33e369 100644
--- a/wger/core/templates/account/signup.html
+++ b/wger/core/templates/account/signup.html
@@ -9,7 +9,7 @@
{% block content %}
- <form method="post" action="{% url 'core:user:registration' %}">
+ <form method="post" action="{% url 'core:user:registration' %}" data-disable-on-submit>
{% csrf_token %}
{% if redirect_field_value %}
<input type="hidden"
diff --git a/wger/core/templates/template.html b/wger/core/templates/template.html
index 4fe2757b0..153aa96bd 100644
--- a/wger/core/templates/template.html
+++ b/wger/core/templates/template.html
@@ -50,6 +50,7 @@
<script src="{% static 'node/@popperjs/core/dist/umd/popper.js' %}"></script>
<script src="{% static 'js/nutrition.js' %}"></script>
<script src="{% static 'js/language.js' %}"></script>
+ <script src="{% static 'js/forms.js' %}"></script>
<script>window.WGER_I18N_PATHS = {% react_translation_paths %};</script>
<script src="{% static 'node/@wger-project/react-components/build/main.js' %}" type="module" defer="defer"></script>
diff --git a/wger/core/templates/template_features.html b/wger/core/templates/template_features.html
index 7a1724785..ac9543327 100644
--- a/wger/core/templates/template_features.html
+++ b/wger/core/templates/template_features.html
@@ -42,6 +42,7 @@
<script src="{% static 'node/bootstrap/dist/js/bootstrap.bundle.min.js' %}"></script>
<script src="{% static 'node/@popperjs/core/dist/umd/popper.js' %}"></script>
<script src="{% static 'js/language.js' %}"></script>
+ <script src="{% static 'js/forms.js' %}"></script>
<title>wger Workout Manager - {% translate "Features" %}</title>
</head>
diff --git a/wger/software/templates/features.html b/wger/software/templates/features.html
index f8d1853af..4f5ca708d 100644
--- a/wger/software/templates/features.html
+++ b/wger/software/templates/features.html
@@ -349,7 +349,8 @@
<!-- form -->
<form method="post"
action="{% url 'core:user:registration' %}"
- class="row justify-content-center">
+ class="row justify-content-center"
+ data-disable-on-submit>
{% csrf_token %}
<h3 class="col-12 text-center fs-1 uppercase font-bebas-neue">
{% translate "Sign Up Now For Free" %}