2023-07-27 13:27:34 +08:00
|
|
|
import { useState } from 'react'
|
|
|
|
import { useDebounceFn } from 'ahooks'
|
|
|
|
import type { DebouncedFunc } from 'lodash-es'
|
|
|
|
import { ValidatedStatus } from './declarations'
|
|
|
|
import type { ValidateCallback, ValidateValue, ValidatedStatusState } from './declarations'
|
|
|
|
|
|
|
|
export const useValidate: (value: ValidateValue) => [DebouncedFunc<(validateCallback: ValidateCallback) => Promise<void>>, boolean, ValidatedStatusState] = (value) => {
|
|
|
|
const [validating, setValidating] = useState(false)
|
|
|
|
const [validatedStatus, setValidatedStatus] = useState<ValidatedStatusState>({})
|
|
|
|
|
|
|
|
const { run } = useDebounceFn(async (validateCallback: ValidateCallback) => {
|
|
|
|
if (!validateCallback.before(value)) {
|
|
|
|
setValidating(false)
|
|
|
|
setValidatedStatus({})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setValidating(true)
|
|
|
|
|
|
|
|
if (validateCallback.run) {
|
|
|
|
const res = await validateCallback?.run(value)
|
|
|
|
setValidatedStatus(
|
|
|
|
res.status === 'success'
|
|
|
|
? { status: ValidatedStatus.Success }
|
|
|
|
: { status: ValidatedStatus.Error, message: res.message })
|
|
|
|
|
|
|
|
setValidating(false)
|
|
|
|
}
|
2023-08-12 00:57:13 +08:00
|
|
|
}, { wait: 1000 })
|
2023-07-27 13:27:34 +08:00
|
|
|
|
|
|
|
return [run, validating, validatedStatus]
|
|
|
|
}
|