diff --git a/src/components/AddWithdraw/DynamicBankAccountForm.tsx b/src/components/AddWithdraw/DynamicBankAccountForm.tsx index b87e3d3b9..6186bb784 100644 --- a/src/components/AddWithdraw/DynamicBankAccountForm.tsx +++ b/src/components/AddWithdraw/DynamicBankAccountForm.tsx @@ -6,7 +6,7 @@ import { Button } from '@/components/0_Bruddle/Button' import { AddBankAccountPayload, BridgeAccountOwnerType, BridgeAccountType } from '@/app/actions/types/users.types' import BaseInput from '@/components/0_Bruddle/BaseInput' import { countryCodeMap } from '@/components/AddMoney/consts' -import { useParams } from 'next/navigation' +import { useParams, useRouter } from 'next/navigation' import { validateIban, validateBic, isValidRoutingNumber } from '@/utils/bridge-accounts.utils' import ErrorAlert from '@/components/Global/ErrorAlert' import { getBicFromIban } from '@/app/actions/ibanToBic' @@ -14,6 +14,7 @@ import PeanutActionDetailsCard, { PeanutActionDetailsCardProps } from '../Global import { PEANUT_WALLET_TOKEN_SYMBOL } from '@/constants' import { useWithdrawFlow } from '@/context/WithdrawFlowContext' import { getCountryFromIban, validateMXCLabeAccount, validateUSBankAccount } from '@/utils/withdraw.utils' +import useSavedAccounts from '@/hooks/useSavedAccounts' import { useAppDispatch, useAppSelector } from '@/redux/hooks' import { bankFormActions } from '@/redux/slices/bank-form-slice' import { useDebounce } from '@/hooks/useDebounce' @@ -72,9 +73,11 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D const [isSubmitting, setIsSubmitting] = useState(false) const [submissionError, setSubmissionError] = useState(null) const { country: countryNameParams } = useParams() - const { amountToWithdraw } = useWithdrawFlow() + const { amountToWithdraw, setSelectedBankAccount } = useWithdrawFlow() const [firstName, ...lastNameParts] = (user?.user.fullName ?? '').split(' ') const lastName = lastNameParts.join(' ') + const router = useRouter() + const savedAccounts = useSavedAccounts() const [isCheckingBICValid, setisCheckingBICValid] = useState(false) let selectedCountry = (countryNameFromProps ?? (countryNameParams as string)).toLowerCase() @@ -146,6 +149,17 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D setIsSubmitting(true) try { + const existingAccount = savedAccounts.find( + (account) => account.identifier === (data.accountNumber.toLowerCase() || data.clabe.toLowerCase()) + ) + + // Skip adding account if the account already exists for the logged in user + if (existingAccount) { + setSelectedBankAccount(existingAccount) + router.push(`/withdraw/${country}/bank`) + return + } + const isUs = country.toUpperCase() === 'USA' const isMx = country.toUpperCase() === 'MX' const isIban = isUs || isMx ? false : isIBANCountry(country)