Skip to content

Commit

Permalink
[0.5.x] Handle missing onError callback (#102)
Browse files Browse the repository at this point in the history
* Unify submit functions

* Handle missing onError callback
  • Loading branch information
timacdonald authored Oct 22, 2024
1 parent dfad1ec commit f01ef8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
22 changes: 7 additions & 15 deletions packages/react-inertia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,18 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
return form
},
submit(submitMethod: RequestMethod | Config = {}, submitUrl?: string, submitOptions?: any): void {
const isPatchedCall = typeof submitMethod !== 'string'

submitOptions = isPatchedCall
? submitMethod
: submitOptions

submitUrl = isPatchedCall
? resolveUrl(url)
: submitUrl!

submitMethod = isPatchedCall
? resolveMethod(method)
: submitMethod as RequestMethod
if (typeof submitMethod !== 'string') {
submitOptions = submitMethod
submitUrl = resolveUrl(url)
submitMethod = resolveMethod(method)
}

inertiaSubmit(submitMethod, submitUrl, {
inertiaSubmit(submitMethod, submitUrl!, {
...submitOptions,
onError: (errors: SimpleValidationErrors): any => {
precognitiveForm.validator().setErrors(errors)

if (submitOptions.onError) {
if (submitOptions?.onError) {
return submitOptions.onError(errors)
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-inertia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
onError: (errors: SimpleValidationErrors): void => {
precognitiveForm.validator().setErrors(errors)

if (submitOptions!.onError) {
return submitOptions!.onError(errors)
if (submitOptions?.onError) {
return submitOptions.onError(errors)
}
},
})
Expand Down

0 comments on commit f01ef8a

Please sign in to comment.