Aktuellste VErsion
Some checks are pending
Deploy Volleyball Dev / deploy (push) Waiting to run

This commit is contained in:
2025-05-16 18:38:48 +02:00
parent 269dd3c7a5
commit cc0c8c95f5
20 changed files with 113 additions and 45 deletions

View File

@@ -3,7 +3,7 @@ import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Button } from "@/components/ui/button";
import { Switch } from "@/components/ui/switch";
import axios from "axios";
import api from "@/lib/axios";
type Event = {
id?: number;
@@ -31,38 +31,40 @@ const EventsAdmin = () => {
const fetchEvents = async () => {
try {
const res = await axios.get("/api/events?showPrivate=true");
if (Array.isArray(res.data)) {
setEvents(res.data);
} else {
console.warn("⚠️ Events-API hat kein Array geliefert:", res.data);
setEvents([]);
}
const res = await api.get("/api/events?showPrivate=true");
setEvents(Array.isArray(res.data) ? res.data : []);
} catch (error) {
console.error("❌ Fehler beim Laden der Events:", error);
setEvents([]);
}
};
useEffect(() => {
fetchEvents();
}, []);
const handleSubmit = async () => {
if (isEditing && form.id) {
await axios.put(`/api/events/${form.id}`, form);
} else {
await axios.post("/api/events", form);
try {
if (isEditing && form.id) {
await api.put(`/api/events/${form.id}`, form);
} else {
await api.post("/api/events", form);
}
setForm(defaultEvent);
setIsEditing(false);
fetchEvents();
} catch (error) {
console.error("❌ Fehler beim Speichern:", error);
}
setForm(defaultEvent);
setIsEditing(false);
fetchEvents();
};
const handleDelete = async (id: number) => {
await axios.delete(`/api/events/${id}`);
fetchEvents();
try {
await api.delete(`/api/events/${id}`);
fetchEvents();
} catch (error) {
console.error("❌ Fehler beim Löschen:", error);
}
};
const handleEdit = (event: Event) => {
@@ -89,14 +91,18 @@ const EventsAdmin = () => {
type="number"
placeholder="Max. Teilnehmer"
value={form.max_participants ?? ""}
onChange={(e) => setForm({ ...form, max_participants: parseInt(e.target.value) || undefined })}
onChange={(e) =>
setForm({ ...form, max_participants: parseInt(e.target.value) || undefined })
}
/>
<Input
type="number"
step="0.01"
placeholder="Gebühr (€)"
value={form.fee ?? ""}
onChange={(e) => setForm({ ...form, fee: parseFloat(e.target.value) || undefined })}
onChange={(e) =>
setForm({ ...form, fee: parseFloat(e.target.value) || undefined })
}
/>
<Input
placeholder="Adresse"
@@ -116,7 +122,13 @@ const EventsAdmin = () => {
{isEditing ? "Speichern" : "Erstellen"}
</Button>
{isEditing && (
<Button variant="ghost" onClick={() => { setForm(defaultEvent); setIsEditing(false); }}>
<Button
variant="ghost"
onClick={() => {
setForm(defaultEvent);
setIsEditing(false);
}}
>
Abbrechen
</Button>
)}
@@ -126,25 +138,36 @@ const EventsAdmin = () => {
<hr className="my-6" />
<h2 className="text-xl font-semibold mb-2">Bestehende Events</h2>
{Array.isArray(events) && events.length > 0 ? (
{events.length > 0 ? (
<ul className="space-y-2">
{events.map((ev) => (
<li key={ev.id} className="p-3 border rounded-md shadow-sm flex justify-between items-center">
<div>
{events.map((ev) => (
<li
key={ev.id}
className="p-3 border rounded-md shadow-sm flex justify-between items-center"
>
<div>
<div className="font-medium">{ev.title}</div>
<div className="text-sm text-gray-500">{ev.description?.slice(0, 60)}...</div>
</div>
<div className="space-x-2">
<Button variant="outline" onClick={() => handleEdit(ev)}>Bearbeiten</Button>
<Button variant="destructive" onClick={() => handleDelete(ev.id!)}>Löschen</Button>
<div className="text-sm text-gray-500">
{ev.description?.slice(0, 60)}...
</div>
</div>
<div className="space-x-2">
<Button variant="outline" onClick={() => handleEdit(ev)}>
Bearbeiten
</Button>
<Button
variant="destructive"
onClick={() => handleDelete(ev.id!)}
>
Löschen
</Button>
</div>
</li>
))}
))}
</ul>
) : (
) : (
<p className="text-gray-500">Noch keine Events vorhanden.</p>
)}
)}
</div>
);
};