From 93686c10e1c26db72f5394aeaf5cb3e96412cc1a Mon Sep 17 00:00:00 2001 From: Ben <36240171+benkyd@users.noreply.github.com> Date: Fri, 25 Mar 2022 15:35:45 +0000 Subject: [PATCH] lol Former-commit-id: a7f582489dec8308c131d88464a80720c26b3692 --- client/public/components/compact-listing.mjs | 13 ++++- client/public/components/components.mjs | 34 ++++++++----- .../public/components/css/compact-listing.css | 0 client/public/components/css/storefront.css | 0 client/public/components/product-list.mjs | 48 ++++++++++++++++++ client/public/components/storefront.mjs | 22 +++++---- .../components/templates/compact-listing.html | 1 - .../components/templates/storefront.html | 3 -- client/public/index.html | 4 +- docs/API.md | 3 ++ docs/CLIENT-FRAMEWORK.md | 4 +- src/routes/api.js | 1 + src/routes/sets-router.js | 49 +++++++++++++++++++ 13 files changed, 151 insertions(+), 31 deletions(-) delete mode 100644 client/public/components/css/compact-listing.css delete mode 100644 client/public/components/css/storefront.css create mode 100644 client/public/components/product-list.mjs delete mode 100644 client/public/components/templates/compact-listing.html delete mode 100644 client/public/components/templates/storefront.html diff --git a/client/public/components/compact-listing.mjs b/client/public/components/compact-listing.mjs index 72e9646..35874fa 100644 --- a/client/public/components/compact-listing.mjs +++ b/client/public/components/compact-listing.mjs @@ -9,8 +9,17 @@ class CompactProductListing extends Component { Render() { return { - template: SideLoad('./components/templates/compact-listing.html'), - style: SideLoad('./components/css/compact-listing.css'), + template: ` + {this.state.name} + {this.state.desc} + £{this.state.price} + + `, + style: ` + compact-listing-component { + display: flex; + } + `, }; } diff --git a/client/public/components/components.mjs b/client/public/components/components.mjs index 2863c3b..5efd143 100644 --- a/client/public/components/components.mjs +++ b/client/public/components/components.mjs @@ -2,8 +2,6 @@ // neccesary is fetched from the server const preLoadCache = []; export function SideLoad(path) { - console.log(preLoadCache); - return new Promise((resolve) => { if (preLoadCache[path]) { resolve(preLoadCache[path]); @@ -34,9 +32,9 @@ export class Component extends HTMLElement { } // Override these - Render() { this.__WARN('Render'); } - OnceRendered() { this.__WARN('Render'); } - static __IDENTIFY() { this.__WARN('identify'); } + Render() { Component.__WARN('Render'); } + OnceRendered() { Component.__WARN('Render'); } + static __IDENTIFY() { Component.__WARN('identify'); } connectedCallback() { // set up to watch all attributes for changes @@ -45,9 +43,11 @@ export class Component extends HTMLElement { // if there are any attributes related to the element // be sure to include them in the state to be sure that // they can be resolved + let stateUpdateQueue = { ...this.state }; for (const attribute of this.attributes) { - this.SetState({ ...this.state, [attribute.name]: attribute.value }); + stateUpdateQueue = { ...stateUpdateQueue, [attribute.name]: attribute.value }; } + this.setState(stateUpdateQueue); if (this.attributes.length === 0) { this.__INVOKE_RENDER(); @@ -72,27 +72,36 @@ export class Component extends HTMLElement { attributeChangedCallback(name, newValue) { console.log(`attribute changed: ${name} ${newValue}`); - this.SetState({ ...this.state, [name]: newValue }); + this.setState({ ...this.state, [name]: newValue }); this.__INVOKE_RENDER(); } - get GetState() { + get getState() { return this.state; } - SetState(newState) { + setState(newState) { this.state = newState; this.__INVOKE_RENDER(Object.bind(this)); } async __INVOKE_RENDER() { - const res = this.Render(Object.bind(this)); + let res = this.Render(Object.bind(this)); - if (!res.template || !res.style) { - this.__ERR('no template or style'); + if (res instanceof Promise) { + res = await res; + } + + if (res.template === undefined || res.style === undefined) { + Component.__ERR('no template or style'); return; } + // way to formally update state WITHOUT triggering a re-render + if (res.state) { + this.state = res.state; + } + // if res.template is a promise, we need to wait to resolve it if (res.template instanceof Promise) { res.template = await res.template; @@ -113,6 +122,7 @@ export class Component extends HTMLElement { if (m[1].startsWith('this.state')) { const stateKey = m[1].substring(11); const stateValue = this.state[stateKey]; + console.log('attempting to replace', m[0], 'with', stateValue); if (stateValue === undefined) { continue; } diff --git a/client/public/components/css/compact-listing.css b/client/public/components/css/compact-listing.css deleted file mode 100644 index e69de29..0000000 diff --git a/client/public/components/css/storefront.css b/client/public/components/css/storefront.css deleted file mode 100644 index e69de29..0000000 diff --git a/client/public/components/product-list.mjs b/client/public/components/product-list.mjs new file mode 100644 index 0000000..eddd16b --- /dev/null +++ b/client/public/components/product-list.mjs @@ -0,0 +1,48 @@ +import { RegisterComponent, Component } from './components.mjs'; + +class ProductList extends Component { + static __IDENTIFY() { return 'product-list'; } + + constructor() { + super(ProductList); + } + + async Render() { + const route = this.state.getroute; + const products = await fetch(route).then(response => response.json()); + + return { + template: ` +

{this.state.title}

+
+ ${products.data.map(product => { + return ``; + })} +
+ `, + style: ` + .product-list { + display: flex; + flex-wrap: wrap; + margin: 0 auto; + max-width: 800px; + } + `, + state: { + ...this.getState, + products, + }, + }; + } + + + OnceRendered() { + + + } +} + +RegisterComponent(ProductList); diff --git a/client/public/components/storefront.mjs b/client/public/components/storefront.mjs index 0c172f4..bce326b 100644 --- a/client/public/components/storefront.mjs +++ b/client/public/components/storefront.mjs @@ -1,4 +1,4 @@ -import { RegisterComponent, Component, SideLoad } from './components.mjs'; +import { RegisterComponent, Component } from './components.mjs'; class StoreFront extends Component { static __IDENTIFY() { return 'storefront'; } @@ -9,18 +9,22 @@ class StoreFront extends Component { Render() { return { - template: SideLoad('./components/templates/storefront.html'), - style: SideLoad('./components/css/storefront.css'), + template: ` + + + `, + style: ` + product-list-component { + display: block; + margin: 0 auto; + }`, }; } OnceRendered() { - const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10']; - for (const item of items) { - const itemElement = document.createElement('compact-listing-component'); - itemElement.setAttribute('item', item); - this.root.appendChild(itemElement); - } + } } diff --git a/client/public/components/templates/compact-listing.html b/client/public/components/templates/compact-listing.html deleted file mode 100644 index 7f2c95f..0000000 --- a/client/public/components/templates/compact-listing.html +++ /dev/null @@ -1 +0,0 @@ -{this.state.item} \ No newline at end of file diff --git a/client/public/components/templates/storefront.html b/client/public/components/templates/storefront.html deleted file mode 100644 index 466b38b..0000000 --- a/client/public/components/templates/storefront.html +++ /dev/null @@ -1,3 +0,0 @@ -
- -
diff --git a/client/public/index.html b/client/public/index.html index b7eb9a3..eb036ca 100644 --- a/client/public/index.html +++ b/client/public/index.html @@ -13,6 +13,7 @@ + @@ -24,8 +25,7 @@ - - +