import type { FormError } from '@nuxt/ui/dist/runtime/types' export interface UseSubmitOptions { onSuccess?: (result: any) => any onError?: (error: Error) => any } export function useSubmit( fetchable: () => Promise, options: UseSubmitOptions = {}, ) { const validationErrors = ref([]) const error = ref(null) const inProgress = ref(false) const succeeded = ref(null) async function submit() { validationErrors.value = [] error.value = null inProgress.value = true succeeded.value = null try { const data = await fetchable() succeeded.value = true options?.onSuccess?.(data) return data } catch (e: any) { error.value = e succeeded.value = false options?.onError?.(e) if (e.data?.errors) { const errorsArray: FormError[] = [] for (const path in e.data.errors) { const messages = e.data.errors[path] messages.forEach((message: string) => { errorsArray.push({ path, message }) }) } validationErrors.value = errorsArray } else { validationErrors.value = [] } if (e.response?.status !== 422) { throw e } } finally { inProgress.value = false } } return { submit, inProgress, succeeded, validationErrors, error, } }