HacklabJKL / website

Hacklab Jyväskylä web site
https://uusi.jyvaskyla.hacklab.fi
0 stars 0 forks source link

More robust visitor list #5

Closed zouppen closed 7 years ago

zouppen commented 7 years ago

Sometimes it just says "Loading...". The script has no error handling.

jyrama commented 7 years ago

Patch for corresponding widget, here hypothetically visitor-update.js, with updated XMLHttpRequest usage.

--- visitor-update.js.old   2017-10-24 02:15:59.022493909 +0300
+++ visitor-update.js   2017-10-24 03:05:28.061735734 +0300
@@ -5,7 +5,13 @@

     var ul = document.getElementById('visitors');
     var f = new Intl.DateTimeFormat(ul.getAttribute("data-locale"), { hour: 'numeric', minute: 'numeric'});
-    var visitors = JSON.parse(this.responseText);
+    
+    var visitors = req.response;
+    if (visitors === null) {
+        console.log('Warning: visitor data response was null!');
+        return;
+    }
+    
     document.getElementById('visitors_load').classList.add('hide');

     if (visitors.length === 0) {
@@ -59,4 +65,5 @@
 var req = new XMLHttpRequest();
 req.addEventListener("load", tryUpdate);
 req.open("GET", "https://hacklab.ihme.org/visitors/api/v1/visitors?format=json");
+req.responseType = 'json';
 req.send();
zouppen commented 7 years ago

Sorry about the missing code. Merged.