67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import {
|
|
contentMutationService,
|
|
createContentMutationInvocation,
|
|
} from "@/features/housekeeping/domains/content/services/mutations";
|
|
import { createCorrelationId } from "@/features/housekeeping/foundation/contracts";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { PERMS } from "@/lib/permissions";
|
|
|
|
function boxInput(formData: FormData) {
|
|
const position = Number(formData.get("position"));
|
|
return {
|
|
title: String(formData.get("title") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, 255),
|
|
icon: String(formData.get("icon") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, 255),
|
|
content: String(formData.get("content") ?? "").normalize("NFC"),
|
|
position:
|
|
Number.isFinite(position) && position >= 0 ? Math.floor(position) : 0,
|
|
isActive: String(formData.get("isActive") ?? "").normalize("NFC") === "1",
|
|
};
|
|
}
|
|
|
|
function refreshBoxes(): void {
|
|
revalidatePath("/admin/writeable-boxes");
|
|
revalidatePath("/", "layout");
|
|
}
|
|
|
|
async function executeBox(
|
|
formData: FormData,
|
|
action: "create" | "update" | "delete" | "toggle",
|
|
): Promise<boolean> {
|
|
const staff = await requirePermission(PERMS.PAGES_EDIT);
|
|
const id = String(formData.get("id") ?? "")
|
|
.normalize("NFC")
|
|
.trim();
|
|
if (action !== "create" && !/^\d+$/u.test(id)) return false;
|
|
const input = boxInput(formData);
|
|
if ((action === "create" || action === "update") && !input.title)
|
|
return false;
|
|
const result = await contentMutationService.execute(
|
|
createContentMutationInvocation(staff, createCorrelationId()),
|
|
"writeable-box.change",
|
|
{ action, ...(id ? { id } : {}), ...input, next: formData.get("next") },
|
|
);
|
|
return result.ok;
|
|
}
|
|
|
|
export async function createBox(formData: FormData): Promise<void> {
|
|
if (await executeBox(formData, "create")) refreshBoxes();
|
|
}
|
|
export async function updateBox(formData: FormData): Promise<void> {
|
|
if (await executeBox(formData, "update")) refreshBoxes();
|
|
}
|
|
export async function deleteBox(formData: FormData): Promise<void> {
|
|
if (await executeBox(formData, "delete")) refreshBoxes();
|
|
}
|
|
export async function toggleBox(formData: FormData): Promise<void> {
|
|
if (await executeBox(formData, "toggle")) refreshBoxes();
|
|
}
|