147 lines
6.8 KiB
TypeScript
147 lines
6.8 KiB
TypeScript
import { Form, useLoaderData, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import {
|
|
applyOfflineMutation,
|
|
bulkReassignScheduleEntries,
|
|
loadPlantafel,
|
|
updateScheduleEntryStatus,
|
|
upsertScheduleEntry,
|
|
type OfflineSyncOperation,
|
|
type PlantafelVM,
|
|
type ScheduleEntryStatus,
|
|
} from "~/utils/integrations/projects/plantafelAcceptanceService.server";
|
|
import { loadProjectContext, projectActionErrorResponse, requireAnyProjectPermission, requireProjectPermission } from "~/utils/integrations/projects/projectAccess.server";
|
|
|
|
type LoaderData = { plantafel: PlantafelVM };
|
|
|
|
function formString(formData: FormData, name: string): string | null {
|
|
const value = formData.get(name);
|
|
return typeof value === "string" ? value : null;
|
|
}
|
|
|
|
function parseJsonObject(value: string | null): Record<string, unknown> {
|
|
if (!value) return {};
|
|
const parsed = JSON.parse(value) as unknown;
|
|
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? (parsed as Record<string, unknown>) : {};
|
|
}
|
|
|
|
function parseEntryIds(formData: FormData): string[] {
|
|
const raw = formString(formData, "entryIds");
|
|
if (!raw) return [];
|
|
const parsed = JSON.parse(raw) as unknown;
|
|
return Array.isArray(parsed) ? parsed.filter((item): item is string => typeof item === "string") : [];
|
|
}
|
|
|
|
function dateParam(url: URL, name: string, fallback: Date): Date {
|
|
const value = url.searchParams.get(name);
|
|
const parsed = value ? new Date(value) : fallback;
|
|
return Number.isNaN(parsed.getTime()) ? fallback : parsed;
|
|
}
|
|
|
|
export async function loader({ request, params }: LoaderFunctionArgs) {
|
|
const ctx = requireAnyProjectPermission(await loadProjectContext({ request, params, requireModule: true }), ["plantafel.read_all", "plantafel.read_own"]);
|
|
const url = new URL(request.url);
|
|
const now = new Date();
|
|
const from = dateParam(url, "from", new Date(now.getFullYear(), now.getMonth(), now.getDate()));
|
|
const to = dateParam(url, "to", new Date(from.getTime() + 7 * 24 * 60 * 60 * 1000));
|
|
const plantafel = await loadPlantafel({ tenantId: ctx.tenantId, ctx, from, to, assignedUserId: url.searchParams.get("assignedUserId") });
|
|
return Response.json({ plantafel } satisfies LoaderData);
|
|
}
|
|
|
|
export async function action({ request, params }: ActionFunctionArgs) {
|
|
const ctx = await loadProjectContext({ request, params, requireModule: true });
|
|
const formData = await request.formData();
|
|
const intent = formString(formData, "_action");
|
|
try {
|
|
if (intent === "upsertSchedule") {
|
|
requireProjectPermission(ctx, "plantafel.write");
|
|
const result = await upsertScheduleEntry({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
entryId: formString(formData, "entryId"),
|
|
projektId: formString(formData, "projektId") ?? "",
|
|
assignedUserId: formString(formData, "assignedUserId") ?? "",
|
|
title: formString(formData, "title") ?? "",
|
|
startAt: formString(formData, "startAt") ?? "",
|
|
endAt: formString(formData, "endAt") ?? "",
|
|
status: (formString(formData, "status") as ScheduleEntryStatus | null) ?? "planned",
|
|
calendarEventId: formString(formData, "calendarEventId"),
|
|
conflictOverrideReason: formString(formData, "conflictOverrideReason"),
|
|
clientMutationId: formString(formData, "clientMutationId"),
|
|
});
|
|
return Response.json({ ok: true, ...result });
|
|
}
|
|
if (intent === "updateStatus") {
|
|
requireProjectPermission(ctx, "plantafel.write");
|
|
const entry = await updateScheduleEntryStatus({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
entryId: formString(formData, "entryId") ?? "",
|
|
status: (formString(formData, "status") as ScheduleEntryStatus | null) ?? "planned",
|
|
clientMutationId: formString(formData, "clientMutationId"),
|
|
});
|
|
return Response.json({ ok: true, entry });
|
|
}
|
|
if (intent === "bulkReassign") {
|
|
requireProjectPermission(ctx, "plantafel.bulk_reassign");
|
|
const result = await bulkReassignScheduleEntries({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
entryIds: parseEntryIds(formData),
|
|
assignedUserId: formString(formData, "assignedUserId") ?? "",
|
|
conflictOverrideReason: formString(formData, "conflictOverrideReason"),
|
|
});
|
|
return Response.json({ ok: true, ...result });
|
|
}
|
|
if (intent === "offlineSync") {
|
|
requireAnyProjectPermission(ctx, ["plantafel.write", "acceptance.write"]);
|
|
const result = await applyOfflineMutation({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
clientMutationId: formString(formData, "clientMutationId") ?? "",
|
|
operation: formString(formData, "operation") as OfflineSyncOperation,
|
|
payload: parseJsonObject(formString(formData, "payload")),
|
|
});
|
|
return Response.json({ ok: true, ...result });
|
|
}
|
|
return Response.json({ error: "projects.error.unsupportedAction" }, { status: 400 });
|
|
} catch (error) {
|
|
return projectActionErrorResponse(error);
|
|
}
|
|
}
|
|
|
|
export default function PlantafelRoute() {
|
|
const { t } = useTranslation();
|
|
const data = useLoaderData<LoaderData>();
|
|
|
|
return (
|
|
<main className="mx-auto flex w-full max-w-5xl flex-col gap-6 p-6">
|
|
<header>
|
|
<h1 className="text-2xl font-semibold">{t("projects.plantafel.title")}</h1>
|
|
</header>
|
|
<section className="grid gap-3">
|
|
{data.plantafel.entries.map((entry) => (
|
|
<article key={entry.id} className="rounded-md border bg-background p-4">
|
|
<h2 className="font-semibold">{entry.title}</h2>
|
|
<p className="text-sm text-muted-foreground">
|
|
{entry.assignedUserId} · {entry.status}
|
|
</p>
|
|
{entry.conflicts && entry.conflicts.length > 0 ? <p className="text-sm text-destructive">{t("projects.plantafel.conflicts", { count: entry.conflicts.length })}</p> : null}
|
|
</article>
|
|
))}
|
|
</section>
|
|
<Form className="grid gap-3 rounded-md border bg-background p-4" method="post">
|
|
<input type="hidden" name="_action" value="upsertSchedule" />
|
|
<input className="rounded-md border px-3 py-2 text-sm" name="projektId" placeholder={t("projects.fields.projectId")} />
|
|
<input className="rounded-md border px-3 py-2 text-sm" name="assignedUserId" placeholder={t("projects.fields.assignedUserId")} />
|
|
<input className="rounded-md border px-3 py-2 text-sm" name="title" placeholder={t("projects.fields.scheduleTitle")} />
|
|
<input className="rounded-md border px-3 py-2 text-sm" name="startAt" type="datetime-local" />
|
|
<input className="rounded-md border px-3 py-2 text-sm" name="endAt" type="datetime-local" />
|
|
<button className="rounded-md bg-primary px-3 py-2 text-sm text-primary-foreground" type="submit">
|
|
{t("projects.actions.saveScheduleEntry")}
|
|
</button>
|
|
</Form>
|
|
</main>
|
|
);
|
|
}
|