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-19 09:29:49 +00:00
|
|
|
defaultValues: CustomerData; // ✅ ya no recibe DTO
|
2025-09-16 17:29:37 +00:00
|
|
|
isPending?: boolean;
|
2025-09-19 09:29:49 +00:00
|
|
|
onSubmit: (data: CustomerData) => void;
|
|
|
|
|
onError: (errors: FieldErrors<CustomerUpdateData>) => void;
|
|
|
|
|
errorMessage?: string; // ✅ prop nueva para mostrar error global
|
2025-09-16 17:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-19 09:29:49 +00:00
|
|
|
export const CustomerEditForm = ({
|
|
|
|
|
formId,
|
|
|
|
|
defaultValues,
|
|
|
|
|
onSubmit,
|
|
|
|
|
isPending,
|
|
|
|
|
errorMessage,
|
|
|
|
|
}: CustomerFormProps) => {
|
2025-09-16 17:29:37 +00:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2025-09-17 17:37:41 +00:00
|
|
|
const form = useForm<CustomerUpdateData>({
|
|
|
|
|
resolver: zodResolver(CustomerUpdateSchema),
|
2025-09-19 09:29:49 +00:00
|
|
|
defaultValues,
|
2025-09-16 17:29:37 +00:00
|
|
|
disabled: isPending,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useUnsavedChangesNotifier({
|
|
|
|
|
isDirty: form.formState.isDirty,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Form {...form}>
|
2025-09-18 09:46:25 +00:00
|
|
|
<FormDebug form={form} />
|
2025-09-19 09:29:49 +00:00
|
|
|
<form id={formId} onSubmit={form.handleSubmit(onSubmit, oError)}>
|
2025-09-16 17:29:37 +00:00
|
|
|
<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>
|
|
|
|
|
);
|
|
|
|
|
};
|