2025-09-16 17:29:37 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
2025-09-17 17:37:41 +00:00
|
|
|
import { FieldErrors, useForm } from "react-hook-form";
|
2025-09-16 17:29:37 +00:00
|
|
|
|
2025-09-18 09:46:25 +00:00
|
|
|
import { Form } from "@repo/shadcn-ui/components";
|
2025-09-16 17:29:37 +00:00
|
|
|
|
|
|
|
|
import { useUnsavedChangesNotifier } from "@erp/core/hooks";
|
2025-09-18 09:46:25 +00:00
|
|
|
import { FormDebug } from "../../components/form-debug";
|
2025-09-16 17:29:37 +00:00
|
|
|
import { useTranslation } from "../../i18n";
|
2025-09-17 17:37:41 +00:00
|
|
|
import { CustomerData, CustomerUpdateData, CustomerUpdateSchema } from "../../schemas";
|
2025-09-18 09:46:25 +00:00
|
|
|
import { CustomerAdditionalConfigFields } from "./customer-additional-config-fields";
|
|
|
|
|
import { CustomerAddressFields } from "./customer-address-fields";
|
|
|
|
|
import { CustomerBasicInfoFields } from "./customer-basic-info-fields";
|
|
|
|
|
import { CustomerContactFields } from "./customer-contact-fields";
|
2025-09-16 17:29:37 +00:00
|
|
|
|
|
|
|
|
interface CustomerFormProps {
|
|
|
|
|
formId: string;
|
2025-09-17 17:37:41 +00:00
|
|
|
data?: CustomerData;
|
2025-09-16 17:29:37 +00:00
|
|
|
isPending?: boolean;
|
|
|
|
|
/**
|
|
|
|
|
* Callback function to handle form submission.
|
|
|
|
|
* @param data - The customer data submitted by the form.
|
|
|
|
|
*/
|
2025-09-17 17:37:41 +00:00
|
|
|
onSubmit?: (data: CustomerUpdateData) => void;
|
2025-09-16 17:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CustomerEditForm = ({ formId, data, onSubmit, isPending }: CustomerFormProps) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2025-09-17 17:37:41 +00:00
|
|
|
const form = useForm<CustomerUpdateData>({
|
|
|
|
|
resolver: zodResolver(CustomerUpdateSchema),
|
2025-09-16 17:29:37 +00:00
|
|
|
defaultValues: data,
|
|
|
|
|
disabled: isPending,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useUnsavedChangesNotifier({
|
|
|
|
|
isDirty: form.formState.isDirty,
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-17 17:37:41 +00:00
|
|
|
const handleSubmit = (data: CustomerUpdateData) => {
|
2025-09-16 17:29:37 +00:00
|
|
|
console.log("Datos del formulario:", data);
|
|
|
|
|
onSubmit?.(data);
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-17 17:37:41 +00:00
|
|
|
const handleError = (errors: FieldErrors<CustomerUpdateData>) => {
|
2025-09-16 17:29:37 +00:00
|
|
|
console.error("Errores en el formulario:", errors);
|
|
|
|
|
// Aquí puedes manejar los errores, por ejemplo, mostrar un mensaje al usuario
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
|
form.reset(data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Form {...form}>
|
2025-09-18 09:46:25 +00:00
|
|
|
<FormDebug form={form} />
|
2025-09-16 17:29:37 +00:00
|
|
|
<form id={formId} onSubmit={form.handleSubmit(handleSubmit, handleError)}>
|
|
|
|
|
<div className='w-full grid grid-cols-1 space-y-8 space-x-8 xl:grid-cols-2'>
|
2025-09-18 09:46:25 +00:00
|
|
|
<CustomerBasicInfoFields control={form.control} />
|
|
|
|
|
<CustomerAddressFields control={form.control} />
|
|
|
|
|
<CustomerContactFields control={form.control} />
|
|
|
|
|
<CustomerAdditionalConfigFields control={form.control} />
|
2025-09-16 17:29:37 +00:00
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
);
|
|
|
|
|
};
|