75 lines
3.3 KiB
TypeScript
75 lines
3.3 KiB
TypeScript
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData, useNavigation, useParams, useSubmit } from "react-router";
|
|
import { badRequestResponse, leadActionErrorResponse, loadLeadContext, requireLeadPermission } from "~/utils/integrations/leads/leadAccess.server";
|
|
import { auditLeadEvent, logLeadActivity } from "~/utils/integrations/leads/leadAudit.server";
|
|
import { leadsPath, LEADS_MODULE_KEY } from "~/utils/integrations/leads/leadRoutePaths";
|
|
import { createLead, type LeadQuelle } from "~/utils/integrations/leads/leadService.server";
|
|
import { LeadNewPage, type LeadNewFields } from "~/components/screens/leads/LeadNewPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
|
|
const QUELLEN = new Set<LeadQuelle>(["form", "email", "aircall", "manual", "custom"]);
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
|
requireLeadPermission(ctx, "leads.write");
|
|
const { t } = await getTranslations(request);
|
|
// Page title consumed by the shell breadcrumb (useTitleData).
|
|
return Response.json({ title: t("leads.new.title") });
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
|
requireLeadPermission(ctx, "leads.write");
|
|
const tenantSlug = params.tenant ?? "";
|
|
const form = await request.formData();
|
|
const quelle = form.get("quelle")?.toString() as LeadQuelle | undefined;
|
|
if (!quelle || !QUELLEN.has(quelle)) throw badRequestResponse();
|
|
try {
|
|
const lead = await createLead({
|
|
tenantId: ctx.tenantId,
|
|
userId: ctx.userId,
|
|
quelle,
|
|
vorname: form.get("vorname")?.toString(),
|
|
nachname: form.get("nachname")?.toString(),
|
|
email: form.get("email")?.toString(),
|
|
telefon: form.get("telefon")?.toString(),
|
|
message: form.get("message")?.toString(),
|
|
zustaendigerUserId: form.get("zustaendigerUserId")?.toString() || ctx.userId,
|
|
});
|
|
await auditLeadEvent({ ctx, action: "lead.created", targetType: "lead", targetId: lead.id, metadata: { quelle } });
|
|
await logLeadActivity({ ctx, leadId: lead.id, activityType: "lead_created", summaryText: "leads.activity.created" });
|
|
// Back to the inbox, not into the new lead: creating one is a triage step,
|
|
// and the list is where the next one gets handled.
|
|
return redirect(`${leadsPath(tenantSlug)}?toast=${encodeURIComponent("leads.toast.created")}`);
|
|
} catch (error) {
|
|
return leadActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function LeadNewRoute() {
|
|
const params = useParams();
|
|
const actionData = useActionData<{ error?: string }>();
|
|
const submit = useSubmit();
|
|
const navigation = useNavigation();
|
|
return (
|
|
<LeadNewPage
|
|
backHref={leadsPath(params.tenant ?? "")}
|
|
submitting={navigation.state !== "idle"}
|
|
errorKey={actionData?.error ?? null}
|
|
actions={{
|
|
onCreate: (fields: LeadNewFields) => {
|
|
const form = new FormData();
|
|
for (const [key, value] of Object.entries(fields)) form.set(key, value);
|
|
submit(form, { method: "post" });
|
|
},
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: LEADS_MODULE_KEY };
|