import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useGetRoleById, useGetRolePermissions } from "@/hooks/queries"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Shield, ArrowLeft, Check, X } from "lucide-react"; import type { PermissionOnRole } from "@/types/permission"; export const Route = createFileRoute("/_auth/user/role/$roleId/")({ head: () => ({ meta: [{ title: "Quyền của người dùng | AccessControl" }] }), component: ViewRolePermissionsComponent, loader: async ({ context, params }) => { context.breadcrumbs = [ { title: "Quản lý tài khoản", path: "#" }, { title: "Danh sách người dùng", path: "/user" }, { title: "Quyền của người dùng", path: `/user/role/${params.roleId}` } ]; }, }); function ViewRolePermissionsComponent() { const { roleId } = Route.useParams(); const navigate = useNavigate(); const roleIdNum = parseInt(roleId, 10); const { data: role, isLoading: roleLoading } = useGetRoleById(roleIdNum); const { data: permissions = [], isLoading: permissionsLoading } = useGetRolePermissions(roleIdNum); const isLoading = roleLoading || permissionsLoading; // Group permissions by parent const groupedPermissions = (permissions as PermissionOnRole[]).reduce((acc, permission) => { if (permission.parentId === null) { if (!acc[permission.permisionId]) { acc[permission.permisionId] = { parent: permission, children: [] }; } else { acc[permission.permisionId].parent = permission; } } else { if (!acc[permission.parentId]) { acc[permission.parentId] = { parent: null as any, children: [] }; } acc[permission.parentId].children.push(permission); } return acc; }, {} as Record); if (isLoading) { return (
); } return (
Quyền hạn của Role: {role?.roleName || `#${roleId}`} Danh sách các quyền được gán cho role này {role?.priority !== undefined && ( (Độ ưu tiên: {role.priority}) )} {permissions.length === 0 ? (
Không có quyền nào được gán cho role này
) : (
{Object.values(groupedPermissions).map(({ parent, children }) => (
{parent?.permissionName || "Allow all"} {parent?.permissionCode}
{(parent?.isChecked === 1 || parent === null) ? ( Đã bật ) : ( Đã tắt )}
{children.length > 0 && (
{children.map((child) => (
{child.permissionName} {child.permissionCode}
{child.isChecked === 1 ? ( ) : ( )}
))}
)}
))}
)}
); }