Skip to content

Commit 0395c46

Browse files
BenjaminCharmesml-evs
authored andcommitted
Prevent double-click on modal submit button
1 parent c588dcb commit 0395c46

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

webapp/src/components/Modal.vue

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323
</div>
2424
<div class="modal-footer">
2525
<slot name="footer">
26-
<input type="submit" class="btn btn-info" :disabled="disableSubmit" value="Submit" />
26+
<button
27+
type="button"
28+
class="btn btn-info"
29+
:disabled="disableSubmit || submitLocked"
30+
@click="lockAndSubmit"
31+
>
32+
Submit
33+
</button>
34+
2735
<button
2836
type="button"
2937
class="btn btn-secondary"
@@ -57,6 +65,7 @@ export default {
5765
return {
5866
modalDisplayed: false,
5967
modalOpaque: false,
68+
submitLocked: false,
6069
};
6170
},
6271
watch: {
@@ -70,7 +79,37 @@ export default {
7079
},
7180
},
7281
methods: {
82+
lockAndSubmit(event) {
83+
this.submitLocked = true;
84+
85+
let el = event && event.target ? event.target : null;
86+
if (!el) return;
87+
88+
const form = el.closest && el.closest("form");
89+
90+
if (form) {
91+
if (typeof form.requestSubmit === "function") {
92+
form.requestSubmit();
93+
return;
94+
}
95+
96+
try {
97+
const submitEvent = new Event("submit", { bubbles: true, cancelable: true });
98+
const canceled = !form.dispatchEvent(submitEvent);
99+
if (!canceled) {
100+
if (typeof form.submit === "function") {
101+
form.submit();
102+
}
103+
}
104+
} catch (e) {
105+
if (typeof form.submit === "function") {
106+
form.submit();
107+
}
108+
}
109+
}
110+
},
73111
async openModal() {
112+
this.submitLocked = false;
74113
this.modalDisplayed = true;
75114
await new Promise((resolve) => setTimeout(resolve, 20)); //hacky...
76115
this.$nextTick(() => {
@@ -81,6 +120,7 @@ export default {
81120
this.modalOpaque = false;
82121
await new Promise((resolve) => setTimeout(resolve, 200)); // super hacky
83122
this.modalDisplayed = false;
123+
this.submitLocked = false;
84124
this.$emit("update:modelValue", false);
85125
},
86126
},

0 commit comments

Comments
 (0)