"use client"; import { useState, useEffect } from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Plus, MoreHorizontal, Trash, Edit, Users } from "lucide-react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Label } from "@/components/ui/label"; import { toast } from "@/components/ui/use-toast"; interface Group { id: string; name: string; path: string; membersCount: number; } interface ApiError { message: string; } interface GroupsTableProps { userRole?: string[]; } export function GroupsTable({ userRole = [] }: GroupsTableProps) { const [groups, setGroups] = useState([]); const [loading, setLoading] = useState(true); const [searchTerm, setSearchTerm] = useState(""); const [newGroupDialog, setNewGroupDialog] = useState(false); const [newGroupName, setNewGroupName] = useState(""); useEffect(() => { fetchGroups(); }, []); const fetchGroups = async () => { try { setLoading(true); const response = await fetch("/api/groups"); const data = await response.json(); if (!response.ok) { throw new Error(data.message || "Erreur lors de la récupération des groupes"); } setGroups(Array.isArray(data) ? data : []); } catch (error) { toast({ title: "Erreur", description: error instanceof Error ? error.message : "Une erreur est survenue", variant: "destructive", }); } finally { setLoading(false); } }; const handleCreateGroup = async () => { try { if (!newGroupName.trim()) { throw new Error("Le nom du groupe est requis"); } const response = await fetch("/api/groups", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name: newGroupName }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.message || "Erreur lors de la création du groupe"); } setGroups(prev => [...prev, data]); setNewGroupDialog(false); setNewGroupName(""); toast({ title: "Succès", description: "Le groupe a été créé avec succès", }); } catch (error) { toast({ title: "Erreur", description: error instanceof Error ? error.message : "Une erreur est survenue", variant: "destructive", }); } }; const handleDeleteGroup = async (groupId: string) => { try { const response = await fetch(`/api/groups/${groupId}`, { method: "DELETE", }); if (!response.ok) { throw new Error("Erreur lors de la suppression du groupe"); } setGroups(prev => prev.filter(group => group.id !== groupId)); toast({ title: "Succès", description: "Le groupe a été supprimé avec succès", }); } catch (error) { toast({ title: "Erreur", description: error instanceof Error ? error.message : "Une erreur est survenue", variant: "destructive", }); } }; const filteredGroups = groups.filter(group => group.name.toLowerCase().includes(searchTerm.toLowerCase()) ); return (

Gestion des groupes

setSearchTerm(e.target.value)} className="w-full bg-black/20 border-0 text-white placeholder:text-gray-400" />
Créer un nouveau groupe
setNewGroupName(e.target.value)} placeholder="Entrez le nom du groupe" />
Nom du groupe Chemin Nombre de membres Actions {filteredGroups.map((group) => ( {group.name} {group.path} {group.membersCount} Actions console.log("Edit", group.id)} className="text-white hover:bg-black/50" > Modifier console.log("Manage members", group.id)} className="text-white hover:bg-black/50" > Gérer les membres handleDeleteGroup(group.id)} > Supprimer ))}
); }