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 => {
|
||||
let result = false
|
||||
setState((s) => {
|
||||
const nextErrors: BookingErrors = {}
|
||||
if (!s.contact.name.trim()) nextErrors.name = 'Name is required.'
|
||||
if (!isValidEmail(s.contact.email)) nextErrors.email = 'A valid email is required.'
|
||||
if (!s.contact.phone.trim()) nextErrors.phone = 'Phone is required.'
|
||||
setErrors(nextErrors)
|
||||
if (Object.keys(nextErrors).length > 0) {
|
||||
result = false
|
||||
return s
|
||||
}
|
||||
result = true
|
||||
return { ...s, step: 'confirmed' }
|
||||
})
|
||||
return result
|
||||
}, [])
|
||||
const nextErrors: BookingErrors = {}
|
||||
if (!state.contact.name.trim()) nextErrors.name = 'Name is required.'
|
||||
if (!isValidEmail(state.contact.email)) nextErrors.email = 'A valid email is required.'
|
||||
if (!state.contact.phone.trim()) nextErrors.phone = 'Phone is required.'
|
||||
setErrors(nextErrors)
|
||||
if (Object.keys(nextErrors).length > 0) {
|
||||
return false
|
||||
}
|
||||
setState((s) => ({ ...s, step: 'confirmed' }))
|
||||
return true
|
||||
}, [state.contact])
|
||||
|
||||
const reset = useCallback(() => {
|
||||
setState(initialState(initialServiceId))
|
||||
|
||||
Reference in New Issue
Block a user