Files
FL-Akademie/app/admin/landing/actions.ts

52 lines
1.8 KiB
TypeScript

"use server";
import { revalidatePath } from "next/cache";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth-options";
import type { LandingContentV1 } from "@/lib/landing";
import { saveLandingContent } from "@/lib/landing";
async function assertAdmin() {
const session = await getServerSession(authOptions);
if (!session?.user?.id || session.user.role !== "ADMIN") {
throw new Error("Keine Berechtigung.");
}
}
export async function updateLandingAction(formData: FormData) {
await assertAdmin();
const heroTitle = String(formData.get("heroTitle") ?? "").trim();
const heroLead = String(formData.get("heroLead") ?? "").trim();
const primaryLabel = String(formData.get("primaryLabel") ?? "").trim();
const primaryHref = String(formData.get("primaryHref") ?? "").trim();
const secondaryLabel = String(formData.get("secondaryLabel") ?? "").trim();
const secondaryHref = String(formData.get("secondaryHref") ?? "").trim();
const benefitSectionTitle = String(formData.get("benefitSectionTitle") ?? "").trim();
const benefits: { title: string; body: string }[] = [];
for (let i = 1; i <= 6; i++) {
const title = String(formData.get(`benefit${i}Title`) ?? "").trim();
const body = String(formData.get(`benefit${i}Body`) ?? "").trim();
if (title || body) benefits.push({ title, body });
}
const content: LandingContentV1 = {
version: 1,
heroTitle,
heroLead,
primaryCta: {
label: primaryLabel || "Zu den Kursen",
href: primaryHref || "/kurse",
},
secondaryCta:
secondaryLabel && secondaryHref ? { label: secondaryLabel, href: secondaryHref } : undefined,
benefitSectionTitle: benefitSectionTitle || "Deine Vorteile",
benefits,
};
await saveLandingContent(content);
revalidatePath("/");
revalidatePath("/admin/landing");
}