mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 11:46:27 +00:00
Improve RBAC management and permissions UI
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.
This commit is contained in:
@@ -64,7 +64,7 @@ const ConfirmAsync: FC<Props> = ({ title, description, onConfirm, okText, cancel
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
{okText || 'Continue'}
|
||||
{okText || 'Delete'}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
|
||||
@@ -15,7 +15,7 @@ import { Icons } from "@/components/Icons";
|
||||
import { type IMenu } from "./type";
|
||||
import { toast } from "sonner";
|
||||
import { TABLER_ICON_OPTIONS } from "@/lib/tabler-icons";
|
||||
import RbacBadge from "@/components/ui/rbac-badge";
|
||||
import RbacBadge from "@/components/rbac/rbac-badge";
|
||||
import { Plus, X } from "lucide-react";
|
||||
import PermissionForm from "../../action-permissions/component/Form";
|
||||
|
||||
@@ -48,7 +48,8 @@ function getDescendantIds(targetId: number, menus: IMenu[]): Set<number> {
|
||||
export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props) {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [selectedIcon, setSelectedIcon] = useState<string>('');
|
||||
const [showPermissionForm, setShowPermissionForm] = useState(false)
|
||||
const [isAdminMenu, setIsAdminMenu] = useState<boolean>(false);
|
||||
const [showPermissionForm, setShowPermissionForm] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
const router = useRouter();
|
||||
const axios = useAxiosAuth();
|
||||
@@ -89,24 +90,27 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
return {
|
||||
label,
|
||||
value: item.id!,
|
||||
isAdmin: item.is_admin_menu,
|
||||
isAdmin: Boolean(item.is_admin_menu),
|
||||
};
|
||||
});
|
||||
}, [menuList, isUpdate, data]);
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
const adminStatus = Boolean(data.is_admin_menu);
|
||||
setIsAdminMenu(adminStatus);
|
||||
form.setFieldsValue({
|
||||
...data,
|
||||
menu_parent_id: data.menu_parent_id ?? null,
|
||||
is_admin_menu: data.is_admin_menu ?? false,
|
||||
is_menu_admin: adminStatus,
|
||||
action: data.action ?? [],
|
||||
});
|
||||
setSelectedIcon(data.icon || '');
|
||||
} else {
|
||||
form.resetFields();
|
||||
setIsAdminMenu(false);
|
||||
form.setFieldsValue({
|
||||
is_admin_menu: false,
|
||||
is_menu_admin: false,
|
||||
menu_parent_id: null,
|
||||
action: [],
|
||||
});
|
||||
@@ -120,7 +124,7 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
path_url: values.path_url?.trim(),
|
||||
description: values.description?.trim() || '',
|
||||
icon: values.icon?.trim() || '',
|
||||
is_admin_menu: Boolean(values.is_admin_menu),
|
||||
is_admin_menu: Boolean(isAdminMenu),
|
||||
menu_parent_id: values.menu_parent_id ? Number(values.menu_parent_id) : null,
|
||||
action: Array.isArray(values.action) ? values.action : [],
|
||||
};
|
||||
@@ -141,7 +145,7 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
return;
|
||||
}
|
||||
|
||||
toast.success('Menu successfully created.');
|
||||
toast.success('Menu created successfully.');
|
||||
router.refresh();
|
||||
if (onCancel) onCancel();
|
||||
} catch (error: any) {
|
||||
@@ -164,7 +168,7 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
return;
|
||||
}
|
||||
|
||||
toast.success('Menu successfully updated.');
|
||||
toast.success('Menu updated successfully.');
|
||||
router.refresh();
|
||||
if (onCancel) onCancel();
|
||||
} catch (error: any) {
|
||||
@@ -180,10 +184,10 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
<Form form={form} onFinish={handleSubmit} layout="vertical">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="dark:text-white">
|
||||
{isUpdate ? 'Edit Menu' : 'Create New Menu'}
|
||||
{isUpdate ? 'Edit Menu' : 'Create Menu'}
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
Configure menu routing, hierarchy, tabler icon, and assigned permissions.
|
||||
Configure menu routing, hierarchy, icon, and assigned permissions.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
@@ -198,20 +202,24 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
|
||||
<Form.Item
|
||||
name="path_url"
|
||||
label="Path URL"
|
||||
rules={[{ required: true, message: 'Path URL is required!' }]}
|
||||
label="Menu Path"
|
||||
rules={[{ required: true, message: 'Menu path is required!' }]}
|
||||
>
|
||||
<Input className="input font-mono" placeholder="e.g. /administration/user" />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item name="menu_parent_id" label="Parent Menu" tooltip="Select parent menu or leave as Root menu (null)">
|
||||
<Select placeholder="Root Menu (No Parent)" allowClear
|
||||
optionFilterProp="children" getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
||||
<Form.Item
|
||||
name="menu_parent_id"
|
||||
label="Parent Menu"
|
||||
tooltip="Select parent menu or leave as Root menu (None)"
|
||||
>
|
||||
<Select placeholder="Root Menu (None)" allowClear
|
||||
showSearch={{ filterOption: (input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase()) }}
|
||||
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
||||
options={[
|
||||
{ label: '— Root Menu (None) —', value: null as any },
|
||||
{ label: '(None) - Root Menu', value: null as any },
|
||||
...validParentOptions.map(p => ({
|
||||
label: `${p.label} ${p.isAdmin ? ' (Admin)' : ''}`,
|
||||
label: `${p.label} ${p.isAdmin ? '(Admin)' : ''}`,
|
||||
value: p.value,
|
||||
})),
|
||||
]}
|
||||
@@ -231,8 +239,8 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
</span>
|
||||
}
|
||||
>
|
||||
<Select placeholder="Select Tabler Icon" allowClear
|
||||
optionFilterProp="children" getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
||||
<Select placeholder="Select Tabler Icon" allowClear showSearch
|
||||
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
||||
onChange={(val) => setSelectedIcon(val || '')}
|
||||
options={TABLER_ICON_OPTIONS.map(opt => ({
|
||||
label: (
|
||||
@@ -247,39 +255,56 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
<div className="p-3 rounded-lg border border-zinc-200 dark:border-zinc-800 bg-zinc-50/50 dark:bg-zinc-900/40 flex items-center justify-between">
|
||||
{/* Admin Menu Switch with HIS Primary Color */}
|
||||
<div className="p-3.5 rounded-lg border border-zinc-200 dark:border-zinc-800 bg-zinc-50/50 dark:bg-zinc-900/40 flex items-center justify-between">
|
||||
<div>
|
||||
<span className="text-sm font-medium text-zinc-900 dark:text-zinc-100 block">
|
||||
Admin Panel Menu
|
||||
<span className="text-sm font-semibold text-zinc-900 dark:text-zinc-100 block">
|
||||
Admin Menu
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground block mt-0.5">
|
||||
When enabled, this menu is treated as part of the Admin Panel and governed by Allow Admin Panel permission.
|
||||
When enabled, this menu belongs to the Admin Panel and requires admin panel privileges.
|
||||
</span>
|
||||
</div>
|
||||
<Form.Item name="is_admin_menu" valuePropName="checked" className="mb-0">
|
||||
<Switch />
|
||||
<Form.Item name="is_menu_admin" valuePropName="checked" className="mb-0">
|
||||
<Switch
|
||||
checked={isAdminMenu}
|
||||
onChange={(checked) => {
|
||||
setIsAdminMenu(checked);
|
||||
form.setFieldValue('is_menu_admin', checked);
|
||||
}}
|
||||
style={{
|
||||
backgroundColor: isAdminMenu ? '#EA512C' : undefined,
|
||||
}}
|
||||
className="[&.ant-switch-checked]:bg-[#EA512C]! [&.ant-switch-checked:hover]:bg-[#EA512C]/90!"
|
||||
/>
|
||||
</Form.Item>
|
||||
</div>
|
||||
|
||||
{/* Action / Permissions Multi-select Container */}
|
||||
<div className="flex items-center gap-2">
|
||||
<Form.Item name="action" label="Action / Permissions" tooltip="Select which action permissions are available on this menu" className="w-full">
|
||||
<Form.Item name="action" label="Action Permissions"
|
||||
tooltip="Select action permissions available on this menu"
|
||||
className="w-full"
|
||||
rules={[{ required: true, message: 'Please add aleast one action!' }]}
|
||||
>
|
||||
<Select mode="multiple" allowClear loading={permissionLoading} options={availablePermissions.map(p => ({ label: p, value: p }))}
|
||||
placeholder={permissionLoading ? "Loading permissions..." : "Select permissions..."}
|
||||
showSearch={{ filterOption: (input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase()) }}
|
||||
placeholder={permissionLoading ? "Loading permissions..." : "Assign permissions to this menu..."}
|
||||
className="h-max"
|
||||
optionFilterProp="children" getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
||||
className="w-full min-h-[42px] [&_.ant-select-selector]:min-h-[42px]! [&_.ant-select-selector]:h-auto! [&_.ant-select-selector]:max-h-40! [&_.ant-select-selector]:overflow-y-auto! [&_.ant-select-selector]:p-1.5!"
|
||||
getPopupContainer={(triggerNode: any) => triggerNode.parentNode}
|
||||
tagRender={(props) => {
|
||||
const { value, closable, onClose } = props;
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 mr-1.5 my-0.5">
|
||||
<span className="inline-flex items-center gap-1 mr-1.5 my-1">
|
||||
<RbacBadge permission={String(value)} size="sm" />
|
||||
{closable && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="text-xs text-muted-foreground hover:text-rose-500 ml-0.5"
|
||||
aria-label={`Remove permission ${value}`}
|
||||
className="text-xs text-muted-foreground hover:text-rose-500 rounded p-0.5 hover:bg-rose-50 dark:hover:bg-rose-950/40 transition-colors"
|
||||
>
|
||||
<X size={10} />
|
||||
<X size={11} />
|
||||
</button>
|
||||
)}
|
||||
</span>
|
||||
@@ -287,11 +312,18 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Button variant='outline' className="mt-6" title="Add Permission" type="button" onClick={() => setShowPermissionForm(true)}><Plus /></Button>
|
||||
<div className="pt-6">
|
||||
<Button variant='outline' size='icon' type="button" onClick={() => setShowPermissionForm(true)}><Plus /></Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Form.Item name="description" label="Description">
|
||||
<Input.TextArea className="input" id="description" rows={3} placeholder="Describe the purpose of this menu" />
|
||||
<Form.Item name="description" label="Description" className="mb-0">
|
||||
<Input.TextArea
|
||||
className="input"
|
||||
id="description"
|
||||
rows={3}
|
||||
placeholder="Describe the purpose and scope of this menu..."
|
||||
/>
|
||||
</Form.Item>
|
||||
</div>
|
||||
|
||||
@@ -299,23 +331,25 @@ export default function MenuForm({ data, menuList, isUpdate, onCancel }: Props)
|
||||
<Button variant="outline" type="button" onClick={onCancel} disabled={isLoading}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={isLoading}>
|
||||
<Button type="submit" disabled={isLoading} className="bg-his hover:bg-his/90 text-white font-medium">
|
||||
{isLoading && (
|
||||
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
||||
<Icons.spinner className="mr-1.5 h-4 w-4 animate-spin" />
|
||||
)}
|
||||
Save Menu
|
||||
{isUpdate ? 'Save Changes' : 'Save'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
|
||||
{showPermissionForm ?
|
||||
{showPermissionForm && (
|
||||
<PermissionForm
|
||||
isUpdate={false}
|
||||
onCancel={() => { setShowPermissionForm(false); mutate() }}
|
||||
onCancel={() => {
|
||||
setShowPermissionForm(false);
|
||||
mutate();
|
||||
}}
|
||||
/>
|
||||
: null
|
||||
}
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
),
|
||||
},
|
||||
{
|
||||
title: 'Path URL',
|
||||
title: 'Menu Path',
|
||||
dataIndex: 'path_url',
|
||||
key: 'path_url',
|
||||
render: (path: string) => (
|
||||
@@ -156,29 +156,30 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
},
|
||||
{
|
||||
title: 'Type',
|
||||
dataIndex: 'is_admin_menu',
|
||||
key: 'is_admin_menu',
|
||||
width: 120,
|
||||
dataIndex: 'is_menu_admin',
|
||||
key: 'is_menu_admin',
|
||||
width: 140,
|
||||
align: 'center',
|
||||
render: (isAdmin?: boolean) => (
|
||||
isAdmin ? (
|
||||
render: (_val: any, record: IMenu) => {
|
||||
const isAdmin = Boolean(record.is_admin_menu);
|
||||
return isAdmin ? (
|
||||
<span className="inline-flex items-center px-2 py-0.5 rounded-sm text-[11px] font-semibold bg-indigo-500/10 text-indigo-600 border border-indigo-500/20 dark:bg-indigo-500/20 dark:text-indigo-300 dark:border-indigo-500/30">
|
||||
Admin Panel
|
||||
Admin Menu
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center px-2 py-0.5 rounded-sm text-[11px] font-semibold bg-zinc-500/10 text-zinc-600 border border-zinc-500/20 dark:bg-zinc-500/20 dark:text-zinc-400 dark:border-zinc-500/30">
|
||||
General
|
||||
Non-Admin Menu
|
||||
</span>
|
||||
)
|
||||
),
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'Actions / Permissions',
|
||||
title: 'Permissions',
|
||||
dataIndex: 'action',
|
||||
key: 'action',
|
||||
render: (actions?: string[]) => {
|
||||
if (!actions || actions.length === 0) {
|
||||
return <span className="text-xs text-muted-foreground italic">No action assigned</span>;
|
||||
return <span className="text-xs text-muted-foreground italic">No permissions assigned</span>;
|
||||
}
|
||||
return (
|
||||
<div className="flex flex-wrap gap-1 max-w-md">
|
||||
@@ -200,7 +201,7 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
),
|
||||
},
|
||||
{
|
||||
title: 'Action',
|
||||
title: 'Actions',
|
||||
key: 'row_action',
|
||||
width: 80,
|
||||
align: 'center',
|
||||
@@ -230,7 +231,7 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<ConfirmAsync
|
||||
title="Delete Menu?"
|
||||
title="Delete Menu"
|
||||
description={`Are you sure you want to delete menu "${record.name}"? If this menu has child menus or permissions assigned, access may be affected.`}
|
||||
onConfirm={() => handleDelete(record.id!)}
|
||||
onOkSuccess={() => router.refresh()}
|
||||
@@ -276,7 +277,7 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
}}
|
||||
>
|
||||
<PlusCircledIcon className="mr-2 h-4 w-4" />
|
||||
Create New Menu
|
||||
Create Menu
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -289,9 +290,9 @@ const ComponentMenuPage: FC<Props> = ({ data }) => {
|
||||
emptyText: (
|
||||
<div className="py-8 text-center text-muted-foreground">
|
||||
<i className="ti ti-category text-3xl mb-2 text-zinc-400 block" />
|
||||
<p className="text-sm font-medium">No menus registered</p>
|
||||
<p className="text-sm font-medium">No menus found</p>
|
||||
<p className="text-xs text-zinc-400 mt-1">
|
||||
{searchText ? 'No menu matching your search.' : 'Click "Create New Menu" to register your first navigation menu.'}
|
||||
{searchText ? 'No menu matching your search.' : 'Click "Create Menu" to register your first navigation menu.'}
|
||||
</p>
|
||||
</div>
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user