fix: make useBooking submit() validate synchronously instead of racing setState's updater

This commit is contained in:
belviskhoremk
2026-08-03 17:51:45 +00:00
parent a270c33c73
commit 358b5121f0

View File

@@ -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.'
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) {
result = false
return s
return false
}
result = true
return { ...s, step: 'confirmed' }
})
return result
}, [])
setState((s) => ({ ...s, step: 'confirmed' }))
return true
}, [state.contact])
const reset = useCallback(() => {
setState(initialState(initialServiceId))