|
18 | 18 | <script type="text/babel"> |
19 | 19 | 'use strict'; |
20 | 20 |
|
21 | | - function Embeddable({onComponentsLoad, ...props}) { |
| 21 | + function Embeddable({token, onComponentsLoad, variables, onVariablesChange}) { |
22 | 22 | const ref = React.useRef(null); |
23 | | - |
| 23 | + function handleVariableChange(e) { |
| 24 | + if(onVariablesChange) { |
| 25 | + onVariablesChange( |
| 26 | + Object.fromEntries( |
| 27 | + e.detail.map(c => [c.variableName, c.newValue]) |
| 28 | + ) |
| 29 | + ) |
| 30 | + } |
| 31 | + } |
24 | 32 | React.useEffect(() => { |
| 33 | + ref.current.addEventListener('variablesChange', handleVariableChange); |
25 | 34 | ref.current.addEventListener('componentsLoad', onComponentsLoad); |
26 | 35 | return () => { |
27 | 36 | ref.current.removeEventListener('componentsLoad', onComponentsLoad); |
| 37 | + ref.current.removeEventListener('variablesChange', handleVariableChange); |
28 | 38 | }; |
29 | 39 | }, []); |
30 | | - return React.createElement('em-beddable', {...props, ref}); |
| 40 | + return React.createElement('em-beddable', { |
| 41 | + ref, |
| 42 | + token, |
| 43 | + variables: JSON.stringify(variables) |
| 44 | + }); |
31 | 45 | } |
32 | 46 |
|
33 | 47 | const TestComponent = () => { |
34 | 48 | const [load, setLoad] = React.useState(false); |
35 | 49 | const [isLoading, setIsLoading] = React.useState(false); |
| 50 | + const DEFAULT = { 'Text Value': 'Germany' }; |
| 51 | + const [variables, setVariables] = React.useState(DEFAULT) |
36 | 52 |
|
37 | 53 | if (load) { |
38 | 54 | return ( |
39 | 55 | <div> |
40 | 56 | {isLoading && <div>Loading dashboard...</div>} |
41 | | - <Embeddable |
42 | | - token="eyJhbGciOiJIUzI1NiJ9.eyJzZWN1cml0eVRva2VuSWQiOiJlNWQ2ZWFmZS0yMDMzLTRkYjgtYTdiYy1mYTAyOTkzYjczZGQiLCJpYXQiOjE3MDg1OTA5MDgsImV4cCI6MTcwOTE5NTcwOH0.ccj1bNbT3KOmYCu7Oi7JWT7Yea1vo0Q5xN30mvA9i64" |
43 | | - onComponentsLoad={() => setIsLoading(false)} |
44 | | - /> |
| 57 | + <div> |
| 58 | + <div>{JSON.stringify(variables)}</div> |
| 59 | + <button onClick={() => setVariables(DEFAULT)}>reset</button> |
| 60 | + </div> |
| 61 | + <div> |
| 62 | + <Embeddable |
| 63 | + variables={variables} |
| 64 | + onVariablesChange={v => setVariables(v)} |
| 65 | + onComponentsLoad={() => setIsLoading(false)} |
| 66 | + token="eyJhbGciOiJIUzI1NiJ9.eyJzZWN1cml0eVRva2VuSWQiOiIwODQ5MzA3ZC1kNzgxLTQ2ZTItYWVhYy0wMzhlYzBiMWQ2ZTEiLCJpYXQiOjE3MDk4MTU5NDgsImV4cCI6MTcxMDQyMDc0OH0.NojqfJ8sHGLyNAvfPvGtcxAMAPz2FsfA84jriQnJmXc" |
| 67 | + /> |
| 68 | + </div> |
45 | 69 | </div> |
46 | 70 | ); |
47 | 71 | } |
|
0 commit comments