first commit
This commit is contained in:
30
hooks/useForm.ts
Normal file
30
hooks/useForm.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
"use client";
|
||||
import { useState } from 'react';
|
||||
import { supabase } from '@/lib/supabase/client';
|
||||
|
||||
export const useFormSubmit = (tableName: string) => {
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [success, setSuccess] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const submit = async (data: any) => {
|
||||
setIsSubmitting(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const { error: sbError } = await supabase
|
||||
.from(tableName)
|
||||
.insert([data]);
|
||||
|
||||
if (sbError) throw sbError;
|
||||
|
||||
setSuccess(true);
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Error al enviar los datos');
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return { submit, isSubmitting, success, error };
|
||||
};
|
||||
Reference in New Issue
Block a user