1
- import React from 'react' ;
2
- import type { GestureResponderEvent } from 'react-native' ;
1
+ import React , { useMemo } from 'react' ;
3
2
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 ' ;
6
5
import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
6
+ import Icon from '@components/Icon' ;
7
+ import getBankIcon from '@components/Icon/BankIcons' ;
7
8
import * as Expensicons from '@components/Icon/Expensicons' ;
8
9
import MenuItem from '@components/MenuItem' ;
9
10
import ScreenWrapper from '@components/ScreenWrapper' ;
11
+ import ScrollView from '@components/ScrollView' ;
12
+ import SelectionList from '@components/SelectionList' ;
13
+ import RadioListItem from '@components/SelectionList/RadioListItem' ;
10
14
import useLocalize from '@hooks/useLocalize' ;
11
15
import useThemeStyles from '@hooks/useThemeStyles' ;
16
+ import { getLastFourDigits } from '@libs/BankAccountUtils' ;
12
17
import Navigation from '@libs/Navigation/Navigation' ;
13
18
import * as BankAccounts from '@userActions/BankAccounts' ;
14
19
import * as PaymentMethods from '@userActions/PaymentMethods' ;
15
20
import CONST from '@src/CONST' ;
16
21
import ONYXKEYS from '@src/ONYXKEYS' ;
17
22
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' ;
20
26
21
- type ChooseTransferAccountPageOnyxProps = {
22
- /** Wallet transfer propTypes */
23
- walletTransfer : OnyxEntry < WalletTransfer > ;
24
- } ;
27
+ function ChooseTransferAccountPage ( ) {
28
+ const [ walletTransfer , walletTransferResult ] = useOnyx ( ONYXKEYS . WALLET_TRANSFER ) ;
25
29
26
- type ChooseTransferAccountPageProps = ChooseTransferAccountPageOnyxProps ;
27
-
28
- function ChooseTransferAccountPage ( { walletTransfer = { } } : ChooseTransferAccountPageProps ) {
29
30
const styles = useThemeStyles ( ) ;
30
31
const { translate} = useLocalize ( ) ;
31
32
/**
@@ -34,7 +35,7 @@ function ChooseTransferAccountPage({walletTransfer = {}}: ChooseTransferAccountP
34
35
* @param accountType of the selected account type
35
36
* @param account of the selected account data
36
37
*/
37
- const selectAccountAndNavigateBack = ( event ?: GestureResponderEvent | KeyboardEvent , accountType ?: string , account ?: AccountData ) => {
38
+ const selectAccountAndNavigateBack = ( accountType ?: string , account ?: AccountData ) => {
38
39
PaymentMethods . saveWalletTransferAccountTypeAndID (
39
40
accountType ?? '' ,
40
41
( accountType === CONST . PAYMENT_METHODS . PERSONAL_BANK_ACCOUNT ? account ?. bankAccountID ?. toString ( ) : account ?. fundID ?. toString ( ) ) ?? '' ,
@@ -50,40 +51,73 @@ function ChooseTransferAccountPage({walletTransfer = {}}: ChooseTransferAccountP
50
51
BankAccounts . openPersonalBankAccountSetupView ( ) ;
51
52
} ;
52
53
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
+
53
88
return (
54
89
< ScreenWrapper testID = { ChooseTransferAccountPage . displayName } >
55
90
< HeaderWithBackButton
56
91
title = { translate ( 'chooseTransferAccountPage.chooseAccount' ) }
57
92
onBackButtonPress = { ( ) => Navigation . goBack ( ROUTES . SETTINGS_WALLET_TRANSFER_BALANCE ) }
58
93
/>
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 ( ) }
68
106
/>
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 >
79
117
</ ScreenWrapper >
80
118
) ;
81
119
}
82
120
83
121
ChooseTransferAccountPage . displayName = 'ChooseTransferAccountPage' ;
84
122
85
- export default withOnyx < ChooseTransferAccountPageProps , ChooseTransferAccountPageOnyxProps > ( {
86
- walletTransfer : {
87
- key : ONYXKEYS . WALLET_TRANSFER ,
88
- } ,
89
- } ) ( ChooseTransferAccountPage ) ;
123
+ export default ChooseTransferAccountPage ;
0 commit comments