-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
index.tsx
67 lines (58 loc) · 2.08 KB
/
index.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
import React, {useCallback, useEffect, useState} from 'react';
import {ActivityIndicator, View} from 'react-native';
import type {PlaidLinkOnSuccessMetadata} from 'react-plaid-link';
import {usePlaidLink} from 'react-plaid-link';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import Log from '@libs/Log';
import type PlaidLinkProps from './types';
function PlaidLink({token, onSuccess = () => {}, onError = () => {}, onExit = () => {}, onEvent, receivedRedirectURI}: PlaidLinkProps) {
const [isPlaidLoaded, setIsPlaidLoaded] = useState(false);
const theme = useTheme();
const styles = useThemeStyles();
const successCallback = useCallback(
(publicToken: string, metadata: PlaidLinkOnSuccessMetadata) => {
onSuccess({publicToken, metadata});
},
[onSuccess],
);
const {open, ready, error} = usePlaidLink({
token,
onSuccess: successCallback,
onExit: (exitError, metadata) => {
Log.info('[PlaidLink] Exit: ', false, {exitError, metadata});
onExit();
},
onEvent: (event, metadata) => {
Log.info('[PlaidLink] Event: ', false, {event, metadata});
onEvent(event, metadata);
},
onLoad: () => setIsPlaidLoaded(true),
// The redirect URI with an OAuth state ID. Needed to re-initialize the PlaidLink after directing the
// user to their respective bank platform
receivedRedirectUri: receivedRedirectURI,
});
useEffect(() => {
if (error) {
onError(error);
return;
}
if (!ready) {
return;
}
if (!isPlaidLoaded) {
return;
}
open();
}, [ready, error, isPlaidLoaded, open, onError]);
return (
<View style={[styles.flex1, styles.alignItemsCenter, styles.justifyContentCenter]}>
<ActivityIndicator
color={theme.spinner}
size="large"
/>
</View>
);
}
PlaidLink.displayName = 'PlaidLink';
export default PlaidLink;