UNPKG

3.1 kBJavaScriptView Raw
1import React from 'react'
2import ReactDOM from 'react-dom'
3
4import { EmailArticleData, EmailArticleView } from '..'
5
6import DemosConfig from './demos-config'
7
8const randomContent = (
9 <div>
10 <p><em>Hint: Click on the email icon above</em></p>
11 <p>Lorem ipsum dolor sit amet, ea nonumy lucilius nam, pri ei tale soluta. Et nibh minimum duo, mea ad omnis feugait argumentum, doming deseruisse est at. Duo volumus detracto ne. Nam consul albucius sapientem id, decore prodesset consequuntur est ne. Ex dicit eirmod vituperata vel.</p>
12 </div>
13)
14
15export default class extends React.Component {
16
17 onDemosConfigChange (mode, apiResponses, customMessage) {
18 // get rid of old stuff and do it all over again
19 Array.from(document.querySelectorAll('[data-n-article-email-container]')).forEach(view => view.innerHTML = null)
20
21 // non-React `next-article` will lazily load this React component and so we're doing similar here
22 this.views = {}
23 this.data = new EmailArticleData(mode)
24
25 // mock the API calls
26 Object.keys(apiResponses).map(api => this.data.api[api] = apiResponses[api])
27
28 // set the custom message flag value
29 this.data.customMessage = customMessage
30 }
31
32 onToggleOpen (id) {
33 // non-React `next-article` will lazily load this React component and so we're doing similar here
34 const isTop = id === 'top'
35 // lazily load the view
36 if (!this.views[id]) {
37 const props = {
38 mode: this.mode,
39 isTop: isTop,
40 store: this.data.store,
41 actions: this.data.actions,
42 dispatch: this.data.dispatch,
43 customMessage: this.data.customMessage
44 }
45 this.views[id] = React.createElement(EmailArticleView, props)
46 const container = document.querySelector(`[data-n-article-email-${id}-container]`)
47 ReactDOM.render(this.views[id], container)
48 }
49 // toggle showing/hiding of the view
50 if (isTop) this.data.dispatch(this.data.actions.toggleOpenTop())
51 else this.data.dispatch(this.data.actions.toggleOpenBottom())
52 }
53
54 render () {
55 // please note that we're not using React ways of doing things here
56 // as `next-article` doesn't use React
57 return (
58 <div className="article" data-content-id="737195aa-1347-11e6-839f-292294709880">
59 <div className="demos__config">
60 <DemosConfig
61 onConfigChange={(mode, apiResponses, customMessage) => {
62 return this.onDemosConfigChange(mode, apiResponses, customMessage)}
63 }
64 />
65 </div>
66 <div className="demos__article">
67 <h1 className="demos__article-title">Article title</h1>
68 <h2>Top toolbar</h2>
69 <button onClick={() => this.onToggleOpen('top')} type="button"
70 className="o-buttons o-buttons--medium demos__email">Email</button>
71 <div data-n-article-email-container data-n-article-email-top-container></div>
72 {randomContent}
73 <h2>Bottom toolbar</h2>
74 <button onClick={() => this.onToggleOpen('bottom')} type="button"
75 className="o-buttons o-buttons--medium demos__email">Email</button>
76 <div data-n-article-email-container data-n-article-email-bottom-container></div>
77 {randomContent}
78 </div>
79 </div>
80 )
81 }
82
83}