Uecko_ERP/modules/customers/src/web/pages/create/customer-create.tsx

112 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-09-23 08:59:15 +00:00
import { AppBreadcrumb, AppContent } from "@repo/rdx-ui/components";
2025-09-22 17:43:55 +00:00
import { useNavigate } from "react-router-dom";
2025-09-23 08:59:15 +00:00
import { FormCommitButtonGroup, UnsavedChangesProvider } from "@erp/core/hooks";
2025-09-24 10:34:04 +00:00
import { showErrorToast, showSuccessToast } from "@repo/rdx-ui/helpers";
2025-09-23 17:21:16 +00:00
import { FieldErrors, FormProvider } from "react-hook-form";
2025-09-22 17:43:55 +00:00
import { CustomerEditForm, ErrorAlert } from "../../components";
2025-09-23 17:21:16 +00:00
import { useCreateCustomerMutation, useCustomerForm } from "../../hooks";
2025-09-22 17:43:55 +00:00
import { useTranslation } from "../../i18n";
2025-09-23 17:21:16 +00:00
import { CustomerFormData, defaultCustomerFormData } from "../../schemas";
2025-09-22 17:43:55 +00:00
export const CustomerCreate = () => {
const { t } = useTranslation();
const navigate = useNavigate();
2025-09-23 16:38:20 +00:00
// 1) Estado de creación (mutación)
2025-09-22 17:43:55 +00:00
const {
2025-09-23 11:48:19 +00:00
mutate,
2025-09-22 17:43:55 +00:00
isPending: isCreating,
isError: isCreateError,
error: createError,
} = useCreateCustomerMutation();
2025-09-23 17:21:16 +00:00
// 2) Form hook
const form = useCustomerForm({
initialValues: defaultCustomerFormData,
});
// 3) Submit con navegación condicionada por éxito
const handleSubmit = (formData: CustomerFormData) => {
2025-09-23 11:48:19 +00:00
mutate(
{ data: formData },
{
onSuccess(data) {
showSuccessToast(t("pages.create.successTitle"), t("pages.create.successMsg"));
2025-09-23 09:14:29 +00:00
2025-09-24 10:34:04 +00:00
// 🔹 limpiar el form e isDirty pasa a false
2025-09-23 17:21:16 +00:00
form.reset(defaultCustomerFormData);
navigate("/customers/list", {
state: { customerId: data.id, isNew: true },
replace: true,
});
2025-09-23 11:48:19 +00:00
},
onError(error) {
showErrorToast(t("pages.create.errorTitle"), error.message);
},
2025-09-22 17:43:55 +00:00
}
2025-09-23 11:48:19 +00:00
);
2025-09-22 17:43:55 +00:00
};
2025-09-23 17:21:16 +00:00
const handleError = (errors: FieldErrors<CustomerFormData>) => {
2025-09-22 17:43:55 +00:00
console.error("Errores en el formulario:", errors);
// Aquí puedes manejar los errores, por ejemplo, mostrar un mensaje al usuario
};
2025-09-24 11:49:17 +00:00
const handleBack = () => {
navigate(-1);
};
2025-09-22 17:43:55 +00:00
return (
<>
<AppBreadcrumb />
<AppContent>
2025-09-23 17:21:16 +00:00
<UnsavedChangesProvider isDirty={form.formState.isDirty}>
2025-09-24 15:09:37 +00:00
<div className='flex items-center justify-between space-y-6'>
2025-09-23 08:59:15 +00:00
<div className='space-y-2'>
<h2 className='text-2xl font-bold tracking-tight text-balance scroll-m-2'>
{t("pages.create.title")}
</h2>
<p className='text-muted-foreground scroll-m-20 tracking-tight text-balance'>
{t("pages.create.description")}
</p>
</div>
<FormCommitButtonGroup
2025-09-24 11:49:17 +00:00
isLoading={isCreating}
disabled={isCreating}
2025-09-23 08:59:15 +00:00
cancel={{
to: "/customers/list",
2025-09-24 11:49:17 +00:00
disabled: isCreating,
2025-09-22 17:43:55 +00:00
}}
2025-09-23 08:59:15 +00:00
submit={{
formId: "customer-create-form",
disabled: isCreating,
}}
2025-09-24 11:49:17 +00:00
onBack={() => handleBack()}
2025-09-23 08:59:15 +00:00
/>
</div>
{/* Alerta de error de actualización (si ha fallado el último intento) */}
{isCreateError && (
<ErrorAlert
title={t("pages.create.errorTitle", "No se pudo guardar los cambios")}
message={
(createError as Error)?.message ??
t("pages.create.errorMsg", "Revisa los datos e inténtalo de nuevo.")
}
/>
)}
2025-09-22 17:43:55 +00:00
2025-09-24 15:09:37 +00:00
<FormProvider {...form}>
<CustomerEditForm
formId='customer-create-form'
onSubmit={handleSubmit}
onError={handleError}
/>
</FormProvider>
2025-09-23 08:59:15 +00:00
</UnsavedChangesProvider>
2025-09-22 17:43:55 +00:00
</AppContent>
</>
);
};