|
18 | 18 | <script type="text/babel"> |
19 | 19 | 'use strict'; |
20 | 20 |
|
21 | | - function Embeddable(props: { token: string }) { |
22 | | - return React.createElement('em-beddable', props); |
| 21 | + function Embeddable({onComponentsLoad, ...props}) { |
| 22 | + const ref = React.useRef(null); |
| 23 | + |
| 24 | + React.useEffect(() => { |
| 25 | + ref.current.addEventListener('componentsLoad', onComponentsLoad); |
| 26 | + return () => { |
| 27 | + ref.current.removeEventListener('componentsLoad', onComponentsLoad); |
| 28 | + }; |
| 29 | + }, []); |
| 30 | + return React.createElement('em-beddable', {...props, ref}); |
23 | 31 | } |
24 | 32 |
|
25 | | - class TestComponent extends React.Component { |
26 | | - constructor(props) { |
27 | | - super(props); |
28 | | - this.state = { load: false }; |
29 | | - } |
| 33 | + const TestComponent = () => { |
| 34 | + const [load, setLoad] = React.useState(false); |
| 35 | + const [isLoading, setIsLoading] = React.useState(false); |
30 | 36 |
|
31 | | - render() { |
32 | | - if (this.state.load) { |
33 | | - return <Embeddable |
34 | | - token="eyJhbGciOiJIUzI1NiJ9.eyJzZWN1cml0eVRva2VuSWQiOiJmZWE5NDA0Ny1lMjhhLTQ5ZGQtOGQ0YS1mZDdjNDY2MDE0YzUiLCJpYXQiOjE3MDc3Njk3ODEsImV4cCI6MTcwODM3NDU4MX0.IglDlXs9JymBwLrpPUMREpSkClkjY0xuJmoXgHwiqLk" |
35 | | - />; |
| 37 | + if (load) { |
| 38 | + return ( |
| 39 | + <div> |
| 40 | + {isLoading && <div>Loading dashboard...</div>} |
| 41 | + <Embeddable |
| 42 | + token="eyJhbGciOiJIUzI1NiJ9.eyJzZWN1cml0eVRva2VuSWQiOiJlNWQ2ZWFmZS0yMDMzLTRkYjgtYTdiYy1mYTAyOTkzYjczZGQiLCJpYXQiOjE3MDg1OTA5MDgsImV4cCI6MTcwOTE5NTcwOH0.ccj1bNbT3KOmYCu7Oi7JWT7Yea1vo0Q5xN30mvA9i64" |
| 43 | + onComponentsLoad={() => setIsLoading(false)} |
| 44 | + /> |
| 45 | + </div> |
| 46 | + ); |
36 | 47 | } |
37 | 48 |
|
38 | 49 | return ( |
39 | | - <button onClick={() => this.setState({ load: true })}> |
| 50 | + <button onClick={() => { |
| 51 | + setIsLoading(true); |
| 52 | + setLoad(true); |
| 53 | + }}> |
40 | 54 | Load my dashboard |
41 | 55 | </button> |
42 | 56 | ); |
43 | | - } |
44 | 57 | } |
45 | 58 |
|
46 | 59 | const domContainer = document.querySelector('#test-container'); |
|
0 commit comments