ソースを参照

Add notifier for errors, etc.

Lee Morgan 2 ヶ月 前
親
コミット
b71c5bff06
5 ファイル変更、65 行追加、20 行削除
  1. 9 15
      ui/adminCreate/index.js
  2. 33 0
      ui/index.css
  3. 7 3
      ui/index.html
  4. 14 0
      ui/index.js
  5. 2 2
      ui/login/index.js

+ 9 - 15
ui/adminCreate/index.js

@@ -12,32 +12,26 @@ export default {
         document.getElementById("adminCreateName").focus();
         document.getElementById("adminCreateName").focus();
     },
     },
 
 
-    submit(event){
+    async submit(event){
         event.preventDefault();
         event.preventDefault();
 
 
-        const data = {
+        const body = {
             name: document.getElementById("adminCreateName").value,
             name: document.getElementById("adminCreateName").value,
             email: document.getElementById("adminCreateEmail").value,
             email: document.getElementById("adminCreateEmail").value,
             password: document.getElementById("adminCreatePass").value,
             password: document.getElementById("adminCreatePass").value,
             confirm_password: document.getElementById("adminCreatePassConf").value
             confirm_password: document.getElementById("adminCreatePassConf").value
         };
         };
 
 
-        fetch("/admin/first", {
+        const response = await fetch("/admin/first", {
             method: "POST",
             method: "POST",
             headers: {"Content-Type": "application/json"},
             headers: {"Content-Type": "application/json"},
             credentials: "include",
             credentials: "include",
-            body: JSON.stringify(data)
-        })
-            .then((r)=>{
-                if(!r.ok) return console.log(r.msg);
+            body: JSON.stringify(body)
+        });
 
 
-                return r.json();
-            })
-            .then((response)=>{
-                openPage("login");
-            })
-            .catch((e)=>{
-                console.log(e);
-            });
+        const data = await response.json();
+        if(!response.ok) return notify("error", data.msg);
+
+        openPage("login");
     }
     }
 }
 }

+ 33 - 0
ui/index.css

@@ -82,3 +82,36 @@ body{
     background: var(--bg-base);
     background: var(--bg-base);
     color: var(--text-primary);
     color: var(--text-primary);
 }
 }
+
+#notifier{
+    position: fixed;
+    top: 24px;
+    left: 50%;
+    transform: translateX(-50%);
+    z-index: 1000;
+    display: flex;
+    align-items: center;
+    max-width: min(420px, calc(100% - 48px));
+    background: var(--bg-surface-raised);
+    border-left: 4px solid var(--accent);
+    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
+    padding: 14px 18px;
+}
+
+#notifier.success{
+    border-left-color: var(--success);
+}
+
+#notifier.warning{
+    border-left-color: var(--warning);
+}
+
+#notifier.error{
+    border-left-color: var(--danger);
+}
+
+#notifierMessage{
+    font-size: 14px;
+    line-height: 1.4;
+    color: var(--text-primary);
+}

+ 7 - 3
ui/index.html

@@ -8,7 +8,11 @@
     <style></style>
     <style></style>
 </head>
 </head>
 <body>
 <body>
-    <div id="loginPage" class="page" display="none">
+    <div id="notifier" style="display: none">
+        <p id="notifierMessage"></p>
+    </div>
+
+    <div id="loginPage" class="page" style="display: none">
         <form>
         <form>
             <h1>Login</h1>
             <h1>Login</h1>
 
 
@@ -24,11 +28,11 @@
         </form>
         </form>
     </div>
     </div>
 
 
-    <div id="dashboardPage" class="page" display="none">
+    <div id="dashboardPage" class="page" style="display: none">
         <h1>Dashboard</h1>
         <h1>Dashboard</h1>
     </div>
     </div>
 
 
-    <div id="adminCreatePage" class="page" display="none">
+    <div id="adminCreatePage" class="page" style="display: none">
         <form>
         <form>
             <h1>Admin Creation</h1>
             <h1>Admin Creation</h1>
             <p>Create an admin account in order to use Home Assistant</p>
             <p>Create an admin account in order to use Home Assistant</p>

+ 14 - 0
ui/index.js

@@ -6,6 +6,20 @@ window.adminExists = "true";
 
 
 let pages = document.querySelectorAll(".page");
 let pages = document.querySelectorAll(".page");
 
 
+//Notifier
+notify = (type, msg)=>{
+    const notifier = document.getElementById("notifier");
+    notifier.style.display = "flex";
+    notifier.className = type;
+    console.log("something");
+    console.log(msg);
+    document.getElementById("notifierMessage").textContent = msg;
+
+    setTimeout(()=>{
+        notifier.style.display = "none";
+    }, 7500);
+}
+
 //Page change function
 //Page change function
 openPage = (page)=>{
 openPage = (page)=>{
     for(let i = 0; i < pages.length; i++){
     for(let i = 0; i < pages.length; i++){

+ 2 - 2
ui/login/index.js

@@ -26,9 +26,9 @@ export default {
             body: JSON.stringify(body)
             body: JSON.stringify(body)
         });
         });
         
         
-        if(!response.ok) return console.log(response.msg);
+        const data = await response.json();
+        if(!response.ok) return notify("error", data.msg);
 
 
-        const data = response.json();
         openPage("dashboard");
         openPage("dashboard");
     }
     }
 }
 }