mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
6ffe7746c8
Unify interactive and read-only role permission rows, refine role and menu forms and labels, and correct the access-permissions sidebar route. Update role payloads and admin-menu handling.
190 lines
9.6 KiB
TypeScript
190 lines
9.6 KiB
TypeScript
import { FC } from 'react';
|
|
import { type Metadata } from 'next';
|
|
import Link from 'next/link';
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import fetchData from '@/lib/fetchData';
|
|
import { Button } from '@/components/ui/button';
|
|
import { type IRole, type IRoleDetail } from '../component/type';
|
|
import RolePermissionRow from '@/components/rbac/role-permission-row';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export const metadata: Metadata = {
|
|
title: "Role Permission Details | Admin",
|
|
description: "Detailed overview of role permissions and authorized menu actions",
|
|
};
|
|
|
|
type Props = {
|
|
params: Promise<{ id: string }>;
|
|
};
|
|
|
|
async function getRoleData(id: string): Promise<IRole | undefined> {
|
|
try {
|
|
const res = await fetchData(`/role/${id}`, 'no-cache');
|
|
if (res) {
|
|
if (res.role) return res.role;
|
|
return res;
|
|
}
|
|
} catch {
|
|
try {
|
|
const list = await fetchData('/role', 'no-cache');
|
|
const roles = Array.isArray(list) ? list : (list?.role || list?.roles || []);
|
|
return roles.find((r: any) => String(r.id) === String(id));
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
const UserRoleDetailPage: FC<Props> = async ({ params }) => {
|
|
const { id } = await params;
|
|
const data: IRole | undefined = await getRoleData(id);
|
|
|
|
const isAdminAllowed = Boolean(data?.allow_admin_panel ?? data?.allow_admin);
|
|
const roleDetails: IRoleDetail[] = Array.isArray(data?.role_detail)
|
|
? data.role_detail
|
|
: Array.isArray(data?.privilege)
|
|
? data.privilege.map((p: any) => ({
|
|
menu_id: p.menu_id || p.id,
|
|
menu_name: p.menu_name || p.name,
|
|
menu_path: p.menu_path || p.path || '#',
|
|
is_admin_menu: Boolean(p.is_admin_menu ?? p.is_menu_admin),
|
|
action: Array.isArray(p.action) ? p.action : [],
|
|
}))
|
|
: [];
|
|
|
|
return (
|
|
<div className="flex flex-col space-y-6 pb-12">
|
|
{/* Header & Overview Card */}
|
|
<Card>
|
|
<CardHeader className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
|
|
<div className="flex items-center space-x-4">
|
|
<Link href="/admin/rbac/user-role">
|
|
<Button variant="outline" size="icon" className="h-9 w-9">
|
|
<i className="ti ti-arrow-left text-lg" />
|
|
</Button>
|
|
</Link>
|
|
<div>
|
|
<CardTitle className="text-xl">Role Permission Details</CardTitle>
|
|
<CardDescription className="mt-1">
|
|
Detailed overview of role permissions and authorized menu actions
|
|
</CardDescription>
|
|
</div>
|
|
</div>
|
|
<Link href={`/admin/rbac/user-role/edit/${id}`}>
|
|
<Button className="bg-his hover:bg-his/90 text-white font-medium">
|
|
<i className="ti ti-edit mr-2 text-base" /> Edit Role
|
|
</Button>
|
|
</Link>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 p-4 rounded-lg bg-zinc-50/60 dark:bg-zinc-900/40 border border-zinc-200 dark:border-zinc-800">
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground uppercase tracking-wider font-semibold">
|
|
Role Code
|
|
</span>
|
|
<span className="font-mono font-bold text-sm text-zinc-900 dark:text-zinc-100">
|
|
{data?.code || `ROLE-${id}`}
|
|
</span>
|
|
</div>
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground uppercase tracking-wider font-semibold">
|
|
Role Name
|
|
</span>
|
|
<span className="font-bold text-sm text-zinc-900 dark:text-zinc-100 uppercase">
|
|
{data?.name || '-'}
|
|
</span>
|
|
</div>
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground uppercase tracking-wider font-semibold">
|
|
Admin Panel Access
|
|
</span>
|
|
<div>
|
|
{isAdminAllowed ? (
|
|
<span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-emerald-500/10 text-emerald-600 border border-emerald-500/20 dark:bg-emerald-500/20 dark:text-emerald-300">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-emerald-500" />
|
|
Web CMS & Admin Panel
|
|
</span>
|
|
) : (
|
|
<span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-zinc-500/10 text-zinc-500 border border-zinc-500/20 dark:bg-zinc-500/20 dark:text-zinc-400">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-zinc-400" />
|
|
General Access Only
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-col space-y-1">
|
|
<span className="text-xs text-muted-foreground uppercase tracking-wider font-semibold">
|
|
Status
|
|
</span>
|
|
<div>
|
|
<span className="inline-flex items-center gap-1.5 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-green-500/10 text-green-600 border border-green-500/20">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-green-500" />
|
|
{data?.status?.name || 'Active'}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-col space-y-1 md:col-span-2">
|
|
<span className="text-xs text-muted-foreground uppercase tracking-wider font-semibold">
|
|
Description
|
|
</span>
|
|
<span className="text-sm text-zinc-700 dark:text-zinc-300">
|
|
{data?.description || 'No description provided.'}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Menu Permissions List */}
|
|
<Card>
|
|
<CardHeader className="pb-3 border-b border-zinc-200 dark:border-zinc-800">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
<i className="ti ti-checklist text-lg text-his" />
|
|
<CardTitle className="text-base font-semibold">
|
|
Authorized Menu Permissions ({roleDetails.length} Menus Allowed)
|
|
</CardTitle>
|
|
</div>
|
|
</div>
|
|
</CardHeader>
|
|
<CardContent className="p-0">
|
|
{roleDetails.length > 0 ? (
|
|
<div className="divide-y divide-zinc-200/80 dark:divide-zinc-800/80">
|
|
{roleDetails.map((detail, idx) => (
|
|
<RolePermissionRow
|
|
key={detail.menu_id || idx}
|
|
node={{
|
|
id: detail.menu_id,
|
|
name: detail.menu_name,
|
|
path_url: detail.menu_path,
|
|
is_menu_admin: detail.is_admin_menu,
|
|
level: 0,
|
|
action: detail.action,
|
|
}}
|
|
mode="readonly"
|
|
index={idx}
|
|
selectedActions={detail.action || []}
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="py-12 text-center text-muted-foreground">
|
|
<i className="ti ti-lock-open text-3xl mb-2 text-zinc-400 block" />
|
|
<p className="text-sm font-medium">No menus or permissions have been configured for this role.</p>
|
|
<Link href={`/admin/rbac/user-role/edit/${id}`} className="mt-3 inline-block">
|
|
<Button size="sm" variant="outline">
|
|
Configure Permissions Now
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default UserRoleDetailPage;
|