'use client'; import { useUsers, useDeleteUser } from '@/hooks/use-users'; import { useOrganizations } from '@/hooks/use-master-data'; import { Button } from '@/components/ui/button'; import { DataTable } from '@/components/common/data-table'; import { Plus, MoreHorizontal, Pencil, Trash, Search } from 'lucide-react'; import { useState } from 'react'; import { UserDialog } from '@/components/admin/user-dialog'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { Badge } from '@/components/ui/badge'; import { ColumnDef } from '@tanstack/react-table'; import { User } from '@/types/user'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { Skeleton } from '@/components/ui/skeleton'; import { Organization } from '@/types/organization'; import { getApiErrorMessage } from '@/types/api-error'; export default function UsersPage() { const [search, setSearch] = useState(''); const [selectedOrgId, setSelectedOrgId] = useState(null); const { data: users, isLoading, isError, error, } = useUsers({ search: search || undefined, primaryOrganizationId: selectedOrgId ?? undefined, }); const { data: organizations = [] } = useOrganizations(); const userList = Array.isArray(users) ? users : []; const organizationList: Organization[] = Array.isArray(organizations) ? organizations : []; const deleteMutation = useDeleteUser(); const [dialogOpen, setDialogOpen] = useState(false); const [selectedUser, setSelectedUser] = useState(null); // Stats for Delete Dialog const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [userToDelete, setUserToDelete] = useState(null); const handleDeleteClick = (user: User) => { setUserToDelete(user); setDeleteDialogOpen(true); }; const confirmDelete = () => { if (userToDelete) { deleteMutation.mutate(userToDelete.uuid, { onSuccess: () => { setDeleteDialogOpen(false); setUserToDelete(null); }, }); } }; const columns: ColumnDef[] = [ { accessorKey: 'username', header: 'Username', cell: ({ row }) => {row.original.username}, }, { accessorKey: 'email', header: 'Email', }, { id: 'name', header: 'Name', cell: ({ row }) => `${row.original.firstName} ${row.original.lastName}`, }, { id: 'organization', header: 'Organization', cell: ({ row }) => { const orgId = row.original.primaryOrganizationId; if (!orgId) { return 'All Organizations'; } const org = organizationList.find( (o) => (o.id ?? o.uuid) === orgId?.toString() || o.uuid === orgId?.toString() ); return org ? org.organizationCode : 'All Organizations'; }, }, { id: 'roles', header: 'Roles', cell: ({ row }) => { const roles = row.original.roles || []; return (
{roles.map((r) => ( {r.roleName} ))}
); }, }, { accessorKey: 'isActive', header: 'Status', cell: ({ row }) => ( {row.original.isActive ? 'Active' : 'Inactive'} ), }, { id: 'actions', header: 'Actions', cell: ({ row }) => { const user = row.original; return ( { setSelectedUser(user); setDialogOpen(true); }} > Edit handleDeleteClick(user)}> Delete ); }, }, ]; return (

User Management

Manage system users and roles

setSearch(e.target.value)} className="pl-8 bg-background" />
{isError && (
{getApiErrorMessage(error, 'Failed to load users')}
)} {isLoading ? (
{[1, 2, 3, 4, 5].map((i) => (
))}
) : ( )} Are you absolutely sure? This action cannot be undone. This will permanently delete the user {userToDelete?.username} and remove them from the system. Cancel {deleteMutation.isPending ? 'Deleting...' : 'Delete User'}
); }