mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 17:06:29 +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:
@@ -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
|
||||
}
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user