Skip to content

Commit 8c16087

Browse files
committed
add variables and onVariablesChange to react example
1 parent 68b8d88 commit 8c16087

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/react-test.html

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,54 @@
1818
<script type="text/babel">
1919
'use strict';
2020

21-
function Embeddable({onComponentsLoad, ...props}) {
21+
function Embeddable({token, onComponentsLoad, variables, onVariablesChange}) {
2222
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+
}
2432
React.useEffect(() => {
33+
ref.current.addEventListener('variablesChange', handleVariableChange);
2534
ref.current.addEventListener('componentsLoad', onComponentsLoad);
2635
return () => {
2736
ref.current.removeEventListener('componentsLoad', onComponentsLoad);
37+
ref.current.removeEventListener('variablesChange', handleVariableChange);
2838
};
2939
}, []);
30-
return React.createElement('em-beddable', {...props, ref});
40+
return React.createElement('em-beddable', {
41+
ref,
42+
token,
43+
variables: JSON.stringify(variables)
44+
});
3145
}
3246

3347
const TestComponent = () => {
3448
const [load, setLoad] = React.useState(false);
3549
const [isLoading, setIsLoading] = React.useState(false);
50+
const DEFAULT = { 'Text Value': 'Germany' };
51+
const [variables, setVariables] = React.useState(DEFAULT)
3652

3753
if (load) {
3854
return (
3955
<div>
4056
{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>
4569
</div>
4670
);
4771
}

0 commit comments

Comments
 (0)