Uecko_ERP/modules/customers/src/web/update/ui/pages/customer-update-page.tsx

118 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-03-16 17:45:45 +00:00
import { ErrorAlert, NotFoundCard, PageHeader } from "@erp/core/components";
2026-04-03 22:06:54 +00:00
import { UnsavedChangesProvider, UpdateCommitButtonGroup } from "@erp/core/hooks";
2026-03-16 17:45:45 +00:00
import { AppContent, AppHeader, BackHistoryButton } from "@repo/rdx-ui/components";
2026-04-03 22:06:54 +00:00
import { FormProvider } from "react-hook-form";
2026-03-16 17:45:45 +00:00
import { useTranslation } from "../../../i18n";
2026-04-03 22:06:54 +00:00
import { useCustomerUpdatePageController } from "../../controllers";
2026-03-16 17:45:45 +00:00
import { CustomerEditorSkeleton } from "../components";
2026-04-04 16:58:32 +00:00
import { CustomerUpdateEditorForm } from "../editor";
2026-03-16 17:45:45 +00:00
export const CustomerUpdatePage = () => {
const { t } = useTranslation();
2026-04-03 22:06:54 +00:00
const { updateCtrl } = useCustomerUpdatePageController();
2026-03-16 17:45:45 +00:00
const {
form,
formId,
onSubmit,
resetForm,
customerData,
isLoading,
isLoadError,
loadError,
isUpdating,
isUpdateError,
updateError,
2026-04-03 22:06:54 +00:00
} = updateCtrl;
2026-03-16 17:45:45 +00:00
2026-04-03 22:06:54 +00:00
const isDirty = form.formState.isDirty;
2026-03-16 17:45:45 +00:00
if (isLoading) {
return <CustomerEditorSkeleton />;
}
if (isLoadError) {
return (
<>
<AppContent>
<ErrorAlert
message={
(loadError as Error)?.message ??
t("pages.update.loadErrorMsg", "Inténtalo de nuevo más tarde.")
}
title={t("pages.update.loadErrorTitle", "No se pudo cargar el cliente")}
/>
<div className="flex items-center justify-end">
<BackHistoryButton />
</div>
</AppContent>
</>
);
}
if (!customerData)
return (
<>
<AppContent>
<NotFoundCard
message={t("pages.update.notFoundMsg", "Revisa el identificador o vuelve al listado.")}
title={t("pages.update.notFoundTitle", "Cliente no encontrado")}
/>
</AppContent>
</>
);
return (
2026-04-03 22:06:54 +00:00
<UnsavedChangesProvider isDirty={isDirty}>
2026-03-16 17:45:45 +00:00
<AppHeader>
<PageHeader
backIcon
description={t("pages.update.description")}
rightSlot={
<UpdateCommitButtonGroup
cancel={{
formId,
to: "/customers/list",
disabled: isUpdating,
}}
disabled={isUpdating}
isLoading={isUpdating}
onReset={resetForm}
submit={{
formId,
disabled: isUpdating,
}}
/>
}
title={t("pages.update.title")}
/>
</AppHeader>
<AppContent>
{/* Alerta de error de actualización (si ha fallado el último intento) */}
{isUpdateError && (
<ErrorAlert
message={
(updateError as Error)?.message ??
t("pages.update.errorMsg", "Revisa los datos e inténtalo de nuevo.")
}
title={t("pages.update.errorTitle", "No se pudo guardar los cambios")}
/>
)}
<FormProvider {...form}>
2026-04-04 16:58:32 +00:00
<CustomerUpdateEditorForm
2026-03-18 16:38:40 +00:00
className="bg-white rounded-xl border shadow-xl max-w-7xl mx-auto mt-6 " // para que el botón del header pueda hacer submit
2026-03-16 17:45:45 +00:00
formId={formId}
onSubmit={onSubmit}
/>
</FormProvider>
</AppContent>
</UnsavedChangesProvider>
);
};