Files
kalendartool/components/admin/CreateCalendarForm.tsx

71 lines
2.1 KiB
TypeScript

'use client';
/** Formular zum Erstellen eines Kalenders (nur Admin). */
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import { Button, Card, ErrorMessage, Input, Label } from '@/components/ui/ui';
export function CreateCalendarForm() {
const router = useRouter();
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setBusy(true);
setError(null);
const formData = new FormData(event.currentTarget);
try {
const response = await fetch('/api/calendars', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: formData.get('title'),
description: formData.get('description') || undefined,
timezone: formData.get('timezone'),
}),
});
if (!response.ok) {
const data = (await response.json()) as { error?: string };
setError(data.error ?? 'Erstellen fehlgeschlagen.');
return;
}
router.push('/dashboard');
router.refresh();
} catch {
setError('Netzwerkfehler. Bitte erneut versuchen.');
} finally {
setBusy(false);
}
}
return (
<Card>
<form onSubmit={handleSubmit} className="space-y-4">
<ErrorMessage message={error} />
<div>
<Label htmlFor="title">Titel</Label>
<Input id="title" name="title" type="text" required maxLength={120} />
</div>
<div>
<Label htmlFor="description">Beschreibung (optional)</Label>
<Input id="description" name="description" type="text" maxLength={2000} />
</div>
<div>
<Label htmlFor="timezone">Zeitzone</Label>
<Input
id="timezone"
name="timezone"
type="text"
required
defaultValue="Europe/Berlin"
maxLength={64}
/>
</div>
<Button disabled={busy}>{busy ? 'Erstellen …' : 'Kalender erstellen'}</Button>
</form>
</Card>
);
}