UNPKG

@dbs-portal/module-identity

Version:

Identity management module for user and role management

70 lines 4.62 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /** * Role Create Component */ import React from 'react'; import { Card, Form, Input, Button, Switch, Select, Alert, Typography, Divider, message } from 'antd'; import { SafetyOutlined, InfoCircleOutlined } from '@ant-design/icons'; import { useCreateRole } from '../hooks'; const { Title, Text } = Typography; const { TextArea } = Input; const { Option } = Select; // Common permissions list (this would typically come from an API) const COMMON_PERMISSIONS = [ 'Users.Read', 'Users.Create', 'Users.Update', 'Users.Delete', 'Roles.Read', 'Roles.Create', 'Roles.Update', 'Roles.Delete', 'Files.Read', 'Files.Upload', 'Files.Download', 'Files.Delete', 'Admin.Access', 'Admin.Settings', 'Reports.View', 'Reports.Export' ]; export const RoleCreate = ({ onSubmit, loading: externalLoading, error: externalError, className }) => { const [form] = Form.useForm(); const createRoleMutation = useCreateRole(); const isLoading = externalLoading || createRoleMutation.isPending; const error = externalError || createRoleMutation.error?.message; const handleSubmit = async (values) => { try { if (onSubmit) { onSubmit(values); } else { await createRoleMutation.mutateAsync({ name: values.name, displayName: values.displayName, description: values.description, isDefault: values.isDefault ?? false, permissions: values.permissions || [] }); message.success('Role created successfully!'); form.resetFields(); } } catch (err) { console.error('Failed to create role:', err); } }; return (_jsxs(Card, { className: className, children: [_jsx(Title, { level: 2, children: "Create New Role" }), _jsx(Text, { type: "secondary", children: "Create a new role with specific permissions and settings." }), error && (_jsx(Alert, { message: "Role Creation Failed", description: error, type: "error", showIcon: true, style: { marginTop: 16, marginBottom: 16 } })), _jsxs(Form, { form: form, name: "createRole", onFinish: handleSubmit, layout: "vertical", size: "large", style: { marginTop: 24 }, initialValues: { isDefault: false, }, children: [_jsx(Form.Item, { name: "name", label: "Role Name", rules: [ { required: true, message: 'Please enter the role name!' }, { min: 2, message: 'Role name must be at least 2 characters!' }, { pattern: /^[a-zA-Z0-9_-]+$/, message: 'Role name can only contain letters, numbers, underscores, and hyphens!' }, ], children: _jsx(Input, { prefix: _jsx(SafetyOutlined, {}), placeholder: "Enter role name (e.g., Administrator, Editor)" }) }), _jsx(Form.Item, { name: "displayName", label: "Display Name", rules: [ { min: 2, message: 'Display name must be at least 2 characters!' }, ], children: _jsx(Input, { prefix: _jsx(InfoCircleOutlined, {}), placeholder: "Enter display name (optional)" }) }), _jsx(Form.Item, { name: "description", label: "Description", children: _jsx(TextArea, { rows: 3, placeholder: "Enter role description (optional)" }) }), _jsx(Divider, { children: "Permissions" }), _jsx(Form.Item, { name: "permissions", label: "Assign Permissions", children: _jsx(Select, { mode: "multiple", placeholder: "Select permissions", allowClear: true, showSearch: true, filterOption: (input, option) => String(option?.children || '').toLowerCase().includes(input.toLowerCase()), children: COMMON_PERMISSIONS.map(permission => (_jsx(Option, { value: permission, children: permission }, permission))) }) }), _jsx(Divider, { children: "Settings" }), _jsx(Form.Item, { name: "isDefault", label: "Default Role", valuePropName: "checked", extra: "Default roles are automatically assigned to new users", children: _jsx(Switch, {}) }), _jsxs(Form.Item, { style: { marginTop: 32 }, children: [_jsx(Button, { type: "primary", htmlType: "submit", loading: isLoading, size: "large", style: { marginRight: 8 }, children: "Create Role" }), _jsx(Button, { size: "large", onClick: () => form.resetFields(), disabled: isLoading, children: "Reset" })] })] })] })); }; //# sourceMappingURL=RoleCreate.js.map