From 72ab6decfbf237e5f5dc1957e70c1f02671ff763 Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 11 Oct 2021 14:35:43 -0700 Subject: [PATCH] + Missing data safety in index.ts Basically we want sane defaults in case a backend decides to implode on itself --- ts/index.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ts/index.ts b/ts/index.ts index a777ca0..0457f48 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -4,17 +4,27 @@ class Category { constructor(raw: any) { this.name = raw['name'] this.thumbnail_b64 = raw['thumbnail'] + + // Setting some defaults + if(!this.name) { this.name = null } + if(!this.thumbnail_b64) { this.thumbnail_b64 = null } } title_link() : HTMLHeadingElement { let container = document.createElement('h2') let link = document.createElement('a') - link.href = `/collection?c=${this.name}` - link.text = this.name + if(this.name) { + link.href = `/category/${this.name}` + link.text = this.name + } else { + link.href = '#' + link.text = 'this nigga eatin beans' + } container.appendChild(link) + return container } @@ -22,6 +32,7 @@ class Category { let nail = document.createElement('img') nail.className = 'pure-img' + // Modern browser's should be able to cache this request if(!(this.thumbnail_b64 == null || this.thumbnail_b64.length == 0)) { nail.setAttribute('src', `data:image/jpg;base64,${this.thumbnail_b64}`) } else{ @@ -47,22 +58,27 @@ class Category { } } + function ready_handler(e?: Event) : Event { // Only let this make a get request once we're ready on the page if(document.readyState != 'complete') { return e } // All we do here is basically make a get request to /api/categories - const endpoint = 'http://localhost/api/categories' + const endpoint = window.location.origin + '/api/categories' let xml = new XMLHttpRequest() + // TODO: change this to an async request xml.open('GET', endpoint, false) // sync request xml.send(null) // nothing required for stuff if(xml.getResponseHeader('Content-Type') == 'application/json') { let raw = JSON.parse(xml.responseText) - for(const cat_raw of raw['categories']) { + console.log(raw) + for(const cat_raw of raw) { let cat = new Category(cat_raw) cat.as_div() } + } else { + console.log(xml.getResponseHeader('Content-Type')) } return e }