All files / pages RolesPage.tsx

80% Statements 28/35
93.75% Branches 15/16
60% Functions 6/10
80% Lines 28/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192                                                    42x 42x 42x     42x 42x 42x 42x   42x 19x 19x   19x 19x 15x   3x   18x       42x 18x     42x         42x         42x         42x 1x 1x     42x 1x 1x     42x               2x                                                                                                                                                                                                                
/**
 * Roles & permissions management page
 * Full implementation with CRUD operations and permission management
 */
 
import { useEffect, useState } from 'react';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Plus, RefreshCw, Shield } from 'lucide-react';
import RolesTable from '@/components/roles/RolesTable';
import CreateRoleDialog from '@/components/roles/CreateRoleDialog';
import EditRoleDialog from '@/components/roles/EditRoleDialog';
import DeleteRoleDialog from '@/components/roles/DeleteRoleDialog';
import {
    getRoles,
    createRole,
    updateRole,
    deleteRole,
    type RoleListItem,
    type RoleListResponse,
    type CreateRoleData,
    type UpdateRoleData,
} from '@/services/roles.service';
import { handleApiError } from '@/lib/api';
 
export default function RolesPage() {
    const [data, setData] = useState<RoleListResponse | null>(null);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState<string | null>(null);
 
    // Dialog state
    const [createDialogOpen, setCreateDialogOpen] = useState(false);
    const [editDialogOpen, setEditDialogOpen] = useState(false);
    const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
    const [selectedRole, setSelectedRole] = useState<RoleListItem | null>(null);
 
    const fetchRoles = async () => {
        setLoading(true);
        setError(null);
 
        try {
            const response = await getRoles();
            setData(response);
        } catch (err) {
            setError(handleApiError(err));
        } finally {
            setLoading(false);
        }
    };
 
    useEffect(() => {
        fetchRoles();
    }, []);
 
    const handleCreateRole = async (data: CreateRoleData) => {
        await createRole(data);
        await fetchRoles();
    };
 
    const handleUpdateRole = async (roleId: number, data: UpdateRoleData) => {
        await updateRole(roleId, data);
        await fetchRoles();
    };
 
    const handleDeleteRole = async (roleId: number) => {
        await deleteRole(roleId);
        await fetchRoles();
    };
 
    const handleEdit = (role: RoleListItem) => {
        setSelectedRole(role);
        setEditDialogOpen(true);
    };
 
    const handleDelete = (role: RoleListItem) => {
        setSelectedRole(role);
        setDeleteDialogOpen(true);
    };
 
    return (
        <div className="space-y-6">
            {/* Header */}
            <div className="flex items-center justify-between">
                <div>
                    <h1 className="text-3xl font-bold">Roles & Permissions</h1>
                    <p className="text-muted-foreground mt-2">Manage access control and permissions</p>
                </div>
                <Button onClick={() => setCreateDialogOpen(true)} className="gap-2">
                    <Plus className="h-4 w-4" />
                    Create Role
                </Button>
            </div>
 
            {/* Roles Management */}
            <Card>
                <CardHeader>
                    <CardTitle className="flex items-center gap-2">
                        <Shield className="h-5 w-5 text-primary" />
                        Role Management
                    </CardTitle>
                    <CardDescription>
                        Create and manage roles, configure permissions for each role
                    </CardDescription>
                </CardHeader>
                <CardContent className="space-y-4">
                    {/* Refresh Button */}
                    <div className="flex justify-end">
                        <Button
                            variant="outline"
                            size="icon"
                            onClick={fetchRoles}
                            disabled={loading}
                        >
                            <RefreshCw className={`h-4 w-4 ${loading ? 'animate-spin' : ''}`} />
                        </Button>
                    </div>
 
                    {/* Error State */}
                    {error && (
                        <div className="bg-destructive/10 border border-destructive/20 rounded-lg p-4">
                            <p className="text-destructive font-medium">Failed to load roles</p>
                            <p className="text-sm text-muted-foreground mt-1">{error}</p>
                            <Button onClick={fetchRoles} className="mt-4" size="sm">
                                Try Again
                            </Button>
                        </div>
                    )}
 
                    {/* Loading State */}
                    {loading && !data && (
                        <div className="flex items-center justify-center py-12">
                            <RefreshCw className="h-8 w-8 animate-spin text-muted-foreground" />
                        </div>
                    )}
 
                    {/* Table */}
                    {!loading && data && (
                        <>
                            <RolesTable
                                roles={data.items}
                                onEdit={handleEdit}
                                onDelete={handleDelete}
                            />
 
                            {/* Summary */}
                            <p className="text-sm text-muted-foreground">
                                Showing {data.items.length} role{data.items.length !== 1 ? 's' : ''}
                            </p>
                        </>
                    )}
 
                    {/* Empty State */}
                    {!loading && data && data.items.length === 0 && (
                        <div className="text-center py-12">
                            <Shield className="h-12 w-12 mx-auto mb-4 text-muted-foreground opacity-50" />
                            <h3 className="text-lg font-medium mb-2">No roles yet</h3>
                            <p className="text-muted-foreground mb-4">
                                Get started by creating your first role
                            </p>
                            <Button onClick={() => setCreateDialogOpen(true)}>
                                <Plus className="h-4 w-4 mr-2" />
                                Create Role
                            </Button>
                        </div>
                    )}
                </CardContent>
            </Card>
 
            {/* Dialogs */}
            <CreateRoleDialog
                open={createDialogOpen}
                onOpenChange={setCreateDialogOpen}
                onSubmit={handleCreateRole}
            />
 
            <EditRoleDialog
                open={editDialogOpen}
                onOpenChange={setEditDialogOpen}
                role={selectedRole}
                onSubmit={handleUpdateRole}
            />
 
            <DeleteRoleDialog
                open={deleteDialogOpen}
                onOpenChange={setDeleteDialogOpen}
                role={selectedRole}
                onConfirm={handleDeleteRole}
            />
        </div>
    );
}