83 lines
3.8 KiB
TypeScript
83 lines
3.8 KiB
TypeScript
import { type ActionFunctionArgs, type LoaderFunctionArgs, redirect, useActionData, useLoaderData, useNavigation, useSubmit } from "react-router";
|
|
import { buildLeadAntiSpamVM, type LeadAntiSpamVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { badRequestResponse, leadActionErrorResponse, loadLeadContext, requireLeadPermission } from "~/utils/integrations/leads/leadAccess.server";
|
|
import { auditLeadEvent } from "~/utils/integrations/leads/leadAudit.server";
|
|
import { leadAntiSpamAdminPath, LEADS_MODULE_KEY } from "~/utils/integrations/leads/leadRoutePaths";
|
|
import { getLeadAntiSpamConfig, updateLeadAntiSpamConfig, type LeadAntiSpamMode } from "~/utils/integrations/leads/leadService.server";
|
|
import { LeadAntiSpamAdminPage } from "~/components/screens/leads/LeadAntiSpamAdminPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { useSaveToast } from "~/hooks/use-save-toast";
|
|
|
|
type LoaderData = { title: string; vm: LeadAntiSpamVM };
|
|
const MODES = new Set<LeadAntiSpamMode>(["quarantine", "spam"]);
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
|
requireLeadPermission(ctx, "leads.admin");
|
|
const config = await getLeadAntiSpamConfig(ctx.tenantId);
|
|
const title = `${t("leads.antiSpam.title")} | ${process.env.APP_NAME}`;
|
|
return Response.json({ title, vm: buildLeadAntiSpamVM({ config, canWrite: true }) } satisfies LoaderData);
|
|
};
|
|
|
|
function intValue(form: FormData, name: string): number | undefined {
|
|
const value = Number.parseInt(form.get(name)?.toString() ?? "", 10);
|
|
return Number.isFinite(value) ? value : undefined;
|
|
}
|
|
|
|
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() !== "save") throw badRequestResponse();
|
|
const mode = form.get("mode")?.toString() as LeadAntiSpamMode | undefined;
|
|
if (!mode || !MODES.has(mode)) throw badRequestResponse();
|
|
try {
|
|
const config = await updateLeadAntiSpamConfig({
|
|
tenantId: ctx.tenantId,
|
|
mode,
|
|
perSourcePerHourLimit: intValue(form, "perSourcePerHourLimit"),
|
|
perIpPerHourLimit: intValue(form, "perIpPerHourLimit"),
|
|
anomalySpikeFactor: intValue(form, "anomalySpikeFactor"),
|
|
lostAutoDeleteDays: intValue(form, "lostAutoDeleteDays"),
|
|
disposableDomainEnabled: form.get("disposableDomainEnabled") === "on",
|
|
honeypotEnabled: form.get("honeypotEnabled") === "on",
|
|
fastSubmitEnabled: form.get("fastSubmitEnabled") === "on",
|
|
});
|
|
await auditLeadEvent({ ctx, action: "lead.anti_spam_updated", targetType: "lead_anti_spam", targetId: config.id });
|
|
return redirect(`${leadAntiSpamAdminPath(params.tenant ?? "")}?toast=${encodeURIComponent("leads.toast.antiSpamSaved")}`);
|
|
} catch (error) {
|
|
return leadActionErrorResponse(error);
|
|
}
|
|
};
|
|
|
|
export default function LeadAntiSpamRoute() {
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
const actionData = useActionData<{ error?: string }>();
|
|
useSaveToast(actionData);
|
|
const submit = useSubmit();
|
|
const navigation = useNavigation();
|
|
return (
|
|
<LeadAntiSpamAdminPage
|
|
vm={vm}
|
|
submitting={navigation.state !== "idle"}
|
|
errorKey={actionData?.error ?? null}
|
|
actions={{
|
|
onSave: (fields) => {
|
|
const form = new FormData();
|
|
form.set("intent", "save");
|
|
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 };
|