From 9a04001894b4c3952f947d861f4aa7631d3e7ea6 Mon Sep 17 00:00:00 2001 From: heyMP Date: Sat, 2 Mar 2019 13:09:54 -0500 Subject: [PATCH] moved old router. fetching factory --- .../graphql-server/modules/factory/index.js | 2 +- .../ui/components/wcfactory-ui-factories.js | 2 +- .../ui/components/wcfactory-ui-factory.js | 73 +++++++++++++++++++ packages/ui/components/wcfactory-ui.js | 3 + packages/ui/router.js | 10 --- 5 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 packages/ui/components/wcfactory-ui-factory.js delete mode 100644 packages/ui/router.js diff --git a/packages/graphql-server/modules/factory/index.js b/packages/graphql-server/modules/factory/index.js index a7966a5c9..40450c611 100644 --- a/packages/graphql-server/modules/factory/index.js +++ b/packages/graphql-server/modules/factory/index.js @@ -5,7 +5,7 @@ const { factoryOptions, getElements } = require('@wcfactory/common/config') * SDK */ const getFactories = () => factoryOptions().map(i => Object.assign({ name: i.name, location: i.value})) -const getFactory = (name) => getFactories().find(i => i.name === name).map(i => Object.assign({}, i, { factory: name })) +const getFactory = (name) => getFactories().find(i => i.name === name) /** * Define Schema diff --git a/packages/ui/components/wcfactory-ui-factories.js b/packages/ui/components/wcfactory-ui-factories.js index 94df83555..fc43c9073 100644 --- a/packages/ui/components/wcfactory-ui-factories.js +++ b/packages/ui/components/wcfactory-ui-factories.js @@ -37,7 +37,7 @@ class WCFactoryUIFactories extends LitElement { listing of factories `; diff --git a/packages/ui/components/wcfactory-ui-factory.js b/packages/ui/components/wcfactory-ui-factory.js new file mode 100644 index 000000000..1809b6024 --- /dev/null +++ b/packages/ui/components/wcfactory-ui-factory.js @@ -0,0 +1,73 @@ +import { LitElement, html } from 'lit-element'; +import gql from 'graphql-tag' +import client from '../client.js' + +const query = gql` + query($name: ID!) { + factory(name: $name) { + name + location + elements { + name + location + } + } + } +`; + +class WCFactoryUIFactory extends LitElement { + static get properties() { + return { + location: { type: Object }, + loading: { type: Boolean }, + factory: { type: Object } + } + } + + constructor() { + super() + this.factory = null + this.loading = true + } + + render() { + // notice location changed + const { factory } = this.location.params + // update factory + this.fetchFactory(factory) + + // render + if (this.loading) { + return html`loading...` + } + else if (this.factory) { + return html` + Name: ${this.factory.name}
+ Location: ${this.factory.location}
+ Elements: (${this.factory.elements.length}) + + `; + } + } + + fetchFactory(factory) { + try { + client.watchQuery({ + query, + variables: { + name: factory + } + }).subscribe(({ data: { factory }}) => { + this.loading = false + this.factory = factory + }) + } catch (error) { + } + } +} + +customElements.define('wcfactory-ui-factory', WCFactoryUIFactory); \ No newline at end of file diff --git a/packages/ui/components/wcfactory-ui.js b/packages/ui/components/wcfactory-ui.js index cee804d05..7fb580621 100644 --- a/packages/ui/components/wcfactory-ui.js +++ b/packages/ui/components/wcfactory-ui.js @@ -1,6 +1,7 @@ import { LitElement, html } from 'lit-element'; import {Router} from '@vaadin/router'; import './wcfactory-ui-factories.js' +import './wcfactory-ui-factory.js' import './wcfactory-ui-404.js' class WCFactoryUI extends LitElement { @@ -22,6 +23,8 @@ class WCFactoryUI extends LitElement { const router = new Router(outlet); router.setRoutes([ {path: '/', component: 'wcfactory-ui-factories'}, + {path: '/factories', component: 'wcfactory-ui-factory'}, + {path: '/factories/:factory', component: 'wcfactory-ui-factory'}, {path: '(.*)', component: 'wcfactory-ui-404'}, ]); } diff --git a/packages/ui/router.js b/packages/ui/router.js deleted file mode 100644 index 084bfeff9..000000000 --- a/packages/ui/router.js +++ /dev/null @@ -1,10 +0,0 @@ -import {Router} from '@vaadin/router'; - -const outlet = document.getElementById('outlet'); -const router = new Router(outlet); -router.setRoutes([ - {path: '/', component: 'x-home-view'}, - {path: '/users', component: 'x-user-list'}, - {path: '/users/:user', component: 'x-user-profile'}, - {path: '(.*)', component: 'x-not-found-view'}, -]); \ No newline at end of file