mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
751b28fab2
Adds permission, menu, and role administration with hierarchical access controls, plus admin breadcrumbs and a redirect from the legacy user-role page. Remove `credentials.md` before committing; it contains server credentials.
164 lines
6.3 KiB
TypeScript
164 lines
6.3 KiB
TypeScript
import { useRouter } from "next/navigation";
|
|
import { useEffect, useState } from "react";
|
|
import { Form, Input } from "antd";
|
|
import useAxiosAuth from "@/lib/axios/useAxiosAuth";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Icons } from "@/components/Icons";
|
|
import { type IPermission } from "./type";
|
|
import { toast } from "sonner";
|
|
import RbacBadge from "@/components/ui/rbac-badge";
|
|
|
|
type Props = {
|
|
isUpdate?: boolean;
|
|
data?: IPermission;
|
|
onCancel?: () => any;
|
|
};
|
|
|
|
export default function PermissionForm({ data, isUpdate, onCancel }: Props) {
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [previewName, setPreviewName] = useState('');
|
|
const [form] = Form.useForm();
|
|
const router = useRouter();
|
|
const axios = useAxiosAuth();
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.setFieldsValue(data);
|
|
setPreviewName(data.name || '');
|
|
} else {
|
|
form.resetFields();
|
|
setPreviewName('');
|
|
}
|
|
}, [data, form]);
|
|
|
|
const handleSubmit = (value: { name: string; description?: string }) => {
|
|
const payload = {
|
|
name: value.name.trim().toUpperCase(),
|
|
description: value.description?.trim() || '',
|
|
};
|
|
|
|
!isUpdate ? handleCreate(payload) : handleUpdate(payload);
|
|
};
|
|
|
|
const handleCreate = async (payload: { name: string; description?: string }) => {
|
|
try {
|
|
setIsLoading(true);
|
|
const res = await axios.post('/permission', payload);
|
|
setIsLoading(false);
|
|
|
|
if (res.data?.code && res.data.code !== '00' && res.data.code !== 200) {
|
|
toast.error('Failed to create permission', {
|
|
description: res.data?.message || 'Server returned an error',
|
|
});
|
|
return;
|
|
}
|
|
|
|
toast.success('Permission successfully created.');
|
|
router.refresh();
|
|
if (onCancel) onCancel();
|
|
} catch (error: any) {
|
|
setIsLoading(false);
|
|
const msg = error?.response?.data?.message || error?.message || 'Failed to create permission';
|
|
toast.error('Failed to create permission', { description: msg });
|
|
}
|
|
};
|
|
|
|
const handleUpdate = async (payload: { name: string; description?: string }) => {
|
|
try {
|
|
setIsLoading(true);
|
|
const res = await axios.put(`/permission/${data?.id}`, payload);
|
|
setIsLoading(false);
|
|
|
|
if (res.data?.code && res.data.code !== '00' && res.data.code !== 200) {
|
|
toast.error('Failed to update permission', {
|
|
description: res.data?.message || 'Server returned an error',
|
|
});
|
|
return;
|
|
}
|
|
|
|
toast.success('Permission successfully updated.');
|
|
router.refresh();
|
|
if (onCancel) onCancel();
|
|
} catch (error: any) {
|
|
setIsLoading(false);
|
|
const msg = error?.response?.data?.message || error?.message || 'Failed to update permission';
|
|
toast.error('Failed to update permission', { description: msg });
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open>
|
|
<DialogContent>
|
|
<Form form={form} onFinish={handleSubmit} layout="vertical">
|
|
<DialogHeader>
|
|
<DialogTitle className="dark:text-white">
|
|
{isUpdate ? 'Edit Access Permission' : 'Create Access Permission'}
|
|
</DialogTitle>
|
|
<DialogDescription>
|
|
Define an actionable permission identifier used by role-based access control.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<div className="grid mt-6 space-y-0">
|
|
<Form.Item
|
|
name="name"
|
|
label="Permission Name"
|
|
rules={[
|
|
{ required: true, message: 'Permission name is required!' },
|
|
{
|
|
pattern: /^[A-Z0-9_]+$/,
|
|
message: 'Only uppercase letters, numbers, and underscores are allowed (e.g. APPROVE_ATTENDANCE)',
|
|
},
|
|
]}
|
|
>
|
|
<Input
|
|
className="input font-mono uppercase"
|
|
id="name"
|
|
placeholder="APPROVE_ATTENDANCE"
|
|
onChange={(e) => setPreviewName(e.target.value.toUpperCase())}
|
|
/>
|
|
</Form.Item>
|
|
|
|
{previewName && (
|
|
<div className="flex items-center gap-2 -mt-2 mb-2 p-2.5 rounded-md bg-zinc-50 dark:bg-zinc-900 border border-zinc-200 dark:border-zinc-800">
|
|
<span className="text-xs text-muted-foreground font-medium">Badge Preview:</span>
|
|
<RbacBadge permission={previewName} />
|
|
</div>
|
|
)}
|
|
|
|
<Form.Item name="description" label="Description">
|
|
<Input.TextArea
|
|
className="input"
|
|
id="description"
|
|
rows={3}
|
|
placeholder="Permission description or usage scope"
|
|
/>
|
|
</Form.Item>
|
|
</div>
|
|
|
|
<DialogFooter className="mt-6">
|
|
<Button variant="outline" type="button" onClick={onCancel} disabled={isLoading}>
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" disabled={isLoading}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
Save
|
|
</Button>
|
|
</DialogFooter>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|