62 lines
3.4 KiB
TypeScript
62 lines
3.4 KiB
TypeScript
import { type ActionFunctionArgs, type LoaderFunctionArgs, Form, redirect, useActionData, useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { badRequestResponse, leadActionErrorResponse, loadLeadContext, requireLeadPermission } from "~/utils/integrations/leads/leadAccess.server";
|
|
import { auditLeadEvent } from "~/utils/integrations/leads/leadAudit.server";
|
|
import { leadSourcesAdminPath, LEADS_MODULE_KEY } from "~/utils/integrations/leads/leadRoutePaths";
|
|
import { listLeadSources, upsertLeadSource, type LeadSourceRow } from "~/utils/integrations/leads/leadService.server";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { sources: LeadSourceRow[] };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
|
requireLeadPermission(ctx, "leads.admin");
|
|
return Response.json({ sources: await listLeadSources(ctx.tenantId) } satisfies LoaderData);
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
|
requireLeadPermission(ctx, "leads.admin");
|
|
const form = await request.formData();
|
|
if (form.get("intent")?.toString() !== "upsert") throw badRequestResponse();
|
|
try {
|
|
const source = await upsertLeadSource({
|
|
tenantId: ctx.tenantId,
|
|
key: form.get("key")?.toString() ?? "",
|
|
name: form.get("name")?.toString() ?? "",
|
|
displayLabel: form.get("displayLabel")?.toString(),
|
|
defaultZustaendigerUserId: form.get("defaultZustaendigerUserId")?.toString() || null,
|
|
});
|
|
await auditLeadEvent({ ctx, action: "lead.source_created", targetType: "lead_source", targetId: source.id });
|
|
return redirect(leadSourcesAdminPath(params.tenant ?? ""));
|
|
} catch (error) {
|
|
return leadActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function LeadSourcesRoute() {
|
|
const { t } = useTranslation();
|
|
const { sources } = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<{ error?: string }>();
|
|
return (
|
|
<main className="mx-auto max-w-4xl px-4 py-10" data-ui-id="ui.leads.sources">
|
|
<h1 className="text-foreground text-xl font-semibold">{t("leads.sources.title")}</h1>
|
|
{actionData?.error ? <p className="mt-4 text-sm text-red-600">{t(actionData.error)}</p> : null}
|
|
<Form method="post" className="mt-6 grid gap-3 rounded-md border p-4">
|
|
<input type="hidden" name="intent" value="upsert" />
|
|
<label className="grid gap-1 text-sm">{t("leads.sources.field.key")}<input name="key" className="rounded border px-3 py-2" /></label>
|
|
<label className="grid gap-1 text-sm">{t("leads.sources.field.name")}<input name="name" className="rounded border px-3 py-2" /></label>
|
|
<label className="grid gap-1 text-sm">{t("leads.sources.field.displayLabel")}<input name="displayLabel" className="rounded border px-3 py-2" /></label>
|
|
<button className="bg-foreground text-background rounded px-4 py-2 text-sm font-medium">{t("leads.sources.actions.save")}</button>
|
|
</Form>
|
|
<ul className="mt-6 grid gap-2 text-sm">{sources.map((source) => <li key={source.id} className="rounded border p-3">{source.displayLabel}</li>)}</ul>
|
|
</main>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: LEADS_MODULE_KEY };
|