forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessMoneyReportHoldMenu.tsx
More file actions
178 lines (157 loc) · 7.3 KB
/
ProcessMoneyReportHoldMenu.tsx
File metadata and controls
178 lines (157 loc) · 7.3 KB
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 {hasSeenTourSelector} from '@selectors/Onboarding';
import React, {useMemo} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import {hasOnlyNonReimbursableTransactions, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {approveMoneyRequest, payMoneyRequest} from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import DecisionModal from './DecisionModal';
import {useDelegateNoAccessActions, useDelegateNoAccessState} from './DelegateNoAccessModalProvider';
type ActionHandledType = DeepValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE.PAY | typeof CONST.IOU.REPORT_ACTION_TYPE.APPROVE>;
type ProcessMoneyReportHoldMenuProps = {
/** The chat report this report is linked to */
chatReport: OnyxEntry<OnyxTypes.Report>;
/** Full amount of expense report to pay */
fullAmount: string;
/** Whether modal is visible */
isVisible: boolean;
/** The report currently being looked at */
moneyRequestReport: OnyxEntry<OnyxTypes.Report>;
/** Not held amount of expense report */
nonHeldAmount?: string;
/** Callback for closing modal */
onClose: () => void;
/** Type of payment */
paymentType?: PaymentMethodType;
/** Selected VBBA ID for payment */
methodID?: number;
/** Type of action handled */
requestType?: ActionHandledType;
/** Number of transaction of a money request */
transactionCount: number;
/** Callback for displaying payment animation on IOU preview component */
startAnimation?: () => void;
/** Whether the report has non held expenses */
hasNonHeldExpenses?: boolean;
/** Callback when user attempts to pay via ACH but report has only non-reimbursable expenses */
onNonReimbursablePaymentError?: () => void;
/** Transactions associated with report */
transactions?: OnyxTypes.Transaction[];
};
function ProcessMoneyReportHoldMenu({
requestType,
nonHeldAmount = '0',
fullAmount,
onClose,
isVisible,
paymentType,
methodID,
chatReport,
moneyRequestReport,
transactionCount,
startAnimation,
hasNonHeldExpenses,
onNonReimbursablePaymentError,
transactions,
}: ProcessMoneyReportHoldMenuProps) {
const {translate} = useLocalize();
const isApprove = requestType === CONST.IOU.REPORT_ACTION_TYPE.APPROVE;
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to apply the correct modal type
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const [userBillingGracePeriodEnds] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);
const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID);
const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED);
const [ownerBillingGracePeriodEnd] = useOnyx(ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END);
const activePolicy = usePolicy(activePolicyID);
const policy = usePolicy(moneyRequestReport?.policyID);
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
const [moneyRequestReportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${moneyRequestReport?.reportID}`);
const {isBetaEnabled} = usePermissions();
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
const currentUserDetails = useCurrentUserPersonalDetails();
const hasViolations = hasViolationsReportUtils(moneyRequestReport?.reportID, transactionViolations, currentUserDetails.accountID, currentUserDetails.email ?? '');
const {isDelegateAccessRestricted} = useDelegateNoAccessState();
const {showDelegateNoAccessModal} = useDelegateNoAccessActions();
const onSubmit = (full: boolean) => {
if (isDelegateAccessRestricted) {
showDelegateNoAccessModal();
return;
}
if (!isApprove && chatReport && hasOnlyNonReimbursableTransactions(moneyRequestReport?.reportID, transactions) && paymentType && paymentType !== CONST.IOU.PAYMENT_TYPE.ELSEWHERE) {
onClose();
onNonReimbursablePaymentError?.();
return;
}
if (isApprove) {
approveMoneyRequest({
expenseReport: moneyRequestReport,
policy: activePolicy,
currentUserAccountIDParam: currentUserDetails.accountID,
currentUserEmailParam: currentUserDetails.email ?? '',
hasViolations,
isASAPSubmitBetaEnabled,
expenseReportCurrentNextStepDeprecated: moneyRequestReportNextStep,
betas,
userBillingGracePeriodEnds,
amountOwed,
ownerBillingGracePeriodEnd,
full,
onApproved: startAnimation,
});
} else if (chatReport && paymentType) {
payMoneyRequest({
paymentType,
chatReport,
iouReport: moneyRequestReport,
introSelected,
iouReportCurrentNextStepDeprecated: moneyRequestReportNextStep,
currentUserAccountID: currentUserDetails.accountID,
full,
activePolicy,
policy,
betas,
isSelfTourViewed,
userBillingGracePeriodEnds,
amountOwed,
ownerBillingGracePeriodEnd,
methodID,
onPaid: startAnimation,
});
}
onClose();
};
const promptText = useMemo(() => {
if (hasNonHeldExpenses) {
return translate(isApprove ? 'iou.confirmApprovalAmount' : 'iou.confirmPayAmount');
}
return translate(isApprove ? 'iou.confirmApprovalAllHoldAmount' : 'iou.confirmPayAllHoldAmount', {count: transactionCount});
}, [hasNonHeldExpenses, transactionCount, translate, isApprove]);
return (
<DecisionModal
title={translate(isApprove ? 'iou.confirmApprove' : 'iou.confirmPay')}
onClose={onClose}
isVisible={isVisible}
prompt={promptText}
firstOptionText={hasNonHeldExpenses ? `${translate(isApprove ? 'iou.approveOnly' : 'iou.payOnly')} ${nonHeldAmount}` : undefined}
secondOptionText={`${translate(isApprove ? 'iou.approve' : 'iou.pay')} ${fullAmount}`}
onFirstOptionSubmit={() => onSubmit(false)}
onSecondOptionSubmit={() => onSubmit(true)}
isSmallScreenWidth={isSmallScreenWidth}
/>
);
}
export default ProcessMoneyReportHoldMenu;
export type {ActionHandledType};