-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (55 loc) · 1.63 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import '@babel/polyfill'
import React from 'react'
import ReactDOM from 'react-dom'
import Aragon, { providers } from '@aragon/client'
import App from './App'
class ConnectedApp extends React.Component {
state = {
app: new Aragon(new providers.WindowMessage(window.parent)),
observable: null,
userAccount: '',
}
componentDidMount() {
window.addEventListener('message', this.handleWrapperMessage)
// If using Parcel, reload instead of using HMR.
// HMR makes the app disconnect from the wrapper and the state is empty until a reload
// See: https://github.com/parcel-bundler/parcel/issues/289
if (module.hot) {
module.hot.dispose(() => {
window.location.reload();
})
}
}
componentWillUnmount() {
window.removeEventListener('message', this.handleWrapperMessage)
}
// handshake between Aragon Core and the iframe,
// since iframes can lose messages that were sent before they were ready
handleWrapperMessage = ({ data }) => {
if (data.from !== 'wrapper') {
return
}
if (data.name === 'ready') {
const { app } = this.state
this.sendMessageToWrapper('ready', true)
this.setState({
observable: app.state(),
})
app.accounts().subscribe(accounts => {
this.setState({
userAccount: accounts[0],
})
})
}
}
sendMessageToWrapper = (name, value) => {
window.parent.postMessage({ from: 'app', name, value }, '*')
}
render() {
return <App {...this.state} />
}
}
ReactDOM.render(
<ConnectedApp />,
document.getElementById('root')
)