48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import { useRouter } from "next/navigation";
|
|
import { useState, useTransition } from "react";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import type { CalendarActionResult } from "./actions";
|
|
|
|
// Grants access to the event's linked album and jumps straight into it.
|
|
export function SelfGrantButton({
|
|
action,
|
|
eventId,
|
|
}: Readonly<{
|
|
action: (state: CalendarActionResult, formData: FormData) => Promise<CalendarActionResult>;
|
|
eventId: string;
|
|
}>) {
|
|
const router = useRouter();
|
|
const [error, setError] = useState("");
|
|
const [pending, startTransition] = useTransition();
|
|
|
|
const handleClick = () => {
|
|
const formData = new FormData();
|
|
formData.set("eventId", eventId);
|
|
startTransition(async () => {
|
|
const result = await action({ ok: true, message: "" }, formData);
|
|
if (result.ok && result.albumId) {
|
|
router.push(`/redaktion/alben/${encodeURIComponent(result.albumId)}`);
|
|
} else {
|
|
setError(result.message);
|
|
}
|
|
});
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-1">
|
|
<Button type="button" className="min-h-11" disabled={pending} onClick={handleClick}>
|
|
{pending ? "Wird freigeschaltet…" : "Zugriff freischalten"}
|
|
</Button>
|
|
{error ? (
|
|
<p role="alert" className="text-xs text-destructive">
|
|
{error}
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|