import { AppBreadcrumb, AppContent, BackHistoryButton } from "@repo/rdx-ui/components"; import { useNavigate } from "react-router-dom"; import { FormCommitButtonGroup, UnsavedChangesProvider, useUrlParamId } from "@erp/core/hooks"; import { showErrorToast, showSuccessToast } from "@repo/shadcn-ui/lib/utils"; import { useState } from "react"; import { FieldErrors } from "react-hook-form"; import { CustomerEditForm, CustomerEditorSkeleton, ErrorAlert, NotFoundCard, } from "../../components"; import { useCustomerQuery, useUpdateCustomerMutation } from "../../hooks"; import { useTranslation } from "../../i18n"; import { CreateCustomerFormData } from "../../schemas"; export const CustomerUpdate = () => { const customerId = useUrlParamId(); const { t } = useTranslation(); const navigate = useNavigate(); const [isDirty, setIsDirty] = useState(false); // 1) Estado de carga del cliente (query) const { data: customerData, isLoading: isLoadingCustomer, isError: isLoadError, error: loadError, } = useCustomerQuery(customerId, { enabled: !!customerId }); // 2) Estado de actualización (mutación) const { mutate, isPending: isUpdating, isError: isUpdateError, error: updateError, } = useUpdateCustomerMutation(); // 3) Submit con navegación condicionada por éxito const handleSubmit = (formData: CreateCustomerFormData) => { mutate( { id: customerId!, data: formData }, { onSuccess(data) { setIsDirty(false); showSuccessToast(t("pages.update.successTitle"), t("pages.update.successMsg")); // El timeout es para que a React le dé tiempo a procesar // el cambio de estado de isDirty / setIsDirty. setTimeout(() => { navigate("/customers/list", { state: { customerId: data.id, isNew: true }, replace: true, }); }, 0); }, onError(error) { showErrorToast(t("pages.update.errorTitle"), error.message); }, } ); }; const handleError = (errors: FieldErrors) => { console.error("Errores en el formulario:", errors); // Aquí puedes manejar los errores, por ejemplo, mostrar un mensaje al usuario }; if (isLoadingCustomer) { return ; } if (isLoadError) { return ( <>
); } if (!customerData) return ( <> ); return ( <>

{t("pages.update.title")}

{t("pages.update.description")}

{/* Alerta de error de actualización (si ha fallado el último intento) */} {isUpdateError && ( )}
); };