-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AddPersonalBankAccountPage.tsx
112 lines (101 loc) · 4.74 KB
/
AddPersonalBankAccountPage.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
import React, {useCallback, useEffect, useState} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import AddPlaidBankAccount from '@components/AddPlaidBankAccount';
import ConfirmationPage from '@components/ConfirmationPage';
import FormProvider from '@components/Form/FormProvider';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import getPlaidOAuthReceivedRedirectURI from '@libs/getPlaidOAuthReceivedRedirectURI';
import Navigation from '@libs/Navigation/Navigation';
import * as BankAccounts from '@userActions/BankAccounts';
import * as PaymentMethods from '@userActions/PaymentMethods';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {PersonalBankAccount, PlaidData} from '@src/types/onyx';
type AddPersonalBankAccountPageWithOnyxProps = {
/** Contains plaid data */
plaidData: OnyxEntry<PlaidData>;
/** The details about the Personal bank account we are adding saved in Onyx */
personalBankAccount: OnyxEntry<PersonalBankAccount>;
};
function AddPersonalBankAccountPage({personalBankAccount, plaidData}: AddPersonalBankAccountPageWithOnyxProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [selectedPlaidAccountId, setSelectedPlaidAccountId] = useState('');
const shouldShowSuccess = personalBankAccount?.shouldShowSuccess ?? false;
const validateBankAccountForm = () => ({});
const submitBankAccountForm = useCallback(() => {
const bankAccounts = plaidData?.bankAccounts ?? [];
const selectedPlaidBankAccount = bankAccounts.find((bankAccount) => bankAccount.plaidAccountID === selectedPlaidAccountId);
if (selectedPlaidBankAccount) {
BankAccounts.addPersonalBankAccount(selectedPlaidBankAccount);
}
}, [plaidData, selectedPlaidAccountId]);
const exitFlow = useCallback(
(shouldContinue = false) => {
const exitReportID = personalBankAccount?.exitReportID;
const onSuccessFallbackRoute = personalBankAccount?.onSuccessFallbackRoute ?? '';
if (exitReportID) {
Navigation.dismissModal(exitReportID);
} else if (shouldContinue && onSuccessFallbackRoute) {
PaymentMethods.continueSetup(onSuccessFallbackRoute);
} else {
Navigation.goBack(ROUTES.SETTINGS_WALLET);
}
},
[personalBankAccount],
);
useEffect(() => BankAccounts.clearPersonalBankAccount, []);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={shouldShowSuccess}
shouldEnablePickerAvoiding={false}
shouldShowOfflineIndicator={false}
testID={AddPersonalBankAccountPage.displayName}
>
<HeaderWithBackButton
title={translate('bankAccount.addBankAccount')}
onBackButtonPress={exitFlow}
/>
{shouldShowSuccess ? (
<ConfirmationPage
heading={translate('addPersonalBankAccountPage.successTitle')}
description={translate('addPersonalBankAccountPage.successMessage')}
shouldShowButton
buttonText={translate('common.continue')}
onButtonPress={() => exitFlow(true)}
/>
) : (
<FormProvider
formID={ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT}
isSubmitButtonVisible={Boolean(selectedPlaidAccountId)}
submitButtonText={translate('common.saveAndContinue')}
scrollContextEnabled
onSubmit={submitBankAccountForm}
validate={validateBankAccountForm}
style={[styles.mh5, styles.flex1]}
>
<AddPlaidBankAccount
onSelect={setSelectedPlaidAccountId}
plaidData={plaidData}
onExitPlaid={() => Navigation.goBack()}
receivedRedirectURI={getPlaidOAuthReceivedRedirectURI()}
selectedPlaidAccountID={selectedPlaidAccountId}
/>
</FormProvider>
)}
</ScreenWrapper>
);
}
AddPersonalBankAccountPage.displayName = 'AddPersonalBankAccountPage';
export default withOnyx<AddPersonalBankAccountPageWithOnyxProps, AddPersonalBankAccountPageWithOnyxProps>({
personalBankAccount: {
key: ONYXKEYS.PERSONAL_BANK_ACCOUNT,
},
plaidData: {
key: ONYXKEYS.PLAID_DATA,
},
})(AddPersonalBankAccountPage);