-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task/issue 356 Extendable App Templates (#382)
* task: extend app-template * fix: lint issues * test: fix broken tests * docs: update documentation with new stripped down app-template info * fix: cleanup * fix: make requirements more clear * fix: formatting * fix: conventional file names/paths * test: fix example * fix: change base-app to app-root * fix: content wrapper prefix * test: update tests Co-authored-by: hutchgrant <[email protected]> Co-authored-by: Owen Buckley <[email protected]>
- Loading branch information
1 parent
78190a4
commit 7bbdb17
Showing
12 changed files
with
147 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,13 @@ | ||
import { html, LitElement } from 'lit-element'; | ||
import { connectRouter } from 'lit-redux-router'; | ||
import { applyMiddleware, createStore, compose as origCompose, combineReducers } from 'redux'; | ||
import { lazyReducerEnhancer } from 'pwa-helpers/lazy-reducer-enhancer.js'; | ||
import thunk from 'redux-thunk'; | ||
import client from '@greenwood/cli/data/client'; | ||
import ConfigQuery from '@greenwood/cli/data/queries/config'; | ||
import GraphQuery from '@greenwood/cli/data/queries/graph'; | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
const compose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || origCompose; | ||
|
||
const store = createStore((state) => state, | ||
compose(lazyReducerEnhancer(combineReducers), applyMiddleware(thunk)) | ||
); | ||
|
||
import '../index/index'; | ||
|
||
connectRouter(store); | ||
|
||
class AppComponent extends LitElement { | ||
|
||
async connectedCallback() { | ||
super.connectedCallback(); | ||
const route = window.location.pathname; | ||
const response = await Promise.all([ | ||
await client.query({ | ||
query: ConfigQuery | ||
}), | ||
await client.query({ | ||
query: GraphQuery | ||
}) | ||
]); | ||
const { config } = response[0].data; | ||
const currentPage = response[1].data.graph.filter((page) => { | ||
return route === page.link; | ||
})[0]; | ||
|
||
const currentPageTitleSuffix = !currentPage || currentPage.link === '/' | ||
? '' | ||
: ` - ${currentPage.title}`; | ||
const fullTitle = `${config.title}${currentPageTitleSuffix}`; | ||
|
||
this.setDocumentTitle(fullTitle); | ||
this.setMeta(config.meta, currentPage); | ||
} | ||
|
||
setDocumentTitle(title = '') { | ||
const head = document.head; | ||
const titleElement = head.getElementsByTagName('title')[0]; | ||
|
||
titleElement.innerHTML = title; | ||
} | ||
|
||
setMeta(meta = [], currentPage = {}) { | ||
let header = document.head; | ||
|
||
meta.forEach(metaItem => { | ||
const metaType = metaItem.rel // type of meta | ||
? 'rel' | ||
: metaItem.name | ||
? 'name' | ||
: 'property'; | ||
const metaTypeValue = metaItem[metaType]; // value of the meta | ||
let meta = document.createElement('meta'); | ||
|
||
if (metaType === 'rel') { | ||
// change to a <link> tag instead | ||
meta = document.createElement('link'); | ||
|
||
meta.setAttribute('rel', metaTypeValue); | ||
meta.setAttribute('href', metaItem.href); | ||
} else { | ||
const metaContent = metaItem.property === 'og:url' | ||
? `${metaItem.content}${currentPage.link}` | ||
: metaItem.content; | ||
|
||
meta.setAttribute(metaType, metaItem[metaType]); | ||
meta.setAttribute('content', metaContent); | ||
} | ||
|
||
const oldmeta = header.querySelector(`[${metaType}="${metaTypeValue}"]`); | ||
|
||
// rehydration | ||
if (oldmeta) { | ||
header.replaceChild(meta, oldmeta); | ||
} else { | ||
header.appendChild(meta); | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
return html` | ||
MYROUTES | ||
<lit-route><h1>404 Not found</h1></lit-route> | ||
MYROUTES | ||
<lit-route><h1>404 Not found</h1></lit-route> | ||
`; | ||
} | ||
} | ||
|
||
customElements.define('eve-app', AppComponent); | ||
customElements.define('eve-app', AppComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { html, LitElement } from 'lit-element'; | ||
import { connectRouter } from 'lit-redux-router'; | ||
import { applyMiddleware, createStore, compose as origCompose, combineReducers } from 'redux'; | ||
import { lazyReducerEnhancer } from 'pwa-helpers/lazy-reducer-enhancer.js'; | ||
import thunk from 'redux-thunk'; | ||
import client from '@greenwood/cli/data/client'; | ||
import ConfigQuery from '@greenwood/cli/data/queries/config'; | ||
import GraphQuery from '@greenwood/cli/data/queries/graph'; | ||
import '@greenwood/cli/templates/app-template'; | ||
// eslint-disable-next-line no-underscore-dangle | ||
const compose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || origCompose; | ||
|
||
const store = createStore((state) => state, | ||
compose(lazyReducerEnhancer(combineReducers), applyMiddleware(thunk)) | ||
); | ||
|
||
import '../index/index'; | ||
|
||
connectRouter(store); | ||
|
||
class BaseAppComponent extends LitElement { | ||
|
||
async connectedCallback() { | ||
super.connectedCallback(); | ||
const route = window.location.pathname; | ||
const response = await Promise.all([ | ||
await client.query({ | ||
query: ConfigQuery | ||
}), | ||
await client.query({ | ||
query: GraphQuery | ||
}) | ||
]); | ||
const { config } = response[0].data; | ||
const currentPage = response[1].data.graph.filter((page) => { | ||
return route === page.link; | ||
})[0]; | ||
|
||
const currentPageTitleSuffix = !currentPage || currentPage.link === '/' | ||
? '' | ||
: ` - ${currentPage.title}`; | ||
const fullTitle = `${config.title}${currentPageTitleSuffix}`; | ||
|
||
this.setDocumentTitle(fullTitle); | ||
this.setMeta(config.meta, currentPage); | ||
} | ||
|
||
setDocumentTitle(title = 'test') { | ||
const head = document.head; | ||
const titleElement = head.getElementsByTagName('title')[0]; | ||
|
||
titleElement.innerHTML = title; | ||
} | ||
|
||
setMeta(meta = [], currentPage = {}) { | ||
let header = document.head; | ||
|
||
meta.forEach(metaItem => { | ||
const metaType = metaItem.rel // type of meta | ||
? 'rel' | ||
: metaItem.name | ||
? 'name' | ||
: 'property'; | ||
const metaTypeValue = metaItem[metaType]; // value of the meta | ||
let meta = document.createElement('meta'); | ||
|
||
if (metaType === 'rel') { | ||
// change to a <link> tag instead | ||
meta = document.createElement('link'); | ||
|
||
meta.setAttribute('rel', metaTypeValue); | ||
meta.setAttribute('href', metaItem.href); | ||
} else { | ||
const metaContent = metaItem.property === 'og:url' | ||
? `${metaItem.content}${currentPage.link}` | ||
: metaItem.content; | ||
|
||
meta.setAttribute(metaType, metaItem[metaType]); | ||
meta.setAttribute('content', metaContent); | ||
} | ||
|
||
const oldmeta = header.querySelector(`[${metaType}="${metaTypeValue}"]`); | ||
|
||
// rehydration | ||
if (oldmeta) { | ||
header.replaceChild(meta, oldmeta); | ||
} else { | ||
header.appendChild(meta); | ||
} | ||
}); | ||
} | ||
|
||
createRenderRoot() { | ||
return this; | ||
} | ||
|
||
render() { | ||
return html` | ||
<eve-app></eve-app> | ||
`; | ||
} | ||
} | ||
|
||
customElements.define('app-root', BaseAppComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
|
||
<body> | ||
|
||
<eve-app></eve-app> | ||
<app-root></app-root> | ||
|
||
</body> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 2 additions & 38 deletions
40
packages/cli/test/cases/build.default.workspace-template-app/src/templates/app-template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.