fix: make useBooking submit() validate synchronously instead of racing setState's updater
This commit is contained in:
@@ -55,22 +55,17 @@ export default function useBooking(initialServiceId?: string) {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const submit = useCallback((): boolean => {
|
const submit = useCallback((): boolean => {
|
||||||
let result = false
|
const nextErrors: BookingErrors = {}
|
||||||
setState((s) => {
|
if (!state.contact.name.trim()) nextErrors.name = 'Name is required.'
|
||||||
const nextErrors: BookingErrors = {}
|
if (!isValidEmail(state.contact.email)) nextErrors.email = 'A valid email is required.'
|
||||||
if (!s.contact.name.trim()) nextErrors.name = 'Name is required.'
|
if (!state.contact.phone.trim()) nextErrors.phone = 'Phone is required.'
|
||||||
if (!isValidEmail(s.contact.email)) nextErrors.email = 'A valid email is required.'
|
setErrors(nextErrors)
|
||||||
if (!s.contact.phone.trim()) nextErrors.phone = 'Phone is required.'
|
if (Object.keys(nextErrors).length > 0) {
|
||||||
setErrors(nextErrors)
|
return false
|
||||||
if (Object.keys(nextErrors).length > 0) {
|
}
|
||||||
result = false
|
setState((s) => ({ ...s, step: 'confirmed' }))
|
||||||
return s
|
return true
|
||||||
}
|
}, [state.contact])
|
||||||
result = true
|
|
||||||
return { ...s, step: 'confirmed' }
|
|
||||||
})
|
|
||||||
return result
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const reset = useCallback(() => {
|
const reset = useCallback(() => {
|
||||||
setState(initialState(initialServiceId))
|
setState(initialState(initialServiceId))
|
||||||
|
|||||||
Reference in New Issue
Block a user