fix: restore approveEditorialAction and fix AlbumGrid type signature

This commit is contained in:
Leon Tonhäuser committed 2026-08-11 02:55:25 +02:00
1 parent e555069ee1
commit 6e25739e6e
2 files changed
+96 -14

No files matched your search

+80 -7
View File
@@ -3,8 +3,43 @@
import { revalidatePath } from "next/cache";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { requireAdmin } from "@/modules/auth/principal";
import { editorialService } from "@/modules/editorials/service";
import { editorialService, EditorialServiceError } from "@/modules/editorials/service";
// Returned to the client form so failures surface as inline German text
// instead of an opaque Next.js error-digest page (a thrown server action
// becomes a 500 with a digest the user can't read).
export type ApproveActionResult = { ok: boolean; message: string };
const INITIAL_RESULT: ApproveActionResult = { ok: true, message: "" };
export async function approveEditorialAction(
editorialId: string,
_prevState: ApproveActionResult,
formData: FormData,
): Promise<ApproveActionResult> {
const principal = await requireAdmin(new Headers(await headers()));
const safeEditorialId = requiredId(editorialId);
const price = parsePriceCents(formData.get("priceEuros"));
if (!price.ok) {
return { ok: false, message: price.message };
}
try {
await editorialService.approveEditorial({
editorialId: safeEditorialId,
priceCents: price.cents,
adminUserId: principal.userId,
});
} catch (error) {
return { ok: false, message: approveErrorMessage(error) };
}
revalidatePath(`/admin/redaktionen/${safeEditorialId}`);
revalidatePath(`/admin/redaktionen`);
return INITIAL_RESULT;
}
export async function archiveEditorialAction(editorialId: string): Promise<void> {
const admin = await requireAdmin(new Headers(await headers()));
@@ -13,16 +48,16 @@ export async function archiveEditorialAction(editorialId: string): Promise<void>
adminUserId: admin.userId,
});
revalidatePath(`/admin/redaktionen/${editorialId}`);
revalidatePath('/admin/redaktionen');
redirect('/admin/redaktionen');
revalidatePath("/admin/redaktionen");
redirect("/admin/redaktionen");
}
export async function suspendEditorialAction(editorialId: string): Promise<void> {
const admin = await requireAdmin(new Headers(await headers()));
await editorialService.transitionEditorialStatus({
editorialId,
from: 'active',
to: 'suspended',
from: "active",
to: "suspended",
adminUserId: admin.userId,
});
revalidatePath(`/admin/redaktionen/${editorialId}`);
@@ -32,9 +67,47 @@ export async function reactivateEditorialAction(editorialId: string): Promise<vo
const admin = await requireAdmin(new Headers(await headers()));
await editorialService.transitionEditorialStatus({
editorialId,
from: 'suspended',
to: 'active',
from: "suspended",
to: "active",
adminUserId: admin.userId,
});
revalidatePath(`/admin/redaktionen/${editorialId}`);
}
function parsePriceCents(
value: unknown,
): { ok: true; cents: number } | { ok: false; message: string } {
const euros =
typeof value === "string" ? Number.parseFloat(value.replace(",", ".")) : Number.NaN;
if (!Number.isFinite(euros) || euros <= 0) {
return { ok: false, message: "Bitte geben Sie einen gültigen Nettopreis an." };
}
return { ok: true, cents: Math.round(euros * 100) };
}
function approveErrorMessage(error: unknown): string {
if (error instanceof EditorialServiceError) {
const messages: Record<string, string> = {
INVALID_PRICE: "Bitte geben Sie einen gültigen Nettopreis an.",
EDITORIAL_NOT_FOUND: "Redaktion nicht gefunden.",
EMAIL_NOT_VERIFIED: "Die E-Mail-Adresse der Redaktion ist noch nicht bestätigt.",
ALBUM_GRANT_REQUIRED: "Bitte weisen Sie zuerst mindestens ein Album zu.",
INVALID_ACCOUNT_TRANSITION:
"Die Redaktion kann in ihrem aktuellen Status nicht freigegeben werden.",
APPROVAL_NOTICE_DELIVERY_UNKNOWN:
"Der Freigabe-Hinweis konnte nicht eindeutig zugeordnet werden.",
};
return messages[error.code] ?? "Die Freigabe ist fehlgeschlagen.";
}
return "Die Freigabe ist fehlgeschlagen.";
}
function requiredId(value: unknown): string {
if (
typeof value !== "string" ||
!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
) {
throw new Error("INVALID_ID");
}
return value;
}
@@ -3,6 +3,14 @@
import { useState, useTransition } from "react";
import Image from "next/image";
import type { AlbumActionResult } from "./actions";
type AlbumGridAction = (
editorialId: string,
_prevState: AlbumActionResult,
formData: FormData,
) => Promise<AlbumActionResult>;
type AlbumGridProps = {
albums: Array<{
id: string;
@@ -12,8 +20,8 @@ type AlbumGridProps = {
isGranted: boolean;
}>;
editorialId: string;
grantAction: (editorialId: string, formData: FormData) => Promise<void>;
revokeAction: (editorialId: string, formData: FormData) => Promise<void>;
grantAction: AlbumGridAction;
revokeAction: AlbumGridAction;
};
function AlbumCard({
@@ -24,17 +32,18 @@ function AlbumCard({
}: {
album: AlbumGridProps["albums"][0];
editorialId: string;
grantAction: AlbumGridProps["grantAction"];
revokeAction: AlbumGridProps["revokeAction"];
grantAction: AlbumGridAction;
revokeAction: AlbumGridAction;
}) {
const [isPending, startTransition] = useTransition();
const handleAction = (formData: FormData) => {
startTransition(async () => {
const initial: AlbumActionResult = { ok: true, message: "" };
if (album.isGranted) {
await revokeAction(editorialId, formData);
await revokeAction(editorialId, initial, formData);
} else {
await grantAction(editorialId, formData);
await grantAction(editorialId, initial, formData);
}
});
};
@@ -83,7 +92,7 @@ export function AlbumGrid({ albums, editorialId, grantAction, revokeAction }: Al
const [search, setSearch] = useState("");
const filteredAlbums = albums.filter((album) =>
album.albumName.toLowerCase().includes(search.toLowerCase())
album.albumName.toLowerCase().includes(search.toLowerCase()),
);
return (