-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathPaymentMethodList.js
178 lines (154 loc) · 5.61 KB
/
PaymentMethodList.js
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
import _ from 'underscore';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {FlatList} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import styles from '../../../styles/styles';
import * as StyleUtils from '../../../styles/StyleUtils';
import MenuItem from '../../../components/MenuItem';
import Text from '../../../components/Text';
import compose from '../../../libs/compose';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import ONYXKEYS from '../../../ONYXKEYS';
import CONST from '../../../CONST';
import * as Expensicons from '../../../components/Icon/Expensicons';
import bankAccountPropTypes from '../../../components/bankAccountPropTypes';
import * as PaymentUtils from '../../../libs/PaymentUtils';
const MENU_ITEM = 'menuItem';
const propTypes = {
/** What to do when a menu item is pressed */
onPress: PropTypes.func.isRequired,
/** Are we loading payments from the server? */
isLoadingPayments: PropTypes.bool,
/** Is the payment options menu open / active? */
isAddPaymentMenuActive: PropTypes.bool,
/** User's paypal.me username if they have one */
payPalMeUsername: PropTypes.string,
/** List of bank accounts */
bankAccountList: PropTypes.objectOf(bankAccountPropTypes),
/** List of cards */
cardList: PropTypes.objectOf(PropTypes.shape({
/** The name of the institution (bank of america, etc */
cardName: PropTypes.string,
/** The masked credit card number */
cardNumber: PropTypes.string,
/** The ID of the card in the cards DB */
cardID: PropTypes.number,
})),
userWallet: PropTypes.shape({
/** The ID of the linked account */
walletLinkedAccountID: PropTypes.number,
/** The type of the linked account (debitCard or bankAccount) */
walletLinkedAccountType: PropTypes.string,
}),
...withLocalizePropTypes,
};
const defaultProps = {
payPalMeUsername: '',
bankAccountList: {},
cardList: {},
userWallet: {
walletLinkedAccountID: 0,
walletLinkedAccountType: '',
},
isLoadingPayments: false,
isAddPaymentMenuActive: false,
};
class PaymentMethodList extends Component {
constructor(props) {
super(props);
this.renderItem = this.renderItem.bind(this);
}
/**
* Take all of the different payment methods and create a list that can be easily digested by renderItem
*
* @returns {Array}
*/
createPaymentMethodList() {
let combinedPaymentMethods = PaymentUtils.formatPaymentMethods(this.props.bankAccountList, this.props.cardList, this.props.payPalMeUsername, this.props.userWallet);
combinedPaymentMethods = _.map(combinedPaymentMethods, paymentMethod => ({
...paymentMethod,
type: MENU_ITEM,
onPress: e => this.props.onPress(e, paymentMethod.accountType, paymentMethod.accountData),
}));
// If we have not added any payment methods, show a default empty state
if (_.isEmpty(combinedPaymentMethods)) {
combinedPaymentMethods.push({
key: 'addFirstPaymentMethodHelpText',
text: this.props.translate('paymentMethodList.addFirstPaymentMethod'),
});
}
combinedPaymentMethods.push({
type: MENU_ITEM,
title: this.props.translate('paymentMethodList.addPaymentMethod'),
icon: Expensicons.Plus,
onPress: e => this.props.onPress(e),
key: 'addPaymentMethodButton',
disabled: this.props.isLoadingPayments,
iconFill: this.props.isAddPaymentMenuActive ? StyleUtils.getIconFillColor(CONST.BUTTON_STATES.PRESSED) : null,
wrapperStyle: this.props.isAddPaymentMenuActive ? [StyleUtils.getButtonBackgroundColorStyle(CONST.BUTTON_STATES.PRESSED)] : [],
});
return combinedPaymentMethods;
}
/**
* Create a menuItem for each passed paymentMethod
*
* @param {Object} params
* @param {Object} params.item
*
* @return {React.Component}
*/
renderItem({item}) {
if (item.type === MENU_ITEM) {
return (
<MenuItem
onPress={item.onPress}
title={item.title}
description={item.description}
icon={item.icon}
disabled={item.disabled}
iconFill={item.iconFill}
iconHeight={item.iconSize}
iconWidth={item.iconSize}
badgeText={item.isDefault ? this.props.translate('paymentMethodList.defaultPaymentMethod') : null}
wrapperStyle={item.wrapperStyle}
/>
);
}
return (
<Text
style={[styles.popoverMenuItem]}
>
{item.text}
</Text>
);
}
render() {
return (
<FlatList
data={this.createPaymentMethodList()}
renderItem={this.renderItem}
keyExtractor={item => item.key}
/>
);
}
}
PaymentMethodList.propTypes = propTypes;
PaymentMethodList.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
cardList: {
key: ONYXKEYS.CARD_LIST,
},
payPalMeUsername: {
key: ONYXKEYS.NVP_PAYPAL_ME_ADDRESS,
},
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
}),
)(PaymentMethodList);