198 lines
8.8 KiB
TypeScript
198 lines
8.8 KiB
TypeScript
// P4 Tasks (AK-P4b) §6 — Task create route. Loader provides the assignee picker
|
|
// options + optional entity pre-fill (when opened as the "new task" CTA from an
|
|
// entity detail view). Action creates the task, audits, logs activity, redirects
|
|
// to the detail. Missing tasks.write -> 404.
|
|
|
|
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { badRequestResponse, loadTasksContext, notFoundResponse, requireTasksPermission } from "~/utils/integrations/taskAccess.server";
|
|
import {
|
|
createTask,
|
|
listAssignableGroups,
|
|
listAssignableUsers,
|
|
type LabeledOption,
|
|
type TaskEntityType,
|
|
type TaskPriority,
|
|
} from "~/utils/integrations/taskService.server";
|
|
import { auditTaskEvent, logTaskActivity } from "~/utils/integrations/taskAudit.server";
|
|
import { tasksDetailPath, tasksListPath } from "~/utils/integrations/taskRoutePaths";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
const TASK_PRIORITIES: TaskPriority[] = ["low", "normal", "high", "urgent"];
|
|
const TASK_ENTITY_TYPES: TaskEntityType[] = ["contact", "company", "lead", "deal", "project", "user", "order", "appointment"];
|
|
|
|
type Prefill = { entityType: TaskEntityType; entityId: string } | null;
|
|
type LoaderData = {
|
|
title: string;
|
|
assignableUsers: LabeledOption[];
|
|
assignableGroups: LabeledOption[];
|
|
prefill: Prefill;
|
|
cancelHref: string;
|
|
};
|
|
|
|
function parsePrefill(url: URL): Prefill {
|
|
const entityType = url.searchParams.get("entityType");
|
|
const entityId = url.searchParams.get("entityId");
|
|
if (entityType && entityId && TASK_ENTITY_TYPES.includes(entityType as TaskEntityType)) {
|
|
return { entityType: entityType as TaskEntityType, entityId };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadTasksContext({ request, params, requireModule: true });
|
|
try {
|
|
requireTasksPermission(ctx, "tasks.write");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const [assignableUsers, assignableGroups] = await Promise.all([listAssignableUsers(ctx.tenantId), listAssignableGroups(ctx.tenantId)]);
|
|
return Response.json({
|
|
title: `${t("tasks.new.title")} | ${process.env.APP_NAME}`,
|
|
assignableUsers,
|
|
assignableGroups,
|
|
prefill: parsePrefill(new URL(request.url)),
|
|
cancelHref: tasksListPath(params.tenant ?? ""),
|
|
} satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadTasksContext({ request, params, requireModule: true });
|
|
const tenantSlug = params.tenant ?? "";
|
|
try {
|
|
requireTasksPermission(ctx, "tasks.write");
|
|
const formData = await request.formData();
|
|
const title = String(formData.get("title") ?? "").trim();
|
|
if (!title) {
|
|
throw badRequestResponse();
|
|
}
|
|
const description = String(formData.get("description") ?? "");
|
|
const priorityRaw = String(formData.get("priority") ?? "normal");
|
|
const priority = (TASK_PRIORITIES.includes(priorityRaw as TaskPriority) ? priorityRaw : "normal") as TaskPriority;
|
|
const deadlineRaw = String(formData.get("deadline") ?? "").trim();
|
|
const assigneeType = String(formData.get("assigneeType") ?? "");
|
|
const assigneeId = String(formData.get("assigneeId") ?? "").trim();
|
|
const entityTypeRaw = String(formData.get("entityType") ?? "").trim();
|
|
const entityId = String(formData.get("entityId") ?? "").trim();
|
|
const linkedEntityType = entityTypeRaw && TASK_ENTITY_TYPES.includes(entityTypeRaw as TaskEntityType) ? (entityTypeRaw as TaskEntityType) : null;
|
|
|
|
const task = await createTask({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
title,
|
|
description: description || null,
|
|
priority,
|
|
deadline: deadlineRaw ? new Date(deadlineRaw) : null,
|
|
assignedToUserId: assigneeType === "user" && assigneeId ? assigneeId : null,
|
|
assignedToGroupId: assigneeType === "group" && assigneeId ? assigneeId : null,
|
|
linkedEntityType,
|
|
linkedEntityId: linkedEntityType && entityId ? entityId : null,
|
|
source: "manual",
|
|
});
|
|
await auditTaskEvent({ ctx, action: "task.created", taskId: task.id, metadata: { source: "manual" } });
|
|
await logTaskActivity({
|
|
ctx,
|
|
taskId: task.id,
|
|
activityType: "task_created",
|
|
summaryText: "Aufgabe erstellt",
|
|
linkedEntityType: task.linkedEntityType,
|
|
linkedEntityId: task.linkedEntityId,
|
|
});
|
|
return redirect(tasksDetailPath(tenantSlug, task.id));
|
|
} catch (error) {
|
|
if (error instanceof Response) throw error;
|
|
throw notFoundResponse();
|
|
}
|
|
};
|
|
|
|
export default function TaskCreateRoute() {
|
|
const { t } = useTranslation();
|
|
const { assignableUsers, assignableGroups, prefill, cancelHref } = useLoaderData<LoaderData>();
|
|
|
|
return (
|
|
<main className="mx-auto max-w-2xl px-4 py-10" data-ui-id="ui.tasks.new">
|
|
<h1 className="text-foreground text-xl font-semibold">{t("tasks.new.title")}</h1>
|
|
<form method="post" className="mt-6 grid gap-3">
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("tasks.detail.field.title")}</span>
|
|
<input name="title" required className="border-muted-foreground/40 w-full rounded border px-2 py-1" data-ui-id="ui.tasks.new.title" />
|
|
</label>
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("tasks.detail.field.description")}</span>
|
|
<textarea name="description" className="border-muted-foreground/40 w-full rounded border px-2 py-1" />
|
|
</label>
|
|
<div className="flex flex-wrap gap-3">
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("tasks.detail.field.priority")}</span>
|
|
<select name="priority" defaultValue="normal" className="border-muted-foreground/40 rounded border px-2 py-1">
|
|
{TASK_PRIORITIES.map((priority) => (
|
|
<option key={priority} value={priority}>
|
|
{t(`tasks.priority.${priority}`)}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("tasks.detail.field.deadline")}</span>
|
|
<input type="date" name="deadline" className="border-muted-foreground/40 rounded border px-2 py-1" />
|
|
</label>
|
|
</div>
|
|
<div className="flex flex-wrap items-end gap-2">
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("tasks.detail.field.assigneeType")}</span>
|
|
<select name="assigneeType" defaultValue="user" className="border-muted-foreground/40 rounded border px-2 py-1">
|
|
<option value="user">{t("tasks.detail.assignee.user")}</option>
|
|
<option value="group">{t("tasks.detail.assignee.group")}</option>
|
|
</select>
|
|
</label>
|
|
<label className="text-sm">
|
|
<span className="text-muted-foreground block text-xs">{t("tasks.detail.field.assignee")}</span>
|
|
<select name="assigneeId" className="border-muted-foreground/40 rounded border px-2 py-1">
|
|
<option value="">{t("tasks.new.assignee.default")}</option>
|
|
<optgroup label={t("tasks.detail.assignee.user")}>
|
|
{assignableUsers.map((user) => (
|
|
<option key={`u-${user.id}`} value={user.id}>
|
|
{user.label}
|
|
</option>
|
|
))}
|
|
</optgroup>
|
|
<optgroup label={t("tasks.detail.assignee.group")}>
|
|
{assignableGroups.map((group) => (
|
|
<option key={`g-${group.id}`} value={group.id}>
|
|
{group.label}
|
|
</option>
|
|
))}
|
|
</optgroup>
|
|
</select>
|
|
</label>
|
|
</div>
|
|
|
|
{prefill ? (
|
|
<>
|
|
<input type="hidden" name="entityType" value={prefill.entityType} />
|
|
<input type="hidden" name="entityId" value={prefill.entityId} />
|
|
<p className="text-muted-foreground text-xs" data-ui-id="ui.tasks.new.prefill">
|
|
{t(`tasks.entity.${prefill.entityType}`)} · {prefill.entityId}
|
|
</p>
|
|
</>
|
|
) : null}
|
|
|
|
<div className="mt-2 flex gap-3">
|
|
<button type="submit" className="text-theme-600 font-medium" data-ui-id="ui.tasks.new.submit">
|
|
{t("tasks.new.actions.create")}
|
|
</button>
|
|
<a href={cancelHref} className="text-muted-foreground" data-ui-id="ui.tasks.new.cancel">
|
|
{t("tasks.new.actions.cancel")}
|
|
</a>
|
|
</div>
|
|
</form>
|
|
</main>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|