Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS Migration] Migrate PlaidLink directory to TypeScript #30915

Merged
merged 11 commits into from
Nov 28, 2023
2 changes: 1 addition & 1 deletion src/components/AddPlaidBankAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function AddPlaidBankAccount({
// Handle Plaid login errors (will potentially reset plaid token and item depending on the error)
if (event === 'ERROR') {
Log.hmmm('[PlaidLink] Error: ', metadata);
if (bankAccountID && metadata.error_code) {
if (bankAccountID && metadata && metadata.error_code) {
BankAccounts.handlePlaidError(bankAccountID, metadata.error_code, metadata.error_message, metadata.request_id);
}
}
Expand Down
42 changes: 0 additions & 42 deletions src/components/PlaidLink/index.native.js

This file was deleted.

41 changes: 41 additions & 0 deletions src/components/PlaidLink/index.native.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {useEffect} from 'react';
import {dismissLink, LinkEvent, openLink, useDeepLinkRedirector, usePlaidEmitter} from 'react-native-plaid-link-sdk';
import Log from '@libs/Log';
import CONST from '@src/CONST';
import PlaidLinkProps from './types';

function PlaidLink({token, onSuccess = () => {}, onExit = () => {}, onEvent}: PlaidLinkProps) {
useDeepLinkRedirector();
usePlaidEmitter((event: LinkEvent) => {
Log.info('[PlaidLink] Handled Plaid Event: ', false, {...event});
onEvent(event.eventName, event.metadata);
});
useEffect(() => {
onEvent(CONST.BANK_ACCOUNT.PLAID.EVENTS_NAME.OPEN);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this argument you've removed was probably unnecessary, but please make sure it doesn't cause any regressions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may actually cause a crash. Please do one of the following:

  • Update AddPlaidBankAccount to take into account the possibility of metadata being undefined (cannot access metadata.error_code)
  • Revert this change and add a dummy object (+update types.ts to mark metadata as required)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (1st option)

openLink({
tokenConfig: {
token,
noLoadingState: false,
JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
},
onSuccess: ({publicToken, metadata}) => {
onSuccess({publicToken, metadata});
},
onExit: ({error, metadata}) => {
Log.info('[PlaidLink] Exit: ', false, {error, metadata});
onExit();
},
});

return () => {
dismissLink();
};

// 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
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return null;
}

PlaidLink.displayName = 'PlaidLink';

export default PlaidLink;
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import {useCallback, useEffect, useState} from 'react';
import {usePlaidLink} from 'react-plaid-link';
import {PlaidLinkOnSuccessMetadata, usePlaidLink} from 'react-plaid-link';
import Log from '@libs/Log';
import {plaidLinkDefaultProps, plaidLinkPropTypes} from './plaidLinkPropTypes';
import PlaidLinkProps from './types';

function PlaidLink(props) {
function PlaidLink({token, onSuccess = () => {}, onError = () => {}, onExit = () => {}, onEvent, receivedRedirectURI}: PlaidLinkProps) {
const [isPlaidLoaded, setIsPlaidLoaded] = useState(false);
const onSuccess = props.onSuccess;
const onError = props.onError;
const successCallback = useCallback(
(publicToken, metadata) => {
(publicToken: string, metadata: PlaidLinkOnSuccessMetadata) => {
onSuccess({publicToken, metadata});
},
[onSuccess],
);

const {open, ready, error} = usePlaidLink({
token: props.token,
token,
onSuccess: successCallback,
onExit: (exitError, metadata) => {
Log.info('[PlaidLink] Exit: ', false, {exitError, metadata});
props.onExit();
onExit();
},
onEvent: (event, metadata) => {
Log.info('[PlaidLink] Event: ', false, {event, metadata});
props.onEvent(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: props.receivedRedirectURI,
receivedRedirectUri: receivedRedirectURI,
});

useEffect(() => {
Expand All @@ -52,7 +50,5 @@ function PlaidLink(props) {
return null;
}

PlaidLink.propTypes = plaidLinkPropTypes;
PlaidLink.defaultProps = plaidLinkDefaultProps;
PlaidLink.displayName = 'PlaidLink';
export default PlaidLink;
3 changes: 0 additions & 3 deletions src/components/PlaidLink/nativeModule/index.android.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/PlaidLink/nativeModule/index.ios.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import PropTypes from 'prop-types';
import {LinkEventMetadata, LinkSuccessMetadata} from 'react-native-plaid-link-sdk';
import {PlaidLinkOnEventMetadata, PlaidLinkOnSuccessMetadata} from 'react-plaid-link';

const plaidLinkPropTypes = {
type PlaidLinkProps = {
// Plaid Link SDK public token used to initialize the Plaid SDK
token: PropTypes.string.isRequired,
token: string;

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

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

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

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

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

const plaidLinkDefaultProps = {
onSuccess: () => {},
onError: () => {},
onExit: () => {},
receivedRedirectURI: null,
};

export {plaidLinkPropTypes, plaidLinkDefaultProps};
export default PlaidLinkProps;
Loading