import { AppBreadcrumb, AppContent, BackHistoryButton, ButtonGroup } from "@repo/rdx-ui/components"; import { Button } from "@repo/shadcn-ui/components"; import { useNavigate } from "react-router-dom"; import { useUrlParamId } from "@erp/core/hooks"; import { useCustomerQuery, useUpdateCustomerMutation } from "../../hooks"; import { useTranslation } from "../../i18n"; import { CustomerEditForm } from "./customer-edit-form"; export const CustomerUpdate = () => { const { t } = useTranslation(); const customerId = useUrlParamId(); const navigate = useNavigate(); // 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 { mutateAsync: updateAsync, isPending: isUpdating, isError: isUpdateError, error: updateError, } = useUpdateCustomerMutation(customerId || ""); // 3) Submit con navegación condicionada por éxito const handleSubmit = async (formData: any) => { try { await updateAsync(formData); // solo navegamos si no lanza // toast?.({ title: t('pages.update.successTitle'), description: t('pages.update.successMsg') }); navigate("/customers/list"); } catch (e) { // toast?.({ variant: 'destructive', title: t('pages.update.errorTitle'), description: (e as Error).message }); // No navegamos en caso de error } }; if (isLoadingCustomer) { return ( <>
{/* Skeleton simple para el formulario */}
); } if (isLoadError) { return ( <>

{t("pages.update.loadErrorTitle", "No se pudo cargar el cliente")}

{(loadError as Error)?.message ?? t("pages.update.loadErrorMsg", "Inténtalo de nuevo más tarde.")}

); } if (!customerData) { return ( <>

{t("pages.update.notFoundTitle", "Cliente no encontrado")}

{t("pages.update.notFoundMsg", "Revisa el identificador o vuelve al listado.")}

); } return ( <>

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

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

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

{t("pages.update.errorTitle", "No se pudo guardar los cambios")}

{(updateError as Error)?.message ?? t("pages.update.errorMsg", "Revisa los datos e inténtalo de nuevo.")}

)}
{/* Importante: proveemos un formId para que el botón del header pueda hacer submit */}
); };