Skip to content

Commit

Permalink
Adds debounce to address validation
Browse files Browse the repository at this point in the history
  • Loading branch information
neokry committed Mar 27, 2023
1 parent aa94ec9 commit 2843879
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { debounce } from 'lodash'
import * as Yup from 'yup'

import { isValidAddress } from 'src/utils/ens'
import { getProvider } from 'src/utils/provider'

const validateAddress = async (
value: string | undefined,
res: (value: boolean | PromiseLike<boolean>) => void
) => {
try {
res(!!value && (await isValidAddress(value, getProvider())))
} catch (err) {
res(false)
}
}

export const deboucedValidateAddress = debounce(validateAddress, 500)

const allocationSchema = Yup.object().shape({
founderAddress: Yup.string()
.test(
'isValidAddress',
'invalid address',
(value: string | undefined) => !!value && isValidAddress(value, getProvider())
(value) => new Promise((res) => deboucedValidateAddress(value, res))
)
.required('*'),
allocationPercentage: Yup.number()
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/modules/create-dao/components/VetoForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
defaultFormButtonWithPrev,
} from 'src/components/Fields/styles.css'
import { Icon } from 'src/components/Icon'
import { getEnsAddress, isValidAddress } from 'src/utils/ens'
import { getEnsAddress } from 'src/utils/ens'
import { isEmpty } from 'src/utils/helpers'
import { getProvider } from 'src/utils/provider'

import { useFormStore } from '../stores'
import { deboucedValidateAddress } from './AllocationForm/AllocationForm.schema'

interface VetoFormProps {
title: string
Expand Down Expand Up @@ -44,7 +44,7 @@ export const vetoValidationSchema = Yup.object().shape({
.test(
'isValidAddress',
'invalid address',
(value: string | undefined) => !!value && isValidAddress(value, getProvider())
(value) => new Promise((res) => deboucedValidateAddress(value, res))
),
}),
})
Expand Down

0 comments on commit 2843879

Please sign in to comment.