94 lines
4.3 KiB
TypeScript
94 lines
4.3 KiB
TypeScript
// R-P9a — project template admin route (restored; the original route was
|
|
// removed in the seat-billing recut while the backend services stayed).
|
|
// Loader/action delegate to auftragService; the screen is interim UI (AD-28).
|
|
|
|
import { useActionData, useLoaderData, useNavigation, useSubmit, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { buildProjectTemplatesAdminVM } from "@hivegrid/view-models";
|
|
import { listProjectTemplates, upsertProjectTemplate, type ProjectTemplateRow } from "~/utils/integrations/projects/auftragService.server";
|
|
import { listPipelinesForAdmin } from "~/utils/integrations/projects/projectService.server";
|
|
import { loadProjectContext, projectActionErrorResponse, requireProjectPermission } from "~/utils/integrations/projects/projectAccess.server";
|
|
import { projectPipelinesAdminPath } from "~/utils/integrations/projects/projectRoutePaths";
|
|
import { ProjectTemplatesAdminPage } from "~/components/screens/projects/ProjectTemplatesAdminPage";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
|
|
import { useSaveToast } from "~/hooks/use-save-toast";
|
|
|
|
type PipelineOption = { id: string; name: string; stages: { id: string; name: string }[] };
|
|
type LoaderData = { tenantSlug: string; title: string; templates: ProjectTemplateRow[]; pipelines: PipelineOption[] };
|
|
|
|
export const meta: v2MetaFunction<LoaderData> = ({ data }) => [{ title: data?.title }];
|
|
|
|
function formString(formData: FormData, name: string): string | null {
|
|
const value = formData.get(name);
|
|
return typeof value === "string" ? value : null;
|
|
}
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.admin");
|
|
const { t } = await getTranslations(request);
|
|
const [templates, pipelines] = await Promise.all([
|
|
listProjectTemplates({ tenantId: ctx.tenantId }),
|
|
listPipelinesForAdmin({ tenantId: ctx.tenantId }),
|
|
]);
|
|
return Response.json({
|
|
tenantSlug: params.tenant ?? "",
|
|
title: `${t("projects.templatesAdmin.title")} | ${process.env.APP_NAME}`,
|
|
templates,
|
|
pipelines: pipelines.map((pipeline) => ({
|
|
id: pipeline.id,
|
|
name: pipeline.name,
|
|
stages: pipeline.stages.map((stage) => ({ id: stage.id, name: stage.name })),
|
|
})),
|
|
} satisfies LoaderData);
|
|
}
|
|
|
|
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 === "upsertProjectTemplate") {
|
|
const template = await upsertProjectTemplate({
|
|
tenantId: ctx.tenantId,
|
|
templateId: formString(formData, "templateId"),
|
|
name: formString(formData, "name") ?? "",
|
|
defaultPipelineId: formString(formData, "defaultPipelineId") || null,
|
|
defaultStageId: formString(formData, "defaultStageId") || null,
|
|
defaultMembers: [],
|
|
defaultTasks: [],
|
|
customFields: {},
|
|
});
|
|
return Response.json({ ok: true, intent, messageKey: "projects.toast.projectTemplateSaved", templateId: template.id });
|
|
}
|
|
return Response.json({ intent, error: "projects.error.unsupportedAction" }, { status: 400 });
|
|
} catch (error) {
|
|
return projectActionErrorResponse(error);
|
|
}
|
|
}
|
|
|
|
export default function ProjectTemplatesAdminRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<{ intent?: string; ok?: boolean; error?: string; messageKey?: string }>();
|
|
useSaveToast(actionData);
|
|
const navigation = useNavigation();
|
|
const submit = useSubmit();
|
|
const vm = buildProjectTemplatesAdminVM({ templates: data.templates, pipelines: data.pipelines });
|
|
|
|
return (
|
|
<ProjectTemplatesAdminPage
|
|
vm={vm}
|
|
backHref={projectPipelinesAdminPath(data.tenantSlug)}
|
|
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" });
|
|
},
|
|
}}
|
|
/>
|
|
);
|
|
}
|