From d1f637668c071af5951d56eee1d7e1b7aeed621b Mon Sep 17 00:00:00 2001
From: James Arcus <jimbo@ucc.asn.au>
Date: Tue, 17 Sep 2024 20:49:25 +0800
Subject: [PATCH] Async load the door opener

---
 door_open.html | 10 ++++++++++
 main.js        |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/door_open.html b/door_open.html
index f08a213..7ee6ed6 100644
--- a/door_open.html
+++ b/door_open.html
@@ -6,6 +6,7 @@
   <body>
     <h1>UCC Door Status</h1>
     <p>The door is currently open!</p>
+    <p>Opened by: <span id="opener">a mysterious entity</span></p>
     <img
       src="/gary.jpg"
       width="574"
@@ -13,6 +14,15 @@
       alt="A wild Gary lurking in the open doorway to the UCC clubroom."
       title="A wild Gary lurking in the open doorway to the UCC clubroom."
       ></img>
+      <script async>
+        fetch('/opener').then(response => {
+          response.json().then(opener => {
+            if (opener) {
+              document.getElementById('opener').innerText = opener;
+            }
+          });
+        });
+      </script>
   </body>
 </html>
 
diff --git a/main.js b/main.js
index 8cb769a..b26ede4 100644
--- a/main.js
+++ b/main.js
@@ -105,7 +105,7 @@ const server = http.createServer((req, res) => {
       return send_json(res, 500, null);
     }
 
-    return send_json(res, 200, doorOpener);
+    return send_json(res, 200, doorState ? doorOpener : null);
   }
 
   if (req.url === '/gary.jpg') {
-- 
GitLab