forked from plaid/react-native-plaid-link-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaidLink.tsx
118 lines (107 loc) · 3.12 KB
/
PlaidLink.tsx
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React, { useEffect } from 'react';
import {
Linking,
NativeEventEmitter,
NativeModules,
Platform,
TouchableOpacity,
} from 'react-native';
import {
LinkError,
LinkEventListener,
LinkExit,
LinkSuccess,
PlaidLinkComponentProps,
PlaidLinkProps,
} from './Types';
/**
* A hook that registers a listener on the Plaid emitter for the 'onEvent' type.
* The listener is cleaned up when this view is unmounted
*
* @param LinkEventListener the listener to call
*/
export const usePlaidEmitter = (LinkEventListener: LinkEventListener) => {
useEffect(() => {
const emitter = new NativeEventEmitter(
Platform.OS === 'ios'
? NativeModules.RNLinksdk
: NativeModules.PlaidAndroid,
);
const listener = emitter.addListener('onEvent', LinkEventListener);
// Clean up after this effect:
return function cleanup() {
listener.remove();
};
}, []);
};
export const openLink = async (props: PlaidLinkProps) => {
if (props.tokenConfig == null) {
console.log('The public_key is being deprecated. Learn how to upgrade to link_tokens at https://plaid.com/docs/link-token-migration-guide/')
}
let config = props.tokenConfig ? props.tokenConfig : props.publicKeyConfig!;
if (Platform.OS === 'android') {
NativeModules.PlaidAndroid.startLinkActivityForResult(
JSON.stringify(config),
(result: LinkSuccess) => {
if (props.onSuccess != null) {
props.onSuccess(result);
}
},
(result: LinkExit) => {
if (props.onExit != null) {
if (result.error != null && result.error.displayMessage != null) {
//TODO(RNSDK-118): Remove errorDisplayMessage field in next major update.
result.error.errorDisplayMessage = result.error.displayMessage
}
props.onExit(result);
}
},
);
} else {
NativeModules.RNLinksdk.create(config);
NativeModules.RNLinksdk.open(
(result: LinkSuccess) => {
if (props.onSuccess != null) {
props.onSuccess(result);
}
},
(error: LinkError, result: LinkExit) => {
if (props.onExit != null) {
if (error) {
var data = result || {};
data.error = error;
props.onExit(data);
} else {
props.onExit(result);
}
}
}
);
}
};
export const dismissLink = () => {
if (Platform.OS === 'ios') {
NativeModules.RNLinksdk.dismiss();
}
};
export const useDeepLinkRedirector = () => {
const _handleListenerChange = (event: { url: string }) => {
if (event.url !== null && Platform.OS === 'ios') {
NativeModules.RNLinksdk.continueFromRedirectUriString(event.url);
}
};
useEffect(() => {
Linking.addEventListener('url', _handleListenerChange);
return function cleanup() {
Linking.removeEventListener('url', _handleListenerChange);
};
}, []);
};
export const PlaidLink = (props: PlaidLinkComponentProps) => {
function onPress() {
props.onPress?.()
openLink(props)
}
useDeepLinkRedirector();
return <TouchableOpacity onPress={onPress}>{props.children}</TouchableOpacity>;
};