Skip to content

Commit 935608b

Browse files
authored
Merge pull request #30915 from JKobrynski/migratePlaidLinkToTypeScript
[TS Migration] Migrate PlaidLink directory to TypeScript
2 parents 300645e + 938887f commit 935608b

File tree

7 files changed

+60
-77
lines changed

7 files changed

+60
-77
lines changed

src/components/AddPlaidBankAccount.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ function AddPlaidBankAccount({
209209
// Handle Plaid login errors (will potentially reset plaid token and item depending on the error)
210210
if (event === 'ERROR') {
211211
Log.hmmm('[PlaidLink] Error: ', metadata);
212-
if (bankAccountID && metadata.error_code) {
212+
if (bankAccountID && metadata && metadata.error_code) {
213213
BankAccounts.handlePlaidError(bankAccountID, metadata.error_code, metadata.error_message, metadata.request_id);
214214
}
215215
}

src/components/PlaidLink/index.native.js

-42
This file was deleted.
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {useEffect} from 'react';
2+
import {dismissLink, LinkEvent, openLink, useDeepLinkRedirector, usePlaidEmitter} from 'react-native-plaid-link-sdk';
3+
import Log from '@libs/Log';
4+
import CONST from '@src/CONST';
5+
import PlaidLinkProps from './types';
6+
7+
function PlaidLink({token, onSuccess = () => {}, onExit = () => {}, onEvent}: PlaidLinkProps) {
8+
useDeepLinkRedirector();
9+
usePlaidEmitter((event: LinkEvent) => {
10+
Log.info('[PlaidLink] Handled Plaid Event: ', false, {...event});
11+
onEvent(event.eventName, event.metadata);
12+
});
13+
useEffect(() => {
14+
onEvent(CONST.BANK_ACCOUNT.PLAID.EVENTS_NAME.OPEN);
15+
openLink({
16+
tokenConfig: {
17+
token,
18+
noLoadingState: false,
19+
},
20+
onSuccess: ({publicToken, metadata}) => {
21+
onSuccess({publicToken, metadata});
22+
},
23+
onExit: ({error, metadata}) => {
24+
Log.info('[PlaidLink] Exit: ', false, {error, metadata});
25+
onExit();
26+
},
27+
});
28+
29+
return () => {
30+
dismissLink();
31+
};
32+
33+
// We generally do not need to include the token as a dependency here as it is only provided once via props and should not change
34+
// eslint-disable-next-line react-hooks/exhaustive-deps
35+
}, []);
36+
return null;
37+
}
38+
39+
PlaidLink.displayName = 'PlaidLink';
40+
41+
export default PlaidLink;

src/components/PlaidLink/index.js src/components/PlaidLink/index.tsx

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
11
import {useCallback, useEffect, useState} from 'react';
2-
import {usePlaidLink} from 'react-plaid-link';
2+
import {PlaidLinkOnSuccessMetadata, usePlaidLink} from 'react-plaid-link';
33
import Log from '@libs/Log';
4-
import {plaidLinkDefaultProps, plaidLinkPropTypes} from './plaidLinkPropTypes';
4+
import PlaidLinkProps from './types';
55

6-
function PlaidLink(props) {
6+
function PlaidLink({token, onSuccess = () => {}, onError = () => {}, onExit = () => {}, onEvent, receivedRedirectURI}: PlaidLinkProps) {
77
const [isPlaidLoaded, setIsPlaidLoaded] = useState(false);
8-
const onSuccess = props.onSuccess;
9-
const onError = props.onError;
108
const successCallback = useCallback(
11-
(publicToken, metadata) => {
9+
(publicToken: string, metadata: PlaidLinkOnSuccessMetadata) => {
1210
onSuccess({publicToken, metadata});
1311
},
1412
[onSuccess],
1513
);
1614

1715
const {open, ready, error} = usePlaidLink({
18-
token: props.token,
16+
token,
1917
onSuccess: successCallback,
2018
onExit: (exitError, metadata) => {
2119
Log.info('[PlaidLink] Exit: ', false, {exitError, metadata});
22-
props.onExit();
20+
onExit();
2321
},
2422
onEvent: (event, metadata) => {
2523
Log.info('[PlaidLink] Event: ', false, {event, metadata});
26-
props.onEvent(event, metadata);
24+
onEvent(event, metadata);
2725
},
2826
onLoad: () => setIsPlaidLoaded(true),
2927

3028
// The redirect URI with an OAuth state ID. Needed to re-initialize the PlaidLink after directing the
3129
// user to their respective bank platform
32-
receivedRedirectUri: props.receivedRedirectURI,
30+
receivedRedirectUri: receivedRedirectURI,
3331
});
3432

3533
useEffect(() => {
@@ -52,7 +50,5 @@ function PlaidLink(props) {
5250
return null;
5351
}
5452

55-
PlaidLink.propTypes = plaidLinkPropTypes;
56-
PlaidLink.defaultProps = plaidLinkDefaultProps;
5753
PlaidLink.displayName = 'PlaidLink';
5854
export default PlaidLink;

src/components/PlaidLink/nativeModule/index.android.js

-3
This file was deleted.

src/components/PlaidLink/nativeModule/index.ios.js

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
import PropTypes from 'prop-types';
1+
import {LinkEventMetadata, LinkSuccessMetadata} from 'react-native-plaid-link-sdk';
2+
import {PlaidLinkOnEventMetadata, PlaidLinkOnSuccessMetadata} from 'react-plaid-link';
23

3-
const plaidLinkPropTypes = {
4+
type PlaidLinkProps = {
45
// Plaid Link SDK public token used to initialize the Plaid SDK
5-
token: PropTypes.string.isRequired,
6+
token: string;
67

78
// Callback to execute once the user taps continue after successfully entering their account information
8-
onSuccess: PropTypes.func,
9+
onSuccess?: (args: {publicToken: string; metadata: PlaidLinkOnSuccessMetadata | LinkSuccessMetadata}) => void;
910

1011
// Callback to execute when there is an error event emitted by the Plaid SDK
11-
onError: PropTypes.func,
12+
onError?: (error: ErrorEvent | null) => void;
1213

1314
// Callback to execute when the user leaves the Plaid widget flow without entering any information
14-
onExit: PropTypes.func,
15+
onExit?: () => void;
1516

1617
// Callback to execute whenever a Plaid event occurs
17-
onEvent: PropTypes.func,
18+
onEvent: (eventName: string, metadata?: PlaidLinkOnEventMetadata | LinkEventMetadata) => void;
1819

1920
// The redirect URI with an OAuth state ID. Needed to re-initialize the PlaidLink after directing the
2021
// user to their respective bank platform
21-
receivedRedirectURI: PropTypes.string,
22+
receivedRedirectURI?: string;
2223
};
2324

24-
const plaidLinkDefaultProps = {
25-
onSuccess: () => {},
26-
onError: () => {},
27-
onExit: () => {},
28-
receivedRedirectURI: null,
29-
};
30-
31-
export {plaidLinkPropTypes, plaidLinkDefaultProps};
25+
export default PlaidLinkProps;

0 commit comments

Comments
 (0)