Skip to content
Closed
Changes from all 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
52 changes: 21 additions & 31 deletions src/components/SettlementButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isEmpty from 'lodash/isEmpty';
import truncate from 'lodash/truncate';
import React, {useEffect, useMemo, useRef} from 'react';
import React, {useEffect, useMemo} from 'react';
import type {GestureResponderEvent} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import type {TupleToUnion} from 'type-fest';
Expand Down Expand Up @@ -31,7 +31,7 @@ import {approveMoneyRequest, resetPreferredPaymentMethod, savePreferredPaymentMe
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {BankAccount, LastPaymentMethodType, Policy} from '@src/types/onyx';
import type {AccountData, BankAccount, LastPaymentMethodType, Policy} from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';
Expand Down Expand Up @@ -119,36 +119,30 @@ function SettlementButton({
// whether the user has single policy and the expense isn't inside a workspace
const hasSinglePolicy = !policy && activeAdminPolicies.length === 1;
const hasMultiplePolicies = !policy && activeAdminPolicies.length > 1;
const lastPaymentMethodRef = useRef(lastPaymentMethod);
const formattedPaymentMethods = formatPaymentMethods(bankAccountList, fundList, styles);
const hasIntentToPay = formattedPaymentMethods.length === 1 && !lastPaymentMethod;

useEffect(() => {
if (isLoadingLastPaymentMethod) {
return;
}
lastPaymentMethodRef.current = lastPaymentMethod;
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [isLoadingLastPaymentMethod]);

const isInvoiceReport = (!isEmptyObject(iouReport) && isInvoiceReportUtil(iouReport)) || false;
const shouldShowPayWithExpensifyOption = !shouldHidePaymentOptions;
const shouldShowPayElsewhereOption = !shouldHidePaymentOptions && !isInvoiceReport;

function getPaymentSubitems(payAsBusiness: boolean) {
return formattedPaymentMethods.map((formattedPaymentMethod) => ({
text: formattedPaymentMethod?.title ?? '',
description: formattedPaymentMethod?.description ?? '',
icon: formattedPaymentMethod?.icon,
shouldUpdateSelectedIndex: true,
onSelected: () => {
onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, payAsBusiness, formattedPaymentMethod.methodID, formattedPaymentMethod.accountType, undefined);
},
iconStyles: formattedPaymentMethod?.iconStyles,
iconHeight: formattedPaymentMethod?.iconSize,
iconWidth: formattedPaymentMethod?.iconSize,
value: CONST.IOU.PAYMENT_TYPE.EXPENSIFY,
}));
return formattedPaymentMethods
.filter((v) =>
payAsBusiness ? (v?.accountData as AccountData)?.type === CONST.BANK_ACCOUNT.TYPE.BUSINESS : (v?.accountData as AccountData)?.type === CONST.BANK_ACCOUNT.TYPE.PERSONAL,
)
.map((formattedPaymentMethod) => ({
text: formattedPaymentMethod?.title ?? '',
description: formattedPaymentMethod?.description ?? '',
icon: formattedPaymentMethod?.icon,
shouldUpdateSelectedIndex: true,
onSelected: () => {
onPress(CONST.IOU.PAYMENT_TYPE.EXPENSIFY, payAsBusiness, formattedPaymentMethod.methodID, formattedPaymentMethod.accountType, undefined);
},
iconStyles: formattedPaymentMethod?.iconStyles,
iconHeight: formattedPaymentMethod?.iconSize,
iconWidth: formattedPaymentMethod?.iconSize,
value: CONST.IOU.PAYMENT_TYPE.EXPENSIFY,
}));
}

function getLatestBankAccountItem() {
Expand Down Expand Up @@ -347,10 +341,6 @@ function SettlementButton({
buttonOptions.push(approveButtonOption);
}

// Put the preferred payment method to the front of the array, so it's shown as default. We assume their last payment method is their preferred.
if (lastPaymentMethodRef.current) {
return buttonOptions.sort((method) => (method.value === lastPaymentMethod && lastPaymentMethod !== CONST.IOU.PAYMENT_TYPE.ELSEWHERE ? -1 : 0));
}
return buttonOptions;
// We don't want to reorder the options when the preferred payment method changes while the button is still visible except for component initialization when the last payment method is not initialized yet.
// We need to be sure that onPress should be wrapped in an useCallback to prevent unnecessary updates.
Expand Down Expand Up @@ -392,7 +382,7 @@ function SettlementButton({
const currentBankInformation = formattedPaymentMethods.at(0) as BankAccount;
onPress(
CONST.IOU.PAYMENT_TYPE.EXPENSIFY,
currentBankInformation.accountType !== CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT,
currentBankInformation.accountData?.type === CONST.BANK_ACCOUNT.TYPE.BUSINESS,
currentBankInformation.methodID,
currentBankInformation.accountType,
undefined,
Expand Down Expand Up @@ -536,7 +526,7 @@ function SettlementButton({
return false;
});

const shouldUseSplitButton = hasPreferredPaymentMethod || !!lastPaymentPolicy || (!!bankAccount && isExpenseReportUtil(iouReport)) || (isInvoiceReport && hasIntentToPay);
const shouldUseSplitButton = hasPreferredPaymentMethod || !!lastPaymentPolicy || (!!bankAccount && isExpenseReportUtil(iouReport)) || hasIntentToPay;

return (
<KYCWall
Expand Down
Loading