122 lines
5.2 KiB
TypeScript
122 lines
5.2 KiB
TypeScript
import { useActionData, useLoaderData, useNavigation, useSubmit, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { buildLostReasonAdminVM, type LostReasonAdminVM } from "@hivegrid/view-models";
|
|
import { archiveLostReason, listLostReasonsForAdmin, reactivateLostReason, upsertLostReason } from "~/utils/integrations/projects/projectService.server";
|
|
import { loadProjectContext, projectActionErrorResponse, requireProjectPermission } from "~/utils/integrations/projects/projectAccess.server";
|
|
import { PROJECTS_MODULE_KEY, projectsBoardsPath } from "~/utils/integrations/projects/projectRoutePaths";
|
|
import { LostReasonAdminPage } from "~/components/screens/projects/LostReasonAdminPage";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
|
|
import { useSaveToast } from "~/hooks/use-save-toast";
|
|
|
|
type LoaderData = {
|
|
vm: LostReasonAdminVM;
|
|
title: string;
|
|
breadcrumbAncestors: { label: string; href: string }[];
|
|
};
|
|
|
|
export const meta: v2MetaFunction<LoaderData> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.admin");
|
|
const { t } = await getTranslations(request);
|
|
const { rows, insights } = await listLostReasonsForAdmin({ tenantId: ctx.tenantId });
|
|
const vm = buildLostReasonAdminVM({
|
|
canAdmin: true,
|
|
insights,
|
|
reasons: rows.map((reason) => ({
|
|
id: reason.id,
|
|
name: reason.name,
|
|
beschreibung: reason.beschreibung,
|
|
kategorie: reason.kategorie,
|
|
position: reason.position,
|
|
isDemoData: reason.isDemoData,
|
|
istVorlage: reason.istVorlage,
|
|
archivedAt: reason.archivedAt?.toISOString() ?? null,
|
|
projectCount: reason.projectCount,
|
|
lastUsedIso: reason.lastUsedAt?.toISOString() ?? null,
|
|
})),
|
|
});
|
|
return Response.json({
|
|
vm,
|
|
title: `${t("projects.lostReasonsAdmin.title")} | ${process.env.APP_NAME}`,
|
|
// Shell breadcrumb: tenant > Boards > Verlustgründe (Boards links to the boards page).
|
|
breadcrumbAncestors: [{ label: t("projects.boards.nav"), href: projectsBoardsPath(params.tenant ?? "") }],
|
|
} satisfies LoaderData);
|
|
}
|
|
|
|
function formString(formData: FormData, name: string): string | null {
|
|
const value = formData.get(name);
|
|
return typeof value === "string" ? value : null;
|
|
}
|
|
|
|
function formNumber(formData: FormData, name: string): number | undefined {
|
|
const value = formString(formData, name);
|
|
if (!value) return undefined;
|
|
const parsed = Number(value);
|
|
return Number.isFinite(parsed) ? parsed : undefined;
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) {
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.admin");
|
|
const formData = await request.formData();
|
|
const intent = formString(formData, "_action");
|
|
try {
|
|
if (intent === "upsertLostReason") {
|
|
const reason = await upsertLostReason({
|
|
tenantId: ctx.tenantId,
|
|
reasonId: formString(formData, "reasonId"),
|
|
name: formString(formData, "name") ?? "",
|
|
beschreibung: formString(formData, "beschreibung"),
|
|
kategorie: formString(formData, "kategorie"),
|
|
position: formNumber(formData, "position"),
|
|
});
|
|
return Response.json({
|
|
ok: true,
|
|
intent,
|
|
messageKey: formString(formData, "reasonId") ? "projects.toast.lostReasonUpdated" : "projects.toast.lostReasonCreated",
|
|
reason,
|
|
});
|
|
}
|
|
if (intent === "archiveLostReason") {
|
|
const reason = await archiveLostReason({ tenantId: ctx.tenantId, reasonId: formString(formData, "reasonId") ?? "" });
|
|
return Response.json({ ok: true, intent, messageKey: "projects.toast.lostReasonArchived", reason });
|
|
}
|
|
if (intent === "reactivateLostReason") {
|
|
const reason = await reactivateLostReason({ tenantId: ctx.tenantId, reasonId: formString(formData, "reasonId") ?? "" });
|
|
return Response.json({ ok: true, intent, messageKey: "projects.toast.lostReasonReactivated", reason });
|
|
}
|
|
return Response.json({ intent, error: "projects.error.unsupportedAction" }, { status: 400 });
|
|
} catch (error) {
|
|
return projectActionErrorResponse(error);
|
|
}
|
|
}
|
|
|
|
export default function LostReasonsAdminRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const submit = useSubmit();
|
|
const navigation = useNavigation();
|
|
const actionData = useActionData<{ intent?: string; ok?: boolean; error?: string; messageKey?: string }>();
|
|
useSaveToast(actionData);
|
|
const vmData = data.vm.status === "ready" ? data.vm.data : null;
|
|
|
|
if (!vmData) return null;
|
|
|
|
return (
|
|
<LostReasonAdminPage
|
|
data={vmData}
|
|
submitting={navigation.state !== "idle"}
|
|
errorKey={actionData?.error ?? null}
|
|
actions={{
|
|
onSubmit: (intent, fields) => {
|
|
const fd = new FormData();
|
|
fd.set("_action", intent);
|
|
for (const [key, value] of Object.entries(fields)) fd.set(key, value);
|
|
submit(fd, { method: "post" });
|
|
},
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export const handle = { moduleKey: PROJECTS_MODULE_KEY };
|