79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
import React, { useMemo } from 'react';
|
|
import { Permission } from '../../types';
|
|
import { useAppContext } from '../../context/AppContext';
|
|
|
|
interface PermissionManagerProps {
|
|
selectedRoleId: string;
|
|
}
|
|
|
|
const PermissionManager: React.FC<PermissionManagerProps> = ({ selectedRoleId }) => {
|
|
const { permissions, rolePermissions, roles, onTogglePermission } = useAppContext();
|
|
|
|
const selectedRole = roles.find((r) => r.id === selectedRoleId);
|
|
|
|
const groupedPermissions = useMemo(() => {
|
|
return permissions.reduce(
|
|
(acc, permission) => {
|
|
const category = permission.category;
|
|
if (!acc[category]) {
|
|
acc[category] = [];
|
|
}
|
|
acc[category].push(permission);
|
|
return acc;
|
|
},
|
|
{} as Record<string, Permission[]>
|
|
);
|
|
}, [permissions]);
|
|
|
|
const hasPermission = (permissionId: string) => {
|
|
return rolePermissions.some(
|
|
(rp) => rp.roleId === selectedRoleId && rp.permissionId === permissionId
|
|
);
|
|
};
|
|
|
|
if (!selectedRole) return null;
|
|
|
|
return (
|
|
<div>
|
|
<h2 className="text-xl font-bold text-gray-800">
|
|
Hak Akses untuk Peran: <span className="text-red-600">{selectedRole.name}</span>
|
|
</h2>
|
|
<p className="text-sm text-gray-500 mb-6">
|
|
Pilih hak akses yang akan diberikan untuk peran ini.
|
|
</p>
|
|
|
|
<div className="space-y-6 max-h-[calc(100vh-350px)] overflow-y-auto pr-3">
|
|
{/* FIX: Explicitly type '[category, perms]' to resolve 'perms' being 'unknown'. */}
|
|
{Object.entries(groupedPermissions).map(([category, perms]: [string, Permission[]]) => (
|
|
<div key={category} className="border border-gray-200 rounded-lg">
|
|
<h3 className="text-md font-semibold text-gray-700 bg-gray-100 p-3 rounded-t-lg">
|
|
{category}
|
|
</h3>
|
|
<div className="p-4 space-y-3">
|
|
{perms.map((permission) => (
|
|
<label
|
|
key={permission.id}
|
|
className="flex items-start space-x-3 p-2 rounded-md hover:bg-gray-50 cursor-pointer"
|
|
>
|
|
<input
|
|
type="checkbox"
|
|
className="h-5 w-5 rounded border-gray-300 text-red-600 focus:ring-red-500 mt-0.5"
|
|
checked={hasPermission(permission.id)}
|
|
onChange={() => onTogglePermission(selectedRoleId, permission.id)}
|
|
/>
|
|
<div>
|
|
<span className="font-medium text-gray-800">{permission.action}</span>
|
|
<p className="text-xs text-gray-500">{permission.description}</p>
|
|
</div>
|
|
</label>
|
|
))}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default PermissionManager;
|