Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure only latest bank account shows default badge #51437

Merged
merged 16 commits into from
Nov 14, 2024
Merged
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
34 changes: 31 additions & 3 deletions src/pages/settings/Wallet/PaymentMethodList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,22 @@ function dismissError(item: PaymentMethodItem) {
}
}

function shouldShowDefaultBadge(filteredPaymentMethods: PaymentMethod[], isDefault = false): boolean {
function shouldShowDefaultBadge(filteredPaymentMethods: PaymentMethod[], item: PaymentMethod, walletLinkedAccountID: number, isDefault = false): boolean {
if (!isDefault) {
return false;
}
// Find all payment methods that are marked as default
const defaultPaymentMethods = filteredPaymentMethods.filter((method: PaymentMethod) => !!method.isDefault);

// If there is more than one payment method, show the default badge only for the most recently added default account.
if (defaultPaymentMethods.length > 1) {
if (item.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT) {
return item.accountData?.bankAccountID === walletLinkedAccountID;
}
if (item.accountType === CONST.PAYMENT_METHODS.DEBIT_CARD) {
return item.accountData?.fundID === walletLinkedAccountID;
}
}
const defaultablePaymentMethodCount = filteredPaymentMethods.filter(
(method) => method.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT || method.accountType === CONST.PAYMENT_METHODS.DEBIT_CARD,
).length;
Expand Down Expand Up @@ -194,6 +205,7 @@ function PaymentMethodList({

const [isUserValidated] = useOnyx(ONYXKEYS.USER, {selector: (user) => !!user?.validated});
const [bankAccountList = {}, bankAccountListResult] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [userWallet] = useOnyx(ONYXKEYS.USER_WALLET);
const isLoadingBankAccountList = isLoadingOnyxValue(bankAccountListResult);
const [cardList = {}, cardListResult] = useOnyx(ONYXKEYS.CARD_LIST);
const isLoadingCardList = isLoadingOnyxValue(cardListResult);
Expand Down Expand Up @@ -406,7 +418,12 @@ function PaymentMethodList({
iconWidth={item.iconWidth ?? item.iconSize}
iconStyles={item.iconStyles}
badgeText={
shouldShowDefaultBadge(filteredPaymentMethods, invoiceTransferBankAccountID ? invoiceTransferBankAccountID === item.methodID : item.isDefault)
shouldShowDefaultBadge(
filteredPaymentMethods,
item,
userWallet?.walletLinkedAccountID ?? 0,
invoiceTransferBankAccountID ? invoiceTransferBankAccountID === item.methodID : item.isDefault,
)
? translate('paymentMethodList.defaultPaymentMethod')
: undefined
}
Expand All @@ -423,7 +440,18 @@ function PaymentMethodList({
</OfflineWithFeedback>
),

[styles.ph6, styles.paymentMethod, styles.badgeBordered, filteredPaymentMethods, invoiceTransferBankAccountID, translate, listItemStyle, shouldShowSelectedState, selectedMethodID],
[
styles.ph6,
styles.paymentMethod,
styles.badgeBordered,
filteredPaymentMethods,
invoiceTransferBankAccountID,
translate,
listItemStyle,
shouldShowSelectedState,
selectedMethodID,
userWallet?.walletLinkedAccountID,
],
);

return (
Expand Down
Loading