{"version":3,"file":"form-EMAaM4Mt.js","sources":["../../../app/helpers/fetch/form.tsx"],"sourcesContent":["import type { FormEvent } from \"react\";\nimport type { Method } from \"@inertiajs/core\";\nimport type { PathHelper, ResponseError } from \"@js-from-routes/client\";\nimport { sentencify } from \"~/helpers/inflect\";\nimport { showFormErrorsAlert } from \"~/helpers/form\";\n\nimport type {\n FormErrors,\n UseFormInput,\n UseFormReturnType,\n} from \"@mantine/form\";\n\ntype FetchPartialForm = Omit<\n UseFormReturnType,\n \"onSubmit\" | \"onReset\"\n>;\n\ntype TransformValues = (values: Values) => any;\n\nexport interface FetchFormOptions<\n Data extends Record & { error?: never },\n Values,\n> extends UseFormInput> {\n action: PathHelper;\n descriptor: string;\n params?: {\n query?: Record;\n [key: string]: any;\n };\n method?: Method;\n failSilently?: boolean;\n transformErrors?: (errors: Record) => FormErrors;\n onSuccess?: (data: Data, form: FetchPartialForm) => void;\n onFailure?: (error: Error, form: FetchPartialForm) => void;\n onError?: (form: FetchPartialForm) => void;\n}\n\ntype FetchFormSubmit = (event?: FormEvent) => void;\n\nexport interface FetchForm<\n Data extends Record & { error?: never; errors?: never },\n Values,\n> extends Omit>, \"onSubmit\"> {\n data: Data | undefined;\n error: Error | undefined;\n processing: boolean;\n submit: FetchFormSubmit;\n}\n\n// TODO: Serialize form data.\nexport const useFetchForm = <\n Data extends Record & { error?: never; errors?: never } = {},\n Values extends Record = Record,\n>(\n options: FetchFormOptions,\n): FetchForm => {\n const {\n action,\n params,\n descriptor,\n method = action.httpMethod,\n failSilently,\n transformValues = deepUnderscoreKeys,\n transformErrors = deepCamelizeKeys,\n onSuccess,\n onFailure,\n onError,\n ...otherOptions\n } = options;\n const form = useForm>({\n ...otherOptions,\n transformValues,\n });\n const [data, setData] = useState();\n const [error, setError] = useState();\n const [processing, setProcessing] = useState(false);\n const submit = form.onSubmit(\n data => {\n setProcessing(true);\n action }>({\n params,\n method,\n data: method === \"delete\" ? undefined : data,\n })\n .then(\n data => {\n setData(data as Data);\n form.reset();\n onSuccess?.(data as Data, form);\n },\n (responseError: ResponseError) => {\n if (responseError.body) {\n const { error, errors } = responseError.body as {\n error?: string;\n errors?: Record;\n };\n if (error) {\n const e = new Error(error);\n setError(e);\n console.error(`Failed to ${descriptor}`, error);\n if (!failSilently) {\n showAlert({\n title: `Failed to ${descriptor}`,\n message: sentencify(error),\n });\n }\n onFailure?.(e, form);\n } else if (errors) {\n const formErrors: FormErrors = transformErrors(errors);\n form.setErrors(formErrors);\n console.warn(`Couldn't ${descriptor}`, {\n errors: formErrors,\n });\n const formWithErrors = { ...form, errors: formErrors };\n if (!failSilently) {\n showFormErrorsAlert(formWithErrors, `Couldn't ${descriptor}`);\n }\n onError?.(formWithErrors);\n }\n } else {\n console.error(\n \"An unknown error response occurred\",\n responseError,\n );\n if (!failSilently) {\n showAlert({\n title: `Failed to ${descriptor}`,\n message: sentencify(responseError.message),\n });\n }\n onFailure?.(responseError, form);\n }\n },\n )\n .finally(() => {\n setProcessing(false);\n });\n },\n errors => {\n const formWithErrors = { ...form, errors };\n onError?.(formWithErrors);\n showFormErrorsAlert(formWithErrors, `Couldn't ${descriptor}`);\n },\n );\n return {\n ...form,\n processing,\n submit,\n data,\n error,\n };\n};\n"],"names":["useFetchForm","options","action","params","descriptor","method","failSilently","transformValues","deepUnderscoreKeys","transformErrors","deepCamelizeKeys","onSuccess","onFailure","onError","otherOptions","form","useForm","data","setData","useState","error","setError","processing","setProcessing","submit","responseError","errors","e","showAlert","sentencify","formErrors","formWithErrors","showFormErrorsAlert"],"mappings":"iKAkDa,MAAAA,EAIXC,GAC4B,CACtB,KAAA,CACJ,OAAAC,EACA,OAAAC,EACA,WAAAC,EACA,OAAAC,EAASH,EAAO,WAChB,aAAAI,EACA,gBAAAC,EAAkBC,EAClB,gBAAAC,EAAkBC,EAClB,UAAAC,EACA,UAAAC,EACA,QAAAC,EACA,GAAGC,CACD,EAAAb,EACEc,EAAOC,EAAyC,CACpD,GAAGF,EACH,gBAAAP,CAAA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