From f01ef8ad4af738aafd8939909c48602b8091cf8e Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Wed, 23 Oct 2024 01:00:02 +1100 Subject: [PATCH] [0.5.x] Handle missing `onError` callback (#102) * Unify submit functions * Handle missing onError callback --- packages/react-inertia/src/index.ts | 22 +++++++--------------- packages/vue-inertia/src/index.ts | 4 ++-- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/packages/react-inertia/src/index.ts b/packages/react-inertia/src/index.ts index a454071..d83258e 100644 --- a/packages/react-inertia/src/index.ts +++ b/packages/react-inertia/src/index.ts @@ -160,26 +160,18 @@ export const useForm = >(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) } }, diff --git a/packages/vue-inertia/src/index.ts b/packages/vue-inertia/src/index.ts index f6d201f..5403bee 100644 --- a/packages/vue-inertia/src/index.ts +++ b/packages/vue-inertia/src/index.ts @@ -178,8 +178,8 @@ export const useForm = >(method: RequestMet onError: (errors: SimpleValidationErrors): void => { precognitiveForm.validator().setErrors(errors) - if (submitOptions!.onError) { - return submitOptions!.onError(errors) + if (submitOptions?.onError) { + return submitOptions.onError(errors) } }, })