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
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import {useEffect} from 'react';
import {dismissLink, openLink, useDeepLinkRedirector, usePlaidEmitter} from 'react-native-plaid-link-sdk';
import {dismissLink, LinkEvent, openLink, useDeepLinkRedirector, usePlaidEmitter} from 'react-native-plaid-link-sdk';
import Log from '@libs/Log';
import CONST from '@src/CONST';
import {plaidLinkDefaultProps, plaidLinkPropTypes} from './plaidLinkPropTypes';
import PlaidLinkProps from './types';

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

Choose a reason for hiding this comment

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

Are you sure there should be event.eventName here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you try to pass the whole event object, it produces the following error:

Argument of type 'LinkEvent' is not assignable to parameter of type 'Parameters | undefined'.
  Type 'LinkEvent' is not assignable to type 'Record<string, unknown>'.

Do you think we should keep it as it is or maybe JSON.stringify it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Related to microsoft/TypeScript#15300. There is no correct way to fix. We can however apply a workaround:

  1. Pass {...event} to Log.info
  2. Or change event type (convert interface to type) event: Pick<LinkEvent, keyof LinkEvent>

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)

onEvent(event.eventName, event.metadata);
});
useEffect(() => {
props.onEvent(CONST.BANK_ACCOUNT.PLAID.EVENTS_NAME.OPEN, {});
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: props.token,
token,
noLoadingState: false,
JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
},
onSuccess: ({publicToken, metadata}) => {
props.onSuccess({publicToken, metadata});
onSuccess({publicToken, metadata});
},
onExit: (exitError, metadata) => {
Log.info('[PlaidLink] Exit: ', false, {exitError, metadata});
props.onExit();
onExit: ({error, metadata}) => {
Log.info('[PlaidLink] Exit: ', false, {error, metadata});
onExit();
},
});

Expand All @@ -35,8 +36,6 @@ function PlaidLink(props) {
return null;
}

PlaidLink.propTypes = plaidLinkPropTypes;
PlaidLink.defaultProps = plaidLinkDefaultProps;
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;
JKobrynski marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.
31 changes: 0 additions & 31 deletions src/components/PlaidLink/plaidLinkPropTypes.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/components/PlaidLink/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {LinkEventMetadata, LinkSuccessMetadata} from 'react-native-plaid-link-sdk';
import {PlaidLinkOnEventMetadata, PlaidLinkOnSuccessMetadata, PlaidLinkStableEvent} from 'react-plaid-link';

type PlaidLinkProps = {
// Plaid Link SDK public token used to initialize the Plaid SDK
token: string;

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

Choose a reason for hiding this comment

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

Suggested change
onSuccess?: (args: {publicToken?: string; metadata: PlaidLinkOnSuccessMetadata | LinkSuccessMetadata}) => void;
onSuccess?: (args: {publicToken: string; metadata: PlaidLinkOnSuccessMetadata | LinkSuccessMetadata}) => void;

publicToken is not optional

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!


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

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

// Callback to execute whenever a Plaid event occurs
onEvent: (eventName: PlaidLinkStableEvent | string, metadata?: PlaidLinkOnEventMetadata | LinkEventMetadata) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
onEvent: (eventName: PlaidLinkStableEvent | string, metadata?: PlaidLinkOnEventMetadata | LinkEventMetadata) => void;
onEvent: (eventName: string, metadata?: PlaidLinkOnEventMetadata | LinkEventMetadata) => void;

PlaidLinkStableEvent is a string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated


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

export default PlaidLinkProps;
Loading