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
Show file tree
Hide file tree
Changes from 10 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
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ const ONYXKEYS = {
/** User's Expensify Wallet */
USER_WALLET: 'userWallet',

/** The method ID of the latest added payment method */
LATEST_ADDED_BANK_ACCOUNT_PLAID_ID: 'latestAddedBankAccountPlaidID',

/** User's metadata that will be used to segmentation */
USER_METADATA: 'userMetadata',

Expand Down Expand Up @@ -947,6 +950,7 @@ type OnyxValuesMapping = {
[ONYXKEYS.NVP_PRIVATE_BILLING_DISPUTE_PENDING]: number;
[ONYXKEYS.NVP_PRIVATE_BILLING_STATUS]: OnyxTypes.BillingStatus;
[ONYXKEYS.USER_WALLET]: OnyxTypes.UserWallet;
[ONYXKEYS.LATEST_ADDED_BANK_ACCOUNT_PLAID_ID]: string;
[ONYXKEYS.WALLET_ONFIDO]: OnyxTypes.WalletOnfido;
[ONYXKEYS.WALLET_ADDITIONAL_DETAILS]: OnyxTypes.WalletAdditionalDetails;
[ONYXKEYS.WALLET_TERMS]: OnyxTypes.WalletTerms;
Expand Down
5 changes: 5 additions & 0 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ function addPersonalBankAccount(account: PlaidBankAccount) {
currentStep: CONST.WALLET.STEP.ADDITIONAL_DETAILS,
},
},
{
onyxMethod: Onyx.METHOD.SET,
key: ONYXKEYS.LATEST_ADDED_BANK_ACCOUNT_PLAID_ID,
value: account.plaidAccountID,
},
],
failureData: [
{
Expand Down
30 changes: 27 additions & 3 deletions src/pages/settings/Wallet/PaymentMethodList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,18 @@ function dismissError(item: PaymentMethodItem) {
}
}

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

// If there are two or more default payment methods, show the default badge only for the most recently added default account.
if (defaultPaymentMethods.length > 1) {
// Return true only if the methodID matches the most recently created default account
return item.accountData?.additionalData?.plaidAccountID === latestAddedBankAccountPlaidID;
}
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 +201,7 @@ function PaymentMethodList({

const [isUserValidated] = useOnyx(ONYXKEYS.USER, {selector: (user) => !!user?.validated});
const [bankAccountList = {}, bankAccountListResult] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [latestAddedBankAccountPlaidID] = useOnyx(ONYXKEYS.LATEST_ADDED_BANK_ACCOUNT_PLAID_ID);
const isLoadingBankAccountList = isLoadingOnyxValue(bankAccountListResult);
const [cardList = {}, cardListResult] = useOnyx(ONYXKEYS.CARD_LIST);
const isLoadingCardList = isLoadingOnyxValue(cardListResult);
Expand Down Expand Up @@ -404,7 +412,12 @@ function PaymentMethodList({
iconWidth={item.iconWidth ?? item.iconSize}
iconStyles={item.iconStyles}
badgeText={
shouldShowDefaultBadge(filteredPaymentMethods, invoiceTransferBankAccountID ? invoiceTransferBankAccountID === item.methodID : item.isDefault)
shouldShowDefaultBadge(
filteredPaymentMethods,
item,
latestAddedBankAccountPlaidID,
invoiceTransferBankAccountID ? invoiceTransferBankAccountID === item.methodID : item.isDefault,
)
? translate('paymentMethodList.defaultPaymentMethod')
: undefined
}
Expand All @@ -421,7 +434,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,
latestAddedBankAccountPlaidID,
],
);

return (
Expand Down
Loading