Skip to content

Commit

Permalink
Ensure only latest bank account shows default badge when adding multi…
Browse files Browse the repository at this point in the history
…ple accounts
  • Loading branch information
Shahidullah-Muffakir committed Oct 24, 2024
1 parent 476ad6b commit 839e379
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/pages/settings/Wallet/PaymentMethodList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,21 @@ function dismissError(item: PaymentMethodItem) {
}
}

function shouldShowDefaultBadge(filteredPaymentMethods: PaymentMethod[], isDefault = false): boolean {
function shouldShowDefaultBadge(filteredPaymentMethods: PaymentMethod[], isDefault = false, item: PaymentMethod): boolean {

Check failure on line 152 in src/pages/settings/Wallet/PaymentMethodList.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Default parameters should be last
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));

Check failure on line 157 in src/pages/settings/Wallet/PaymentMethodList.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Use !! instead of Boolean()

// 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;
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/AccountData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type AccountData = {

/** The debit card ID */
fundID?: number;

/** Date that the account was created */
created: string;
};

export default AccountData;

0 comments on commit 839e379

Please sign in to comment.