-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
99 lines (90 loc) · 2.53 KB
/
App.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Button, StyleSheet, Text, View } from 'react-native';
import { useState } from 'react';
import { WebView } from 'react-native-webview';
const html = `
<html>
<head>
<script src="https://js.flexpa.com/v1/"></script>
<meta name="viewport" content="width=device-width, user-scalable=no" />
</head>
<body>
<script>
FlexpaLink.create({
publishableKey: 'YOUR_PUBLISHABLE_KEY',
onSuccess: (publicToken) => {
// no-op
// use handleFlexpaLinkMessage to handle the success message instead
}
});
FlexpaLink.open();
</script>
</body>
</html>
`;
const injectedJavaScriptBeforeContentLoaded = `
const onMessage = (event) => {
window.ReactNativeWebView.postMessage(JSON.stringify(event.data));
}
window.addEventListener('message', onMessage);
true;
`;
export default function App() {
const [openLink, setOpenLink] = useState(false);
const [publicToken, setPublicToken] = useState(null);
const handleFlexpaLinkMessage = (message) => {
// if message is a string, reparse it as JSON - this is necessary because the success message is a string
if (typeof message === 'string') {
message = JSON.parse(message);
}
switch (message.type) {
case 'SUCCESS':
console.log('Flexpa Link success');
// send the public token to your server for exchange - equivalent to the onSuccess callback in the Flexpa Link docs
setPublicToken(message.payload);
break;
case 'ERROR':
console.log('Flexpa Link error');
break;
case 'LOADED':
console.log('Flexpa Link loaded');
break;
case 'CLOSED':
setOpenLink(false);
break;
default:
console.log(message);
break;
}
}
if (publicToken) {
return (
<View style={styles.container}>
<Text>Public Token: {publicToken}</Text>
</View>
);
}
return (
<>
{openLink ? (
<WebView
source={{ html }}
onMessage={(event) => { handleFlexpaLinkMessage(JSON.parse(event.nativeEvent.data)) }}
injectedJavaScriptBeforeContentLoaded={injectedJavaScriptBeforeContentLoaded}
setSupportMultipleWindows={false}
/>
) : (
<View style={styles.container}>
<Button title="Open Flexpa Link" onPress={() => setOpenLink(true)} />
</View>
)}
</>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});