mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
279b642a7a
Enforce page access and action-level permissions across admin and task views, including exports and record actions. Move user creation to a client component behind server-side permission checks, standardize data fetching, and remove the deprecated user-role route.
217 lines
8.3 KiB
TypeScript
217 lines
8.3 KiB
TypeScript
'use client';
|
|
|
|
import { ChangeEvent, FC, useEffect, useState } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { MoreHorizontalIcon } from 'lucide-react';
|
|
import { Table } from 'antd';
|
|
import { TableProps, type ColumnsType } from 'antd/es/table';
|
|
import { PlusCircledIcon } from '@radix-ui/react-icons';
|
|
import useAxiosAuth from '@/lib/axios/useAxiosAuth';
|
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
|
import { Button } from '@/components/ui/button';
|
|
import { AlertDialog, AlertDialogTrigger } from '@/components/ui/alert-dialog';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Dialog } from '@/components/ui/dialog';
|
|
import PermissionForm from './Form';
|
|
import ConfirmAsync from './ConfirmAsync';
|
|
import { type IPermission } from './type';
|
|
import RbacBadge from '@/components/ui/rbac-badge';
|
|
|
|
type Props = {
|
|
data: IPermission[];
|
|
actions?: string[];
|
|
};
|
|
|
|
const ComponentPermissionPage: FC<Props> = ({ data, actions }) => {
|
|
const [filterData, setFilterData] = useState<IPermission[]>([]);
|
|
const [selectedData, setSelectedData] = useState<IPermission | undefined>(undefined);
|
|
const [showFormDialog, setShowFormDialog] = useState(false);
|
|
const [searchText, setSearchText] = useState('');
|
|
|
|
const axios = useAxiosAuth();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (data && Array.isArray(data)) {
|
|
setFilterData(data);
|
|
} else {
|
|
setFilterData([]);
|
|
}
|
|
}, [data]);
|
|
|
|
const handleDelete = (id: string | number) => {
|
|
return axios.delete(`/permission/${id}`).then((res) => res.data);
|
|
};
|
|
|
|
const handleResetForm = () => {
|
|
setSelectedData(undefined);
|
|
setShowFormDialog(false);
|
|
};
|
|
|
|
const handleSearch = (e: ChangeEvent<HTMLInputElement>) => {
|
|
const text = e.target.value;
|
|
setSearchText(text);
|
|
if (text && Array.isArray(data)) {
|
|
const lower = text.toLowerCase();
|
|
const filtered = data.filter(
|
|
(item) =>
|
|
item.name?.toLowerCase().includes(lower) ||
|
|
item.description?.toLowerCase().includes(lower)
|
|
);
|
|
setFilterData(filtered);
|
|
} else {
|
|
setFilterData(data || []);
|
|
}
|
|
};
|
|
|
|
const columns: ColumnsType<IPermission> = [
|
|
{
|
|
title: 'No',
|
|
dataIndex: 'key',
|
|
width: 70,
|
|
align: 'center',
|
|
render: (_text, _record, index) => (
|
|
<span className="text-xs text-muted-foreground font-medium">{index + 1}</span>
|
|
),
|
|
},
|
|
{
|
|
title: 'Permission Name',
|
|
dataIndex: 'name',
|
|
sorter: (a, b) => (a.name || '').localeCompare(b.name || ''),
|
|
render: (text: string) => (
|
|
<div className="flex items-center">
|
|
<RbacBadge permission={text} size="sm" />
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
title: 'Description',
|
|
dataIndex: 'description',
|
|
render: (text: string) => (
|
|
<span className="text-xs text-zinc-600 dark:text-zinc-300">
|
|
{text || '-'}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
title: 'Actions',
|
|
key: 'action',
|
|
width: 80,
|
|
align: 'center',
|
|
render: (_, record) => actions?.includes('UPDATE') || actions?.includes('EDIT') || actions?.includes('DELETE') && (
|
|
<AlertDialog>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="icon" className="h-8 w-8">
|
|
<MoreHorizontalIcon className="h-4 w-4" />
|
|
<span className="sr-only">Actions</span>
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
{(actions.includes('UPDATE') || actions.includes('EDIT')) && (
|
|
<DropdownMenuItem
|
|
onClick={() => {
|
|
setSelectedData(record);
|
|
setShowFormDialog(true);
|
|
}}
|
|
>
|
|
Edit Permission
|
|
</DropdownMenuItem>
|
|
)}
|
|
{(actions.includes('DELETE')) && (
|
|
<AlertDialogTrigger asChild>
|
|
<DropdownMenuItem className="text-rose-600 dark:text-rose-400">
|
|
Delete Permission
|
|
</DropdownMenuItem>
|
|
</AlertDialogTrigger>
|
|
)}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
<ConfirmAsync
|
|
title="Delete Permission"
|
|
description={`Are you sure you want to delete permission "${record.name}"? This may affect menus and roles using this permission.`}
|
|
onConfirm={() => handleDelete(record.id)}
|
|
onOkSuccess={() => router.refresh()}
|
|
/>
|
|
</AlertDialog>
|
|
),
|
|
},
|
|
];
|
|
|
|
const tableProps: TableProps<IPermission> = {
|
|
bordered: false,
|
|
size: 'small',
|
|
showHeader: true,
|
|
pagination: {
|
|
pageSizeOptions: [10, 20, 50, 100],
|
|
defaultPageSize: 10,
|
|
showSizeChanger: true,
|
|
showQuickJumper: true,
|
|
size: 'middle' as any,
|
|
},
|
|
scroll: { x: 'max-content' },
|
|
tableLayout: 'auto',
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className="flex flex-col space-y-4">
|
|
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3">
|
|
<div className="flex flex-1 items-center space-x-2 w-full sm:w-auto">
|
|
<Input
|
|
placeholder="Search permissions..."
|
|
className="h-8 w-full sm:w-64 md:w-72"
|
|
value={searchText}
|
|
onChange={handleSearch}
|
|
/>
|
|
</div>
|
|
{(!actions || actions.includes('CREATE')) && (
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
className="h-8 text-his hover:text-his hover:bg-his/10 font-semibold"
|
|
onClick={() => {
|
|
setSelectedData(undefined);
|
|
setShowFormDialog(true);
|
|
}}
|
|
>
|
|
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
|
Create Permission
|
|
</Button>
|
|
)}
|
|
</div>
|
|
|
|
<Table
|
|
{...tableProps}
|
|
rowKey="id"
|
|
columns={columns}
|
|
dataSource={filterData || []}
|
|
locale={{
|
|
emptyText: (
|
|
<div className="py-8 text-center text-muted-foreground">
|
|
<i className="ti ti-shield-lock text-3xl mb-2 text-zinc-400 block" />
|
|
<p className="text-sm font-medium">No permissions found</p>
|
|
<p className="text-xs text-zinc-400 mt-1">
|
|
{searchText ? 'Try adjusting your search criteria.' : 'Click "Create Permission" to add the first permission.'}
|
|
</p>
|
|
</div>
|
|
),
|
|
}}
|
|
/>
|
|
</div>
|
|
|
|
{showFormDialog ?
|
|
<PermissionForm
|
|
data={selectedData}
|
|
isUpdate={selectedData !== undefined}
|
|
onCancel={handleResetForm}
|
|
/>
|
|
: null
|
|
}
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default ComponentPermissionPage;
|
|
|