145 lines
6.8 KiB
TypeScript
145 lines
6.8 KiB
TypeScript
import { Form, Link, useLoaderData, redirect, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { createProject, listProjects, loadProjectBoard, moveProjectStage, undoLastProjectStageChange } from "~/utils/integrations/projects/projectService.server";
|
|
import { loadProjectContext, projectActionErrorResponse, requireProjectPermission } from "~/utils/integrations/projects/projectAccess.server";
|
|
import { projectDetailPath, projectPlantafelPath, projectsListPath } from "~/utils/integrations/projects/projectRoutePaths";
|
|
import type { ProjectBoardVM, ProjektRow } from "~/utils/integrations/projects/projectService.server";
|
|
|
|
type LoaderData = { tenantSlug: string; projects: ProjektRow[]; board: ProjectBoardVM; includeDemoData: boolean };
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.read_own");
|
|
const url = new URL(request.url);
|
|
const includeDemoData = url.searchParams.get("demo") === "show";
|
|
const [projects, board] = await Promise.all([
|
|
listProjects({ tenantId: ctx.tenantId, ctx, includeDemoData }),
|
|
loadProjectBoard({
|
|
tenantId: ctx.tenantId,
|
|
ctx,
|
|
pipelineId: url.searchParams.get("pipelineId"),
|
|
onlyMine: url.searchParams.get("onlyMine") === "1",
|
|
includeDemoData,
|
|
}),
|
|
]);
|
|
return Response.json({ tenantSlug: params.tenant!, projects, board, includeDemoData } satisfies LoaderData);
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) {
|
|
try {
|
|
const form = await request.formData();
|
|
const intent = form.get("intent")?.toString() ?? "createProject";
|
|
if (intent === "moveStage") {
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.change_stage");
|
|
await moveProjectStage({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
projectId: form.get("projectId")?.toString() ?? "",
|
|
targetStageId: form.get("targetStageId")?.toString() ?? "",
|
|
lostReasonId: form.get("lostReasonId")?.toString() || null,
|
|
});
|
|
return Response.json({ ok: true });
|
|
}
|
|
if (intent === "undoStage") {
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.change_stage");
|
|
await undoLastProjectStageChange({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
projectId: form.get("projectId")?.toString() ?? "",
|
|
});
|
|
return Response.json({ ok: true });
|
|
}
|
|
const ctx = requireProjectPermission(await loadProjectContext({ request, params, requireModule: true }), "projects.write");
|
|
const project = await createProject({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
projektname: form.get("projektname")?.toString() ?? "",
|
|
beschreibung: form.get("beschreibung")?.toString() ?? null,
|
|
potenziellerAuftragswert: form.get("potenziellerAuftragswert")?.toString() || null,
|
|
zustaendigePersonId: ctx.userId,
|
|
});
|
|
return redirect(projectDetailPath(params.tenant!, project.id));
|
|
} catch (error) {
|
|
return projectActionErrorResponse(error);
|
|
}
|
|
}
|
|
|
|
export default function ProjectsIndexRoute() {
|
|
const { t } = useTranslation();
|
|
const data = useLoaderData<LoaderData>();
|
|
|
|
return (
|
|
<main className="mx-auto flex w-full max-w-6xl flex-col gap-6 p-6">
|
|
<header className="flex flex-col gap-1">
|
|
<h1 className="text-2xl font-semibold">{t("projects.list.title")}</h1>
|
|
<p className="text-sm text-muted-foreground">{t("projects.list.description")}</p>
|
|
<Link className="text-sm text-primary underline-offset-2 hover:underline" to={projectsListPath(data.tenantSlug)}>
|
|
{t("projects.actions.openList")}
|
|
</Link>
|
|
<Link className="text-sm text-primary underline-offset-2 hover:underline" to={projectPlantafelPath(data.tenantSlug)}>
|
|
{t("projects.actions.openPlantafel")}
|
|
</Link>
|
|
<Link className="text-sm text-primary underline-offset-2 hover:underline" to={data.includeDemoData ? "?" : "?demo=show"}>
|
|
{t(data.includeDemoData ? "onboarding.demoData.hideInLists" : "onboarding.demoData.showInLists")}
|
|
</Link>
|
|
</header>
|
|
|
|
<section className="grid gap-3 md:grid-cols-3">
|
|
{data.board.columns.map((column) => (
|
|
<article key={column.stage.id} className="rounded-md border bg-background p-4">
|
|
<h2 className="font-medium">{column.stage.name}</h2>
|
|
<p className="text-sm text-muted-foreground">
|
|
{column.projectCount} · {column.aggregateVisible ? column.aggregateValue : "—"}
|
|
</p>
|
|
</article>
|
|
))}
|
|
</section>
|
|
|
|
<section className="rounded-md border bg-background p-4">
|
|
<Form method="post" className="grid gap-3 md:grid-cols-[1fr_1fr_auto]">
|
|
<input type="hidden" name="intent" value="createProject" />
|
|
<label className="flex flex-col gap-1 text-sm">
|
|
{t("projects.fields.name")}
|
|
<input name="projektname" required className="rounded-md border px-3 py-2" />
|
|
</label>
|
|
<label className="flex flex-col gap-1 text-sm">
|
|
{t("projects.fields.value")}
|
|
<input name="potenziellerAuftragswert" inputMode="decimal" className="rounded-md border px-3 py-2" />
|
|
</label>
|
|
<button type="submit" className="self-end rounded-md bg-primary px-4 py-2 text-primary-foreground">
|
|
{t("projects.actions.create")}
|
|
</button>
|
|
</Form>
|
|
</section>
|
|
|
|
<section className="overflow-hidden rounded-md border bg-background">
|
|
{data.projects.length === 0 ? (
|
|
<p className="p-4 text-sm text-muted-foreground">{t("projects.list.empty")}</p>
|
|
) : (
|
|
<table className="w-full text-sm">
|
|
<thead className="bg-muted text-left">
|
|
<tr>
|
|
<th className="px-4 py-2">{t("projects.fields.number")}</th>
|
|
<th className="px-4 py-2">{t("projects.fields.name")}</th>
|
|
<th className="px-4 py-2">{t("projects.fields.priority")}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{data.projects.map((project) => (
|
|
<tr key={project.id} className="border-t">
|
|
<td className="px-4 py-2">{project.projektNummer}</td>
|
|
<td className="px-4 py-2">
|
|
<Link className="text-primary underline-offset-2 hover:underline" to={projectDetailPath(data.tenantSlug, project.id)}>
|
|
{project.projektname}
|
|
</Link>
|
|
</td>
|
|
<td className="px-4 py-2">{project.prioritaet}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|