-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathIndex.jsx
101 lines (95 loc) · 3.14 KB
/
Index.jsx
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
100
101
import React, { useRef } from "react";
import { WebView } from "react-native-webview";
import queryString from "query-string";
import { LinkErrorCode, LinkErrorType, LinkExitMetadataStatus } from './const'
import Constants from 'expo-constants';
export default function PlaidLink({ linkToken, onEvent, onExit, onSuccess }) {
let webviewRef = useRef();
const handleNavigationStateChange = (event) => {
if (event.url.startsWith("plaidlink://")) {
const eventParams = queryString.parse(event.url.replace(/.*\?/, ""));
const linkSessionId = eventParams.link_session_id;
const mfaType = eventParams.mfa_type;
const requestId = eventParams.request_id;
const viewName = eventParams.view_name;
const errorCode = eventParams.error_code;
const errorMessage = eventParams.error_message;
const errorType = eventParams.error_type;
const exitStatus = eventParams.exist_status;
const institutionId = eventParams.institution_id;
const institutionName = eventParams.institution_name;
const institutionSearchQuery = eventParams.institution_search_query;
const timestamp = eventParams.timestamp;
if (!linkToken) {
console.warn("No link token provided.");
}
if (event.url.startsWith("plaidlink://event") && onEvent) {
onEvent({
eventName: eventParams.event_name,
metadata: {
linkSessionId,
mfaType,
requestId,
viewName,
errorCode,
errorMessage,
errorType,
exitStatus,
institutionId,
institutionName,
institutionSearchQuery,
timestamp,
},
});
} else if (event.url.startsWith("plaidlink://exit") && onExit) {
onExit({
error: {
errorCode: LinkErrorCode[errorCode],
errorMessage: eventParams.error_message,
errorType: LinkErrorType[errorType],
},
metadata: {
status: LinkExitMetadataStatus[exitStatus],
institution: {
id: institutionId,
name: institutionName,
},
linkSessionId,
requestId,
},
});
} else if (event.url.startsWith("plaidlink://connected") && onSuccess) {
const publicToken = eventParams.public_token;
const accounts = JSON.parse(eventParams.accounts);
onSuccess({
publicToken,
metadata: {
institution: {
id: institutionId,
name: institutionName,
},
accounts,
linkSessionId,
},
});
}
return false;
}
return true;
};
return (
<WebView
source={{
uri: `https://cdn.plaid.com/link/v2/stable/link.html?isWebview=true&token=${linkToken}`,
}}
style={{
flex: 1,
marginTop: Constants.statusBarHeight,
}}
ref={(ref) => (webviewRef = ref)}
onError={() => webviewRef.reload()}
originWhitelist={["https://*", "plaidlink://*"]}
onShouldStartLoadWithRequest={handleNavigationStateChange}
/>
);
}