From 2b2a9a67581474d13ce905d568f527877ca0e3f5 Mon Sep 17 00:00:00 2001
From: Lorenz Stechauner <lorenz.stechauner@necronda.net>
Date: Mon, 24 May 2021 15:46:02 +0200
Subject: [PATCH] Small js fixes

---
 www/res/js/modules/locutus.js | 39 +++++++++++++++--------------------
 www/res/js/modules/usimp.js   | 17 ++++++++-------
 2 files changed, 27 insertions(+), 29 deletions(-)

diff --git a/www/res/js/modules/locutus.js b/www/res/js/modules/locutus.js
index ec89996..bf7e893 100644
--- a/www/res/js/modules/locutus.js
+++ b/www/res/js/modules/locutus.js
@@ -76,8 +76,6 @@ export class App {
                 console.warn(`Invalid url hash #${hash}`);
                 return defaultCase();
         }
-
-        //this.setHash(hash);
     }
 
     async login(accountName, domainName, password) {
@@ -161,7 +159,7 @@ export class App {
                 <button type="submit">Login</button>
             </form>`;
 
-        win.getElementsByTagName("form")[0].addEventListener("submit", async (event) => {
+        win.getElementsByTagName("form")[0].addEventListener("submit", async event => {
             event.preventDefault();
             const form = event.target;
             for (const e of form) {
@@ -169,28 +167,24 @@ export class App {
                 e.removeAttribute("invalid");
             }
 
-            for (const d of form.getElementsByTagName("div")) {
-                form.removeChild(d);
-            }
-
-            function formError(msg) {
-                const div = document.createElement("div");
-                div.classList.add("error");
-                div.innerText = msg;
-                form.appendChild(div);
-            }
-
             try {
-                await this.login(form.account.value, form.domain.value, form.password.value);
+                try {
+                    await this.login(form.account.value, form.domain.value, form.password.value);
+                } finally {
+                    for (const d of form.getElementsByTagName("div")) form.removeChild(d);
+                    for (const e of form) e.disabled = false;
+                }
             } catch (error) {
                 if (error.toString() === "Error: unable to authenticate") {
                     document.getElementsByName("account")[0].setAttribute("invalid", "invalid");
                 } else {
                     document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
                 }
-                formError(error.toString());
-            } finally {
-                for (const e of form) e.disabled = false;
+
+                const div = document.createElement("div");
+                div.classList.add("error");
+                div.innerText = error.toString();
+                form.appendChild(div);
             }
         });
 
@@ -217,13 +211,14 @@ export class App {
             </div>`;
 
         const input = document.getElementsByName("message")[0];
-        input.addEventListener("keyup", (event) => {
+        input.addEventListener("keyup", async event => {
             if (event.key === "Enter" && input.value.length > 0) {
-                this.session.sendEvent("60nc0XXDIYUh6QzX4p0rMpCdzDmxghZLZk8dLuQh628", {
-                    message: input.value,
-                });
                 this.addMessage(input.value);
+                const val = input.value;
                 input.value = "";
+                await this.session.sendEvent("60nc0XXDIYUh6QzX4p0rMpCdzDmxghZLZk8dLuQh628", {
+                    message: val,
+                });
             }
         });
 
diff --git a/www/res/js/modules/usimp.js b/www/res/js/modules/usimp.js
index 158218e..6c65e43 100644
--- a/www/res/js/modules/usimp.js
+++ b/www/res/js/modules/usimp.js
@@ -25,7 +25,7 @@ export class Domain {
         const controller = new AbortController();
         const timer = setTimeout(() => controller.abort(), 2000);
 
-        let response = await fetch(`https://${domainName}/.well-known/usimp.json`, {
+        const response = await fetch(`https://${domainName}/.well-known/usimp.json`, {
             redirect: "manual",
             signal: controller.signal,
         });
@@ -166,7 +166,7 @@ export class Session {
             }
 
             const startTime = new Date();
-            let response = await fetch(`${this.httpBaseUrl}/${endpoint}`, {
+            const response = await fetch(`${this.httpBaseUrl}/${endpoint}`, {
                 method: "POST",
                 headers: headers,
                 body: JSON.stringify(data),
@@ -175,7 +175,7 @@ export class Session {
             const endTime = new Date();
             clearTimeout(timer);
 
-            let responseData = await response.json();
+            const responseData = await response.json();
             responseData.duration = endTime - startTime;
             return responseData;
         }
@@ -183,16 +183,19 @@ export class Session {
 
     async ping() {
         let result = {"http": null, "ws": null};
-        const res = await this.send("ping", {}, undefined, true);
-        result.http = res.duration;
+        const resHttp = await this.send("ping", {}, undefined, true);
+        result.http = resHttp.duration;
+
         if (this.wsConnection) {
-            await this.wsConnection.ping();
+            const resWs = await this.send("ping", {});
+            result.ws = resWs.duration;
         }
+
         return result;
     }
 
     async authenticate(accountName, password) {
-        let response = await this.send("authenticate", {
+        const response = await this.send("authenticate", {
             type: "password",
             name: accountName,
             password: password,