Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React from 'react';
import Button from '@components/Button';
import {useDelegateNoAccessActions, useDelegateNoAccessState} from '@components/DelegateNoAccessModalProvider';
import {ModalActions} from '@components/Modal/Global/ModalContext';
import useConfirmModal from '@hooks/useConfirmModal';
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 {hasDynamicExternalWorkflow} from '@libs/PolicyUtils';
import {hasHeldExpenses as hasHeldExpensesReportUtils, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
import {approveMoneyRequest} from '@userActions/IOU';
import {openOldDotLink} from '@userActions/Link';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

type ApproveActionButtonProps = {
iouReportID: string | undefined;
startApprovedAnimation: () => void;
onHoldMenuOpen: (requestType: string) => void;
};

function ApproveActionButton({iouReportID, startApprovedAnimation, onHoldMenuOpen}: ApproveActionButtonProps) {
const {translate} = useLocalize();
const currentUserDetails = useCurrentUserPersonalDetails();
const currentUserAccountID = currentUserDetails.accountID;
const currentUserEmail = currentUserDetails.email ?? '';
const {isBetaEnabled} = usePermissions();
const {showConfirmModal} = useConfirmModal();
const {isDelegateAccessRestricted} = useDelegateNoAccessState();
const {showDelegateNoAccessModal} = useDelegateNoAccessActions();

const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID);
const activePolicy = usePolicy(activePolicyID);
const [iouReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${iouReport?.policyID}`);
const [userBillingGraceEndPeriods] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);
const [iouReportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${iouReportID}`);
const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED);
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [betas] = useOnyx(ONYXKEYS.BETAS);

const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
const isDEWBetaEnabled = isBetaEnabled(CONST.BETAS.NEW_DOT_DEW);
const hasViolations = hasViolationsReportUtils(iouReport?.reportID, transactionViolations, currentUserAccountID, currentUserEmail);

const confirmApproval = () => {
if (hasDynamicExternalWorkflow(policy) && !isDEWBetaEnabled) {
showConfirmModal({
title: translate('customApprovalWorkflow.title'),
prompt: translate('customApprovalWorkflow.description'),
confirmText: translate('customApprovalWorkflow.goToExpensifyClassic'),
shouldShowCancelButton: false,
}).then((result) => {
if (result.action !== ModalActions.CONFIRM) {
return;
}
openOldDotLink(CONST.OLDDOT_URLS.INBOX);
});
return;
}
if (isDelegateAccessRestricted) {
showDelegateNoAccessModal();
} else if (hasHeldExpensesReportUtils(iouReport?.reportID)) {
onHoldMenuOpen(CONST.IOU.REPORT_ACTION_TYPE.APPROVE);
} else {
approveMoneyRequest({
expenseReport: iouReport,
policy: activePolicy,
currentUserAccountIDParam: currentUserAccountID,
currentUserEmailParam: currentUserEmail,
hasViolations,
isASAPSubmitBetaEnabled,
expenseReportCurrentNextStepDeprecated: iouReportNextStep,
betas,
userBillingGraceEndPeriods,
amountOwed,
full: true,
onApproved: startApprovedAnimation,
});
}
};

return (
<Button
text={translate('iou.approve')}
success
onPress={confirmApproval}
sentryLabel={CONST.SENTRY_LABEL.REPORT_PREVIEW.APPROVE_BUTTON}
/>
);
}

export default ApproveActionButton;
Loading
Loading