Skip to content

Commit

Permalink
feat: added balance updated check
Browse files Browse the repository at this point in the history
  • Loading branch information
RyRy79261 committed Dec 13, 2024
1 parent fdcdaea commit 3dfd732
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/features/swap/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ export function SwapFormCard() {
}

function SwapForm() {
const balances = useAppSelector((s) => s.account.balances)
const { balances, lastUpdated } = useAppSelector((s) => s.account)
const { showSlippage } = useAppSelector((s) => s.swap)

const dispatch = useAppDispatch()
const onSubmit = (values: SwapFormValues) => {
dispatch(setFormValues(values))
dispatch(setConfirmView(true)) // Switch to confirm view
}
const validateForm = useFormValidator(balances)
const validateForm = useFormValidator(balances, lastUpdated)
const storedFormValues = useAppSelector((s) => s.swap.formValues) // Get stored form values
const initialFormValues = storedFormValues || initialValues // Use stored values if they exist

Expand Down
5 changes: 3 additions & 2 deletions src/features/swap/useFormValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { areAmountsNearlyEqual, parseAmount, toWei } from 'src/utils/amount'
import { logger } from 'src/utils/logger'
import { useChainId } from 'wagmi'

export function useFormValidator(balances: AccountBalances) {
export function useFormValidator(balances: AccountBalances, lastUpdated: number | null) {
const chainId = useChainId()
return useCallback(
(values?: SwapFormValues): Promise<FormikErrors<SwapFormValues>> => {
return (async () => {
if (!lastUpdated) return { fromTokenId: 'Balance still loading' }
if (!values || !values.amount) return { amount: 'Amount Required' }
const parsedAmount = parseAmount(values.amount)
if (!parsedAmount) return { amount: 'Amount is Invalid' }
Expand All @@ -33,7 +34,7 @@ export function useFormValidator(balances: AccountBalances) {
return {}
})
},
[balances, chainId]
[balances, chainId, lastUpdated]
)
}

Expand Down

0 comments on commit 3dfd732

Please sign in to comment.