Skip to content

Commit b7c0055

Browse files
authored
Merge pull request Expensify#49339 from allgandalf/issue-48974
[Internal QA]: Refactor ChooseTransferAccountPage to use Selection List
2 parents cd3f9d2 + 1260999 commit b7c0055

File tree

1 file changed

+72
-38
lines changed

1 file changed

+72
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
import React from 'react';
2-
import type {GestureResponderEvent} from 'react-native';
1+
import React, {useMemo} from 'react';
32
import {View} from 'react-native';
4-
import type {OnyxEntry} from 'react-native-onyx';
5-
import {withOnyx} from 'react-native-onyx';
3+
import {useOnyx} from 'react-native-onyx';
4+
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
65
import HeaderWithBackButton from '@components/HeaderWithBackButton';
6+
import Icon from '@components/Icon';
7+
import getBankIcon from '@components/Icon/BankIcons';
78
import * as Expensicons from '@components/Icon/Expensicons';
89
import MenuItem from '@components/MenuItem';
910
import ScreenWrapper from '@components/ScreenWrapper';
11+
import ScrollView from '@components/ScrollView';
12+
import SelectionList from '@components/SelectionList';
13+
import RadioListItem from '@components/SelectionList/RadioListItem';
1014
import useLocalize from '@hooks/useLocalize';
1115
import useThemeStyles from '@hooks/useThemeStyles';
16+
import {getLastFourDigits} from '@libs/BankAccountUtils';
1217
import Navigation from '@libs/Navigation/Navigation';
1318
import * as BankAccounts from '@userActions/BankAccounts';
1419
import * as PaymentMethods from '@userActions/PaymentMethods';
1520
import CONST from '@src/CONST';
1621
import ONYXKEYS from '@src/ONYXKEYS';
1722
import ROUTES from '@src/ROUTES';
18-
import type {AccountData, WalletTransfer} from '@src/types/onyx';
19-
import PaymentMethodList from './PaymentMethodList';
23+
import type {AccountData} from '@src/types/onyx';
24+
import type {BankName} from '@src/types/onyx/Bank';
25+
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
2026

21-
type ChooseTransferAccountPageOnyxProps = {
22-
/** Wallet transfer propTypes */
23-
walletTransfer: OnyxEntry<WalletTransfer>;
24-
};
27+
function ChooseTransferAccountPage() {
28+
const [walletTransfer, walletTransferResult] = useOnyx(ONYXKEYS.WALLET_TRANSFER);
2529

26-
type ChooseTransferAccountPageProps = ChooseTransferAccountPageOnyxProps;
27-
28-
function ChooseTransferAccountPage({walletTransfer = {}}: ChooseTransferAccountPageProps) {
2930
const styles = useThemeStyles();
3031
const {translate} = useLocalize();
3132
/**
@@ -34,7 +35,7 @@ function ChooseTransferAccountPage({walletTransfer = {}}: ChooseTransferAccountP
3435
* @param accountType of the selected account type
3536
* @param account of the selected account data
3637
*/
37-
const selectAccountAndNavigateBack = (event?: GestureResponderEvent | KeyboardEvent, accountType?: string, account?: AccountData) => {
38+
const selectAccountAndNavigateBack = (accountType?: string, account?: AccountData) => {
3839
PaymentMethods.saveWalletTransferAccountTypeAndID(
3940
accountType ?? '',
4041
(accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT ? account?.bankAccountID?.toString() : account?.fundID?.toString()) ?? '',
@@ -50,40 +51,73 @@ function ChooseTransferAccountPage({walletTransfer = {}}: ChooseTransferAccountP
5051
BankAccounts.openPersonalBankAccountSetupView();
5152
};
5253

54+
const [bankAccountsList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
55+
const selectedAccountID = walletTransfer?.selectedAccountID;
56+
const data = useMemo(() => {
57+
const options = Object.values(bankAccountsList ?? {}).map((bankAccount) => {
58+
const bankName = (bankAccount.accountData?.additionalData?.bankName ?? '') as BankName;
59+
const bankAccountNumber = bankAccount.accountData?.accountNumber ?? '';
60+
const bankAccountID = bankAccount.accountData?.bankAccountID ?? bankAccount.methodID;
61+
const {icon, iconSize, iconStyles} = getBankIcon({bankName, styles});
62+
return {
63+
value: bankAccountID,
64+
text: bankAccount.title,
65+
leftElement: icon ? (
66+
<View style={[styles.flexRow, styles.alignItemsCenter, styles.mr3]}>
67+
<Icon
68+
src={icon}
69+
width={iconSize}
70+
height={iconSize}
71+
additionalStyles={iconStyles}
72+
/>
73+
</View>
74+
) : null,
75+
alternateText: `${translate('workspace.expensifyCard.accountEndingIn')} ${getLastFourDigits(bankAccountNumber)}`,
76+
keyForList: bankAccountID?.toString(),
77+
isSelected: bankAccountID?.toString() === selectedAccountID,
78+
bankAccount,
79+
};
80+
});
81+
return options;
82+
}, [bankAccountsList, selectedAccountID, styles, translate]);
83+
84+
if (isLoadingOnyxValue(walletTransferResult)) {
85+
return <FullscreenLoadingIndicator />;
86+
}
87+
5388
return (
5489
<ScreenWrapper testID={ChooseTransferAccountPage.displayName}>
5590
<HeaderWithBackButton
5691
title={translate('chooseTransferAccountPage.chooseAccount')}
5792
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WALLET_TRANSFER_BALANCE)}
5893
/>
59-
<View style={[styles.mt3, styles.flexShrink1, styles.flexBasisAuto]}>
60-
<PaymentMethodList
61-
onPress={selectAccountAndNavigateBack}
62-
shouldShowSelectedState
63-
filterType={walletTransfer?.filterPaymentMethodType}
64-
selectedMethodID={walletTransfer?.selectedAccountID}
65-
shouldShowAddPaymentMethodButton={false}
66-
shouldShowAddBankAccount={false}
67-
shouldShowRightIcon={false}
94+
<ScrollView>
95+
<SelectionList
96+
sections={[{data}]}
97+
ListItem={RadioListItem}
98+
onSelectRow={(value) => {
99+
const accountType = value?.bankAccount?.accountType;
100+
const accountData = value?.bankAccount?.accountData;
101+
selectAccountAndNavigateBack(accountType, accountData);
102+
}}
103+
shouldSingleExecuteRowSelect
104+
shouldUpdateFocusedIndex
105+
initiallyFocusedOptionKey={walletTransfer?.selectedAccountID?.toString()}
68106
/>
69-
</View>
70-
<MenuItem
71-
onPress={navigateToAddPaymentMethodPage}
72-
title={
73-
walletTransfer?.filterPaymentMethodType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT
74-
? translate('paymentMethodList.addNewBankAccount')
75-
: translate('paymentMethodList.addNewDebitCard')
76-
}
77-
icon={Expensicons.Plus}
78-
/>
107+
<MenuItem
108+
onPress={navigateToAddPaymentMethodPage}
109+
title={
110+
walletTransfer?.filterPaymentMethodType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT
111+
? translate('paymentMethodList.addNewBankAccount')
112+
: translate('paymentMethodList.addNewDebitCard')
113+
}
114+
icon={Expensicons.Plus}
115+
/>
116+
</ScrollView>
79117
</ScreenWrapper>
80118
);
81119
}
82120

83121
ChooseTransferAccountPage.displayName = 'ChooseTransferAccountPage';
84122

85-
export default withOnyx<ChooseTransferAccountPageProps, ChooseTransferAccountPageOnyxProps>({
86-
walletTransfer: {
87-
key: ONYXKEYS.WALLET_TRANSFER,
88-
},
89-
})(ChooseTransferAccountPage);
123+
export default ChooseTransferAccountPage;

0 commit comments

Comments
 (0)