]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/blobdiff - templates/main.js
Add comment why we logout after pressing "A" key
[coffee/coffee-flask.git] / templates / main.js
index 417dc40f8d88edee6ad278ec6a0bd31590916361..793c590f9c5e090374543762b75c45173dfe5bd9 100644 (file)
@@ -8,6 +8,7 @@ var logoutTimer;
 var reloadTimer = undefined;
 var id_user;                            // ID of the user who is to be accounted for the next coffee
 var identifier_registration = false;    // true if identifier is supposed to be registered for user
+var eventMsg = undefined;               // Feedback message about the last event performed by the user
 
 console.log("hello from flask");
 //sendJSON("{\"type\":\"empty\"}");
@@ -46,9 +47,13 @@ function updateUI()
             return;
         }
 
+        if (eventMsg !== undefined) {
+            update("eventMsg", eventMsg);
+            eventMsg = undefined;
+        }
         if (id_user !== undefined) {
             document.getElementById("nextStep").innerHTML = "Now select a beverage on the coffee machineā€¦";
-        } else {
+        } else if (flavorChosen !== undefined) {
             document.getElementById("nextStep").innerHTML = "Enjoy your " + flavorChosen + "!";
         }
 
@@ -56,7 +61,7 @@ function updateUI()
             document.getElementById("logout_button").innerHTML = '<br>logout<br>(' + timeToLogout + ' s)';
     }
     catch (err) {
-        console.log("Error: ", err);
+        console.log("updateUI error: ", err);
     }
 }
 
@@ -68,7 +73,7 @@ function hiddenUpdateRemote(json, time = new Date()) {
             break;
         case "rfid":
             if (identifier_registration) {
-                ajax("POST", "user/identifier/add", JSON.stringify({ id: msg.uid }), "user");
+                ajax("POST", "user/identifier/add", JSON.stringify({ id: msg.uid }), "content");
 
                 addIdentifier_finish();
             } else {
@@ -80,6 +85,10 @@ function hiddenUpdateRemote(json, time = new Date()) {
                 var flavor = getFlavor(msg.key);
                 if (flavor !== "") {
                     addCoffee(flavor, time);
+                } else if (msg.key == "A") {
+                    // When pressing 'Menu' key, logout to not count
+                    // subsequent keys as coffee orders.
+                    logout();
                 }
             }
             break;
@@ -101,6 +110,10 @@ function loadRemote(string) {
                 replayOfflineQueue();
                 updateUI();
                 clearTimeout(reloadTimer);
+                if (id_user)
+                    logout();
+                else
+                    ajax("GET", "home", "", "content"); // Load home screen on first load or reload
             } else {
                 // Cancel current timer for the case when loadRemote()
                 // was called multiple times (e.g. multiple ajax()
@@ -160,17 +173,18 @@ function ajax(method, route, data, id) {
 
 
 function login(id) {
-    ajax("POST", "login", id, "user");
+    ajax("POST", "login", id, "content");
     id_user = id;
     countingTimeLogout(120);
 }
 
 function logout() {
     sendReset();
-    ajax("GET", "logout", "", "user");
+    ajax("GET", "logout", "", "content");
     id_user = undefined;
     timeToLogout = undefined;
     identifier_registration = false;
+    window.scrollTo(0, 0); // Scroll up
 }
 
 function countingTimeLogout(count_time)
@@ -186,7 +200,7 @@ function countingTimeLogout(count_time)
 }
 
 function renameUser() {
-    ajax("GET", "user/rename?name=" +  document.getElementById("username").value, "", "user");
+    ajax("GET", "user/rename?name=" +  document.getElementById("username").value, "", "content");
 }
 
 function getFlavor(letter) {
@@ -205,13 +219,28 @@ function addCoffee(flavor, time = new Date()) {
         flavor: flavor
     });
     if (id_user) {
-        ajax("POST", "coffee/add", data, "user");
+        ajax("POST", "coffee/add", data, "content");
         flavorChosen = flavor;
         id_user = undefined;
         countingTimeLogout(10); //mean 10 seconds
     }
 }
 
+
+function addEvent(event_name, action_msg, time = new Date()) {
+    var data = JSON.stringify({
+        time: time.toISOString(),
+        event_name: event_name,
+        uid: id_user
+    });
+    if (id_user) {
+        eventMsg = "You have " + action_msg + ". Thanks!"
+        ajax("POST", "event", data, "content");
+        window.scrollTo(0, 0); // Scroll up
+    }
+}
+
+
 function addIdentifier_start() {
     identifier_registration = true;
     document.getElementById("addIdentifier").disabled = true;
@@ -227,7 +256,7 @@ function addIdentifier_finish() {
 }
 
 function disableIdentifier(id) {
-    ajax("POST", "user/identifier/disable", JSON.stringify({ id: id }), "user");
+    ajax("POST", "user/identifier/disable", JSON.stringify({ id: id }), "content");
 }
 
 function renameIdentifier(i) {
@@ -236,7 +265,7 @@ function renameIdentifier(i) {
         name: document.getElementById("identifier_name_" + i).value
     });
 
-    ajax("POST", "user/identifier/rename", data, "user");
+    ajax("POST", "user/identifier/rename", data, "content");
 }
 
 function sendLog(json) {