-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TransferBalancePage.tsx
244 lines (220 loc) · 11.8 KB
/
TransferBalancePage.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, {useEffect} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import ConfirmationPage from '@components/ConfirmationPage';
import CurrentWalletBalance from '@components/CurrentWalletBalance';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as PaymentUtils from '@libs/PaymentUtils';
import variables from '@styles/variables';
import * as PaymentMethods from '@userActions/PaymentMethods';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {BankAccountList, FundList, UserWallet, WalletTransfer} from '@src/types/onyx';
import type PaymentMethod from '@src/types/onyx/PaymentMethod';
import type {FilterMethodPaymentType} from '@src/types/onyx/WalletTransfer';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type TransferBalancePageOnyxProps = {
/** User's wallet information */
userWallet: OnyxEntry<UserWallet>;
/** List of bank accounts */
bankAccountList: OnyxEntry<BankAccountList>;
/** List of user's card objects */
fundList: OnyxEntry<FundList>;
/** Wallet balance transfer props */
walletTransfer: OnyxEntry<WalletTransfer>;
};
type TransferBalancePageProps = TransferBalancePageOnyxProps;
const TRANSFER_TIER_NAMES: string[] = [CONST.WALLET.TIER_NAME.GOLD, CONST.WALLET.TIER_NAME.PLATINUM];
function TransferBalancePage({bankAccountList, fundList, userWallet, walletTransfer}: TransferBalancePageProps) {
const styles = useThemeStyles();
const {numberFormat, translate} = useLocalize();
const {isOffline} = useNetwork();
const paymentCardList = fundList ?? {};
const paymentTypes = [
{
key: CONST.WALLET.TRANSFER_METHOD_TYPE.INSTANT,
title: translate('transferAmountPage.instant'),
description: translate('transferAmountPage.instantSummary', {
rate: numberFormat(CONST.WALLET.TRANSFER_METHOD_TYPE_FEE.INSTANT.RATE),
minAmount: CurrencyUtils.convertToDisplayString(CONST.WALLET.TRANSFER_METHOD_TYPE_FEE.INSTANT.MINIMUM_FEE),
}),
icon: Expensicons.Bolt,
type: CONST.PAYMENT_METHODS.DEBIT_CARD,
},
{
key: CONST.WALLET.TRANSFER_METHOD_TYPE.ACH,
title: translate('transferAmountPage.ach'),
description: translate('transferAmountPage.achSummary'),
icon: Expensicons.Bank,
type: CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT,
},
];
/**
* Get the selected/default payment method account for wallet transfer
*/
function getSelectedPaymentMethodAccount(): PaymentMethod | undefined {
const paymentMethods = PaymentUtils.formatPaymentMethods(bankAccountList ?? {}, paymentCardList, styles);
const defaultAccount = paymentMethods.find((method) => method.isDefault);
const selectedAccount = paymentMethods.find(
(method) => method.accountType === walletTransfer?.selectedAccountType && method.methodID?.toString() === walletTransfer?.selectedAccountID?.toString(),
);
return selectedAccount ?? defaultAccount;
}
function navigateToChooseTransferAccount(filterPaymentMethodType: FilterMethodPaymentType) {
PaymentMethods.saveWalletTransferMethodType(filterPaymentMethodType);
// If we only have a single option for the given paymentMethodType do not force the user to make a selection
const combinedPaymentMethods = PaymentUtils.formatPaymentMethods(bankAccountList ?? {}, paymentCardList, styles);
const filteredMethods = combinedPaymentMethods.filter((paymentMethod) => paymentMethod.accountType === filterPaymentMethodType);
if (filteredMethods.length === 1) {
const account = filteredMethods[0];
PaymentMethods.saveWalletTransferAccountTypeAndID(filterPaymentMethodType ?? '', account?.methodID?.toString() ?? '');
return;
}
Navigation.navigate(ROUTES.SETTINGS_WALLET_CHOOSE_TRANSFER_ACCOUNT);
}
useEffect(() => {
// Reset to the default account when the page is opened
PaymentMethods.resetWalletTransferData();
const selectedAccount = getSelectedPaymentMethodAccount();
if (!selectedAccount) {
return;
}
PaymentMethods.saveWalletTransferAccountTypeAndID(selectedAccount?.accountType ?? '', selectedAccount?.methodID?.toString() ?? '');
// eslint-disable-next-line react-hooks/exhaustive-deps -- we only want this effect to run on initial render
}, []);
if (walletTransfer?.shouldShowSuccess && !walletTransfer?.loading) {
return (
<ScreenWrapper testID={TransferBalancePage.displayName}>
<HeaderWithBackButton
title={translate('common.transferBalance')}
onBackButtonPress={PaymentMethods.dismissSuccessfulTransferBalancePage}
/>
<ConfirmationPage
heading={translate('transferAmountPage.transferSuccess')}
description={
walletTransfer.paymentMethodType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT
? translate('transferAmountPage.transferDetailBankAccount')
: translate('transferAmountPage.transferDetailDebitCard')
}
shouldShowButton
buttonText={translate('common.done')}
onButtonPress={PaymentMethods.dismissSuccessfulTransferBalancePage}
/>
</ScreenWrapper>
);
}
const selectedAccount = getSelectedPaymentMethodAccount();
const selectedPaymentType =
selectedAccount && selectedAccount.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT ? CONST.WALLET.TRANSFER_METHOD_TYPE.ACH : CONST.WALLET.TRANSFER_METHOD_TYPE.INSTANT;
const calculatedFee = PaymentUtils.calculateWalletTransferBalanceFee(userWallet?.currentBalance ?? 0, selectedPaymentType);
const transferAmount = userWallet?.currentBalance ?? 0 - calculatedFee;
const isTransferable = transferAmount > 0;
const isButtonDisabled = !isTransferable || !selectedAccount;
const errorMessage = ErrorUtils.getLatestErrorMessage(walletTransfer);
const shouldShowTransferView = PaymentUtils.hasExpensifyPaymentMethod(paymentCardList, bankAccountList ?? {}) && TRANSFER_TIER_NAMES.includes(userWallet?.tierName ?? '');
return (
<ScreenWrapper testID={TransferBalancePage.displayName}>
<FullPageNotFoundView
shouldShow={!shouldShowTransferView}
titleKey="notFound.pageNotFound"
subtitleKey="transferAmountPage.notHereSubTitle"
linkKey="transferAmountPage.goToWallet"
onLinkPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)}
>
<HeaderWithBackButton
title={translate('common.transferBalance')}
shouldShowBackButton
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET)}
/>
<View style={[styles.flexGrow1, styles.flexShrink1, styles.flexBasisAuto, styles.justifyContentCenter]}>
<CurrentWalletBalance balanceStyles={[styles.transferBalanceBalance]} />
</View>
<ScrollView
style={styles.flexGrow0}
contentContainerStyle={styles.pv5}
>
<View style={styles.ph5}>
{paymentTypes.map((paymentType) => (
<MenuItem
key={paymentType.key}
title={paymentType.title}
description={paymentType.description}
iconWidth={variables.iconSizeXLarge}
iconHeight={variables.iconSizeXLarge}
icon={paymentType.icon}
success={selectedPaymentType === paymentType.key}
wrapperStyle={{
...styles.mt3,
...styles.pv4,
...styles.transferBalancePayment,
...(selectedPaymentType === paymentType.key && styles.transferBalanceSelectedPayment),
}}
onPress={() => navigateToChooseTransferAccount(paymentType.type)}
/>
))}
</View>
<Text style={[styles.p5, styles.textLabelSupporting, styles.justifyContentStart]}>{translate('transferAmountPage.whichAccount')}</Text>
{Boolean(selectedAccount) && (
<MenuItem
title={selectedAccount?.title}
description={selectedAccount?.description}
shouldShowRightIcon
iconStyles={selectedAccount?.iconStyles}
iconWidth={selectedAccount?.iconSize}
iconHeight={selectedAccount?.iconSize}
icon={selectedAccount?.icon}
onPress={() => navigateToChooseTransferAccount(selectedAccount?.accountType ?? CONST.PAYMENT_METHODS.DEBIT_CARD)}
displayInDefaultIconColor
/>
)}
<View style={styles.ph5}>
<Text style={[styles.mt5, styles.mb3, styles.textLabelSupporting, styles.justifyContentStart]}>{translate('transferAmountPage.fee')}</Text>
<Text style={[styles.justifyContentStart]}>{CurrencyUtils.convertToDisplayString(calculatedFee)}</Text>
</View>
</ScrollView>
<View>
<FormAlertWithSubmitButton
buttonText={translate('transferAmountPage.transfer', {
amount: isTransferable ? CurrencyUtils.convertToDisplayString(transferAmount) : '',
})}
isLoading={walletTransfer?.loading}
onSubmit={() => selectedAccount && PaymentMethods.transferWalletBalance(selectedAccount)}
isDisabled={isButtonDisabled || isOffline}
message={errorMessage}
isAlertVisible={!isEmptyObject(errorMessage)}
/>
</View>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
TransferBalancePage.displayName = 'TransferBalancePage';
export default withOnyx<TransferBalancePageProps, TransferBalancePageOnyxProps>({
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
walletTransfer: {
key: ONYXKEYS.WALLET_TRANSFER,
},
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
fundList: {
key: ONYXKEYS.FUND_LIST,
},
})(TransferBalancePage);