Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions public/game/peanut-game.html
Comment thread
kushagrasarathe marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@
* Maximum obstacle grouping count.
* @const
*/
;(Obstacle.MAX_OBSTACLE_LENGTH = 3),
;((Obstacle.MAX_OBSTACLE_LENGTH = 3),
(Obstacle.prototype = {
/**
* Initialise the DOM for the obstacle.
Expand Down Expand Up @@ -1103,7 +1103,7 @@
)
}
},
})
}))
/**
* Obstacle definitions.
* minGap: minimum pixel space betweeen obstacles.
Expand Down
97 changes: 70 additions & 27 deletions src/components/AddWithdraw/DynamicBankAccountForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import { forwardRef, useImperativeHandle, useMemo, useState } from 'react'
import { forwardRef, useImperativeHandle, useMemo, useState, useEffect } from 'react'
import { useForm, Controller } from 'react-hook-form'
import { useAuth } from '@/context/authContext'
import { Button } from '@/components/0_Bruddle/Button'
Expand Down Expand Up @@ -65,6 +65,7 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D
control,
handleSubmit,
setValue,
getValues,
formState: { errors, isValid, isValidating, touchedFields },
} = useForm<IBankAccountDetails>({
defaultValues: {
Expand All @@ -82,15 +83,33 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D
...initialData,
},
mode: 'onBlur',
reValidateMode: 'onSubmit',
})

useImperativeHandle(ref, () => ({
handleSubmit: handleSubmit(onSubmit),
}))

// Clear submission error when form becomes valid and BIC field is filled (if shown)
useEffect(() => {
if (submissionError && isValid && (!showBicField || getValues('bic'))) {
setSubmissionError(null)
}
}, [isValid, submissionError, showBicField, getValues])

const onSubmit = async (data: IBankAccountDetails) => {
// If validation is still running, don't proceed
if (isValidating) {
console.log('Validation still checking, skipping submission')
return
}

// Clear any existing submission errors before starting
if (submissionError) {
setSubmissionError(null)
}

setIsSubmitting(true)
setSubmissionError(null)
try {
const isUs = country.toUpperCase() === 'USA'
const isMx = country.toUpperCase() === 'MX'
Expand All @@ -105,22 +124,38 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D
const accountNumber = isMx ? data.clabe : data.accountNumber

const { firstName, lastName } = data
let bic = data.bic
let bic = data.bic || getValues('bic')
const iban = data.iban || getValues('iban')

if (isIban && !bic) {
try {
bic = await getBicFromIban(accountNumber)
if (!bic) {
// for IBAN countries, ensure BIC is available
if (isIban) {
// if BIC field is shown but empty, don't proceed
if (showBicField && !bic) {
setIsSubmitting(false)
setSubmissionError('BIC is required')
return
}

// if BIC field is not shown and no BIC available, try to get it automatically
if (!showBicField && !bic) {
try {
const autoBic = await getBicFromIban(accountNumber)
if (autoBic) {
bic = autoBic
// set the BIC value in the form without showing the field
setValue('bic', autoBic, { shouldValidate: false })
} else {
setShowBicField(true)
setIsSubmitting(false)
setSubmissionError('BIC is required')
return
}
} catch (error) {
setShowBicField(true)
setIsSubmitting(false)
setSubmissionError('BIC is required')
return
}
} catch (error) {
setShowBicField(true)
setIsSubmitting(false)
setSubmissionError('BIC is required')
return
}
}

Expand Down Expand Up @@ -150,7 +185,7 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D

const result = await onSuccess(payload as AddBankAccountPayload, {
...data,
iban: isIban ? data.accountNumber : undefined,
iban: isIban ? data.accountNumber || iban : undefined,
accountNumber: isIban ? '' : data.accountNumber,
bic: bic,
country,
Expand All @@ -164,7 +199,6 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D
}
} catch (error: any) {
setSubmissionError(error.message)
} finally {
setIsSubmitting(false)
}
}
Expand Down Expand Up @@ -233,13 +267,7 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D

<div className="space-y-4">
<h3 className="text-base font-bold">Enter bank account details</h3>
<form
onSubmit={(e) => {
e.preventDefault()
handleSubmit(onSubmit)()
}}
className="space-y-4"
>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
{flow === 'claim' && !user?.user.userId && (
<div className="w-full space-y-4">
{renderInput('firstName', 'First Name', { required: 'First name is required' })}
Expand Down Expand Up @@ -314,10 +342,25 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D

{isIban &&
showBicField &&
renderInput('bic', 'BIC', {
required: 'BIC is required',
validate: async (value: string) => (await validateBic(value)) || 'Invalid BIC code',
})}
renderInput(
'bic',
'BIC',
{
required: 'BIC is required',
validate: async (value: string) => {
if (!value || value.trim().length === 0) return 'BIC is required'
const isValid = await validateBic(value.trim())
return isValid || 'Invalid BIC code'
},
},
'text',
undefined,
(field) => {
if (field.value && field.value.trim().length > 0 && submissionError) {
setSubmissionError(null)
}
}
)}
{isUs &&
renderInput('routingNumber', 'Routing Number', {
required: 'Routing number is required',
Expand Down Expand Up @@ -345,8 +388,8 @@ export const DynamicBankAccountForm = forwardRef<{ handleSubmit: () => void }, D
variant="purple"
shadowSize="4"
className="!mt-4 w-full"
loading={isSubmitting || isValidating}
disabled={isSubmitting || !isValid || isValidating}
loading={isSubmitting}
disabled={isSubmitting || !isValid}
>
Review
</Button>
Expand Down
Loading