From 839e379b203613a0b6dce452d5e998c5f5125443 Mon Sep 17 00:00:00 2001 From: Shahidullah Muffakir Date: Fri, 25 Oct 2024 00:04:20 +0530 Subject: [PATCH] Ensure only latest bank account shows default badge when adding multiple accounts --- src/pages/settings/Wallet/PaymentMethodList.tsx | 14 ++++++++++++-- src/types/onyx/AccountData.ts | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/pages/settings/Wallet/PaymentMethodList.tsx b/src/pages/settings/Wallet/PaymentMethodList.tsx index 23d0b5ab6550..8d5f2d66b521 100644 --- a/src/pages/settings/Wallet/PaymentMethodList.tsx +++ b/src/pages/settings/Wallet/PaymentMethodList.tsx @@ -149,11 +149,21 @@ function dismissError(item: PaymentMethodItem) { } } -function shouldShowDefaultBadge(filteredPaymentMethods: PaymentMethod[], isDefault = false): boolean { +function shouldShowDefaultBadge(filteredPaymentMethods: PaymentMethod[], isDefault = false, item: PaymentMethod): boolean { if (!isDefault) { return false; } + // Find all payment methods that are marked as default + const defaultPaymentMethods = filteredPaymentMethods.filter((method): method is PaymentMethod & {accountData: AccountData} => Boolean(method.isDefault)); + // If there are two or more default payment methods, find the most recently created one + if (defaultPaymentMethods.length > 1) { + // Sort default payment methods by creation date to find the most recent + const mostRecentDefaultMethod = defaultPaymentMethods.reduce((latest, current) => (new Date(current.accountData.created) > new Date(latest.accountData.created) ? current : latest)); + + // Return true only if the methodID matches the most recently created default account + return mostRecentDefaultMethod.methodID === item.methodID; + } const defaultablePaymentMethodCount = filteredPaymentMethods.filter( (method) => method.accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT || method.accountType === CONST.PAYMENT_METHODS.DEBIT_CARD, ).length; @@ -401,7 +411,7 @@ function PaymentMethodList({ iconWidth={item.iconWidth ?? item.iconSize} iconStyles={item.iconStyles} badgeText={ - shouldShowDefaultBadge(filteredPaymentMethods, invoiceTransferBankAccountID ? invoiceTransferBankAccountID === item.methodID : item.isDefault) + shouldShowDefaultBadge(filteredPaymentMethods, invoiceTransferBankAccountID ? invoiceTransferBankAccountID === item.methodID : item.isDefault, item) ? translate('paymentMethodList.defaultPaymentMethod') : undefined } diff --git a/src/types/onyx/AccountData.ts b/src/types/onyx/AccountData.ts index 6bb69cd78dc4..438f62dacec6 100644 --- a/src/types/onyx/AccountData.ts +++ b/src/types/onyx/AccountData.ts @@ -50,6 +50,9 @@ type AccountData = { /** The debit card ID */ fundID?: number; + + /** Date that the account was created */ + created: string; }; export default AccountData;