119 lines
5.0 KiB
TypeScript
119 lines
5.0 KiB
TypeScript
// WF-P8 S07 "Boards-Übersicht" route: the board table. Each row links to S04
|
|
// (admin.pipelines.$pipelineId) for the per-board configuration. Both routes
|
|
// share handlePipelineAdminAction so the write path stays single-sourced.
|
|
|
|
import { useActionData, useLoaderData, useNavigation, useSubmit, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { buildPipelineAdminVM, type PipelineAdminVM } from "@hivegrid/view-models";
|
|
import {
|
|
ensureDefaultSalesPipeline,
|
|
listPipelinesForAdmin,
|
|
loadPipelineAdminCounts,
|
|
} from "~/utils/integrations/projects/projectService.server";
|
|
import { handlePipelineAdminAction } from "~/utils/integrations/projects/pipelineAdminAction.server";
|
|
import { loadProjectContext, requireProjectPermission } from "~/utils/integrations/projects/projectAccess.server";
|
|
import {
|
|
PROJECTS_MODULE_KEY,
|
|
projectBoardSettingsPath,
|
|
projectLostReasonsAdminPath,
|
|
projectsBoardsPath,
|
|
} from "~/utils/integrations/projects/projectRoutePaths";
|
|
import { PipelineAdminPage } from "~/components/screens/projects/PipelineAdminPage";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { v2MetaFunction } from "~/utils/compat/v2MetaFunction";
|
|
import { useSaveToast } from "~/hooks/use-save-toast";
|
|
|
|
type LoaderData = {
|
|
vm: PipelineAdminVM;
|
|
title: string;
|
|
breadcrumbAncestors: { label: string; href: string }[];
|
|
lostReasonsHref: string;
|
|
tenantSlug: 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);
|
|
await ensureDefaultSalesPipeline({ tenantId: ctx.tenantId, userId: ctx.userId });
|
|
const [pipelines, counts] = await Promise.all([
|
|
listPipelinesForAdmin({ tenantId: ctx.tenantId }),
|
|
loadPipelineAdminCounts({ tenantId: ctx.tenantId }),
|
|
]);
|
|
const vm = buildPipelineAdminVM({
|
|
canAdmin: true,
|
|
pipelines: pipelines.map((pipeline) => ({
|
|
id: pipeline.id,
|
|
name: pipeline.name,
|
|
typHint: pipeline.typHint,
|
|
isDefault: pipeline.isDefault,
|
|
nachfolgePipelineId: pipeline.nachfolgePipelineId,
|
|
projectCount: counts.byPipeline.get(pipeline.id) ?? 0,
|
|
updatedAtIso: pipeline.updatedAt.toISOString(),
|
|
stages: pipeline.stages.map((stage) => ({
|
|
id: stage.id,
|
|
name: stage.name,
|
|
stageKind: stage.stageKind,
|
|
winProbability: stage.winProbability,
|
|
color: stage.color,
|
|
position: stage.position,
|
|
projectCount: counts.byStage.get(stage.id) ?? 0,
|
|
})),
|
|
accessRules: pipeline.accessRules.map((rule) => ({ id: rule.id, principalType: rule.principalType, principalId: rule.principalId, kannSehen: rule.kannSehen, kannBearbeiten: rule.kannBearbeiten })),
|
|
fieldConfigs: pipeline.fieldConfigs.map((config) => ({
|
|
id: config.id,
|
|
stageId: config.stageId,
|
|
fieldKey: config.fieldKey,
|
|
sichtbar: config.sichtbar,
|
|
pflicht: config.pflicht,
|
|
reihenfolge: config.reihenfolge,
|
|
visibilities: config.visibilities.map((visibility) => ({ id: visibility.id, principalType: visibility.principalType, principalId: visibility.principalId, kannSehen: visibility.kannSehen, kannBearbeiten: visibility.kannBearbeiten })),
|
|
})),
|
|
})),
|
|
});
|
|
const tenantSlug = params.tenant ?? "";
|
|
return Response.json({
|
|
vm,
|
|
title: `${t("projects.admin.title")} | ${process.env.APP_NAME}`,
|
|
breadcrumbAncestors: [{ label: t("projects.boards.title"), href: projectsBoardsPath(tenantSlug) }],
|
|
lostReasonsHref: projectLostReasonsAdminPath(tenantSlug),
|
|
tenantSlug,
|
|
} satisfies LoaderData);
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) {
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.admin");
|
|
return handlePipelineAdminAction({ tenantId: ctx.tenantId, userId: ctx.userId, formData: await request.formData() });
|
|
}
|
|
|
|
export default function PipelinesAdminRoute() {
|
|
const data = useLoaderData<LoaderData>();
|
|
const submit = useSubmit();
|
|
const navigation = useNavigation();
|
|
const actionData = useActionData<{ error?: string; ok?: boolean; intent?: string; messageKey?: string }>();
|
|
useSaveToast(actionData);
|
|
const vmData = data.vm.status === "ready" ? data.vm.data : null;
|
|
|
|
if (!vmData) return null;
|
|
|
|
return (
|
|
<PipelineAdminPage
|
|
data={vmData}
|
|
submitting={navigation.state !== "idle"}
|
|
errorKey={actionData?.error ?? null}
|
|
lostReasonsHref={data.lostReasonsHref}
|
|
settingsHref={(pipelineId) => projectBoardSettingsPath(data.tenantSlug, pipelineId)}
|
|
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 };
|