import { apiCall, getUsers } from "@/lib"; import zustand, { User } from "@/Zustand"; import { Button, ButtonGroup, Checkbox, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow, Tooltip, } from "@heroui/react"; import { useAsyncList } from "@react-stately/data"; import { useState } from "react"; import AddUser from "./AddUser"; import { AddLarge, Edit, TrashCan } from "@carbon/icons-react"; import EditUser from "./EditUser"; import DeleteConfirmation from "@/components/DeleteConfirmation"; export default function Users() { const [showAddUser, setShowAddUser] = useState(false); const [editUser, setEditUser] = useState(); const [deleteUser, setDeleteUser] = useState(); const loggedInUser = zustand((state) => state.user); const users = useAsyncList({ async load() { return { items: await getUsers(), }; }, async sort({ items, sortDescriptor }) { return { items: items.sort((a, b) => { let cmp = 0; switch (sortDescriptor.column) { case "admin": if (a.admin && !b.admin) { cmp = -1; } else if (!a.admin && b.admin) { cmp = 1; } break; case "userName": cmp = a.userName.localeCompare(b.userName); } if (sortDescriptor.direction === "descending") { cmp *= -1; } return cmp; }), }; }, }); async function sendDeleteUser(userName: User["userName"] | undefined) { if (!!userName) { const result = await apiCall("DELETE", "users", { userName, }); if (result.ok) { // clear the users first from the zustand zustand.getState().patch({ users: undefined }); users.reload(); setDeleteUser(undefined); } } } // content above the user-tabel const topContent = (
); return (
tr]:first:!shadow-border", }} > Username Admin Edit {(user) => ( {user.userName} )}
{ setShowAddUser(false); // clear the users first from the zustand zustand.getState().patch({ users: undefined }); users.reload(); }} /> !isOpen ? setEditUser(undefined) : undefined } onSuccess={() => { // clear the users first from the zustand zustand.getState().patch({ users: undefined }); users.reload(); setEditUser(undefined); }} /> (!isOpen ? setDeleteUser(undefined) : null)} onDelete={() => sendDeleteUser(deleteUser?.userName)} itemName="User" > {" "} The user {deleteUser?.userName} will be deleted.
); }