From 255e4dd0aa0b5e2265233691f97528752411ded1 Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <yohann.danello@gmail.com>
Date: Sun, 9 Aug 2020 13:19:27 +0200
Subject: [PATCH] Lock interfaces when a transfer is performed to prevent spam
 click accidents

---
 static/js/consos.js   | 33 +++++++++++++++++++++++++++------
 static/js/transfer.js | 25 ++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/static/js/consos.js b/static/js/consos.js
index 24d0d0fb..32dd44a9 100644
--- a/static/js/consos.js
+++ b/static/js/consos.js
@@ -1,6 +1,9 @@
 // Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
 // SPDX-License-Identifier: GPL-3.0-or-later
 
+// When a transaction is performed, lock the interface to prevent spam clicks.
+let LOCK = false;
+
 /**
  * Refresh the history table on the consumptions page.
  */
@@ -35,8 +38,11 @@ $(document).ready(function() {
             note_list_obj.html("");
 
             buttons.forEach(function(button) {
-                $("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons,
-                    "consos_list"));
+                $("#conso_button_" + button.id).click(function() {
+                    if (LOCK)
+                        return;
+                    removeNote(button, "conso_button", buttons,"consos_list");
+                });
             });
         }
     });
@@ -52,8 +58,11 @@ $(document).ready(function() {
                 $("#note_list").html(consos_list_obj.html());
                 consos_list_obj.html("");
                 buttons.forEach(function(button) {
-                    $("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons,
-                        "note_list"));
+                    $("#conso_button_" + button.id).click(function() {
+                        if (LOCK)
+                            return;
+                        removeNote(button, "conso_button", buttons,"note_list");
+                    });
                 });
             }
             else {
@@ -127,7 +136,11 @@ function addConso(dest, amount, type, category_id, category_name, template_id, t
         $("#" + list).html(html);
 
         buttons.forEach(function(button) {
-            $("#conso_button_" + button.id).click(removeNote(button, "conso_button", buttons, list));
+            $("#conso_button_" + button.id).click(function() {
+                if (LOCK)
+                    return;
+                removeNote(button, "conso_button", buttons, list);
+            });
         });
     }
     else
@@ -148,6 +161,7 @@ function reset() {
     $("#profile_pic_link").attr("href", "#");
     refreshHistory();
     refreshBalance();
+    LOCK = false;
 }
 
 
@@ -155,6 +169,11 @@ function reset() {
  * Apply all transactions: all notes in `notes` buy each item in `buttons`
  */
 function consumeAll() {
+    if (LOCK)
+        return;
+
+    LOCK = true;
+
     let error = false;
 
     if (notes_display.length === 0) {
@@ -168,8 +187,10 @@ function consumeAll() {
         error = true;
     }
 
-    if (error)
+    if (error) {
+        LOCK = false;
         return;
+    }
 
     notes_display.forEach(function(note_display) {
         buttons.forEach(function(button) {
diff --git a/static/js/transfer.js b/static/js/transfer.js
index 2ee879dc..8df25c42 100644
--- a/static/js/transfer.js
+++ b/static/js/transfer.js
@@ -1,3 +1,5 @@
+let LOCK = true;
+
 sources = [];
 sources_notes_display = [];
 dests = [];
@@ -42,6 +44,8 @@ function reset(refresh=true) {
         refreshBalance();
         refreshHistory();
     }
+
+    LOCK = false;
 }
 
 $(document).ready(function() {
@@ -91,6 +95,9 @@ $(document).ready(function() {
     let dest = $("#dest_note");
 
     $("#type_transfer").click(function() {
+        if (LOCK)
+            return;
+
         $("#source_me_div").removeClass('d-none');
         $("#source_note").removeClass('is-invalid');
         $("#dest_note").removeClass('is-invalid');
@@ -102,6 +109,9 @@ $(document).ready(function() {
     });
 
     $("#type_credit").click(function() {
+        if (LOCK)
+            return;
+
         $("#source_me_div").addClass('d-none');
         $("#source_note").removeClass('is-invalid');
         $("#dest_note").removeClass('is-invalid');
@@ -122,6 +132,9 @@ $(document).ready(function() {
     });
 
     $("#type_debit").click(function() {
+        if (LOCK)
+            return;
+
         $("#source_me_div").addClass('d-none');
         $("#source_note").removeClass('is-invalid');
         $("#dest_note").removeClass('is-invalid');
@@ -165,6 +178,9 @@ $(document).ready(function() {
     location.hash = "";
 
     $("#source_me").click(function() {
+        if (LOCK)
+            return;
+
         // Shortcut to set the current user as the only emitter
         sources_notes_display.length = 0;
         sources.length = 0;
@@ -198,6 +214,11 @@ $(document).ready(function() {
 });
 
 $("#btn_transfer").click(function() {
+    if (LOCK)
+        return;
+
+    LOCK = true;
+
     let error = false;
 
     let amount_field = $("#amount");
@@ -237,8 +258,10 @@ $("#btn_transfer").click(function() {
         error = true;
     }
 
-    if (error)
+    if (error) {
+        LOCK = false;
         return;
+    }
 
     let reason = reason_field.val();
 
-- 
GitLab