"use client" import { useState, useEffect } from "react" import { useAuth } from "@/lib/auth-context" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Trash2, Plus, Upload } from "lucide-react" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import type { Room } from "@/lib/types" export function RoomManagement() { const { rooms, authHeader } = useAuth() const [localRooms, setLocalRooms] = useState([]) const [newRoomName, setNewRoomName] = useState("") const [isCreateOpen, setIsCreateOpen] = useState(false) const [csvFile, setCsvFile] = useState(null) const [uploading, setUploading] = useState(false) const API_URL = process.env.NEXT_PUBLIC_API_URL + "/api" useEffect(() => { setLocalRooms(rooms) }, [rooms]) const refreshRooms = async () => { if (!authHeader) return const res = await fetch(`${API_URL}/rooms`, { headers: { Authorization: authHeader } }) if (res.ok) { const data = await res.json() setLocalRooms(data) } } const handleCreate = async () => { if (!newRoomName.trim() || !authHeader) return try { const res = await fetch(`${API_URL}/rooms`, { method: "POST", headers: { "Authorization": authHeader, "Content-Type": "application/json" }, body: JSON.stringify({ name: newRoomName }) }) if (res.ok) { setNewRoomName("") setIsCreateOpen(false) refreshRooms() } } catch (e) { console.error(e) } } const handleDelete = async (id: number) => { if (!confirm("Raum wirklich löschen? Dies könnte fehlschlagen, wenn Tickets existieren.") || !authHeader) return try { const res = await fetch(`${API_URL}/rooms/${id}`, { method: "DELETE", headers: { "Authorization": authHeader } }) if (res.ok) refreshRooms() else alert("Löschen fehlgeschlagen (evtl. existieren Tickets für diesen Raum)") } catch (e) { console.error(e) } } const handleUpdate = async (id: number, newName: string) => { if (!authHeader) return try { const res = await fetch(`${API_URL}/rooms/${id}`, { method: "PUT", headers: { "Authorization": authHeader, "Content-Type": "application/json" }, body: JSON.stringify({ name: newName }) }) if (res.ok) refreshRooms() } catch (e) { console.error(e) } } const handleFileUpload = async () => { if (!csvFile || !authHeader) return setUploading(true) const formData = new FormData() formData.append("file", csvFile) try { const res = await fetch(`${API_URL}/rooms/import`, { method: "POST", headers: { "Authorization": authHeader }, body: formData }) if (res.ok) { const data = await res.json() alert(data.message) setCsvFile(null) refreshRooms() } } catch (e) { console.error(e) } finally { setUploading(false) } } return (
Räume verwalten Erstellen, Bearbeiten, Löschen und CSV-Import
Neuen Raum erstellen
setNewRoomName(e.target.value)} />
{/* CSV Import Section */}
Raum-Import (CSV) Datei mit Raumnamen pro Zeile
setCsvFile(e.target.files?.[0] || null)} className="w-full max-w-xs" />
ID Name Aktionen {localRooms.map((room) => ( {room.id} { if (e.target.value !== room.name) { handleUpdate(room.id, e.target.value) } }} /> ))}
) }