-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AddPaymentMethodMenu.tsx
137 lines (118 loc) · 5.02 KB
/
AddPaymentMethodMenu.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
import type {RefObject} from 'react';
import React, {useEffect} from 'react';
import type {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import useLocalize from '@hooks/useLocalize';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {AnchorPosition} from '@src/styles';
import type {Report, Session} from '@src/types/onyx';
import type AnchorAlignment from '@src/types/utils/AnchorAlignment';
import * as Expensicons from './Icon/Expensicons';
import type {PaymentMethod} from './KYCWall/types';
import PopoverMenu from './PopoverMenu';
type AddPaymentMethodMenuOnyxProps = {
/** Session info for the currently logged-in user. */
session: OnyxEntry<Session>;
};
type AddPaymentMethodMenuProps = AddPaymentMethodMenuOnyxProps & {
/** Should the component be visible? */
isVisible: boolean;
/** Callback to execute when the component closes. */
onClose: () => void;
/** Callback to execute when the payment method is selected. */
onItemSelected: (paymentMethod: PaymentMethod) => void;
/** The IOU/Expense report we are paying */
iouReport?: OnyxEntry<Report>;
/** Anchor position for the AddPaymentMenu. */
anchorPosition: AnchorPosition;
/** Where the popover should be positioned relative to the anchor points. */
anchorAlignment?: AnchorAlignment;
/** Popover anchor ref */
anchorRef: RefObject<View | HTMLDivElement>;
/** Whether the personal bank account option should be shown */
shouldShowPersonalBankAccountOption?: boolean;
};
function AddPaymentMethodMenu({
isVisible,
onClose,
anchorPosition,
anchorAlignment = {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
},
anchorRef,
iouReport,
onItemSelected,
session,
shouldShowPersonalBankAccountOption = false,
}: AddPaymentMethodMenuProps) {
const {translate} = useLocalize();
// Users can choose to pay with business bank account in case of Expense reports or in case of P2P IOU report
// which then starts a bottom up flow and creates a Collect workspace where the payer is an admin and payee is an employee.
const isIOUReport = ReportUtils.isIOUReport(iouReport);
const canUseBusinessBankAccount =
ReportUtils.isExpenseReport(iouReport) || (isIOUReport && !ReportActionsUtils.hasRequestFromCurrentAccount(iouReport?.reportID ?? '-1', session?.accountID ?? -1));
const canUsePersonalBankAccount = shouldShowPersonalBankAccountOption || isIOUReport;
const isPersonalOnlyOption = canUsePersonalBankAccount && !canUseBusinessBankAccount;
// We temporarily disabled P2P debit cards so we will automatically select the personal bank account option if there is no other option to select.
useEffect(() => {
if (!isVisible) {
return;
}
onItemSelected(CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT);
}, [isPersonalOnlyOption, isVisible, onItemSelected]);
if (isPersonalOnlyOption) {
return null;
}
return (
<PopoverMenu
isVisible={isVisible}
onClose={onClose}
anchorPosition={anchorPosition}
anchorAlignment={anchorAlignment}
anchorRef={anchorRef}
onItemSelected={onClose}
menuItems={[
...(canUsePersonalBankAccount
? [
{
text: translate('common.personalBankAccount'),
icon: Expensicons.Bank,
onSelected: () => {
onItemSelected(CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT);
},
},
]
: []),
...(canUseBusinessBankAccount
? [
{
text: translate('common.businessBankAccount'),
icon: Expensicons.Building,
onSelected: () => onItemSelected(CONST.PAYMENT_METHODS.BUSINESS_BANK_ACCOUNT),
},
]
: []),
// Adding a debit card for P2P payments is temporarily disabled
// ...[
// {
// text: translate('common.debitCard'),
// icon: Expensicons.CreditCard,
// onSelected: () => onItemSelected(CONST.PAYMENT_METHODS.DEBIT_CARD),
// },
// ],
]}
withoutOverlay
/>
);
}
AddPaymentMethodMenu.displayName = 'AddPaymentMethodMenu';
export default withOnyx<AddPaymentMethodMenuProps, AddPaymentMethodMenuOnyxProps>({
session: {
key: ONYXKEYS.SESSION,
},
})(AddPaymentMethodMenu);