196 lines
7.8 KiB
TypeScript
196 lines
7.8 KiB
TypeScript
import { useEffect } from 'react'
|
||
import {
|
||
Modal,
|
||
Stack,
|
||
TextInput,
|
||
Textarea,
|
||
Switch,
|
||
Group,
|
||
Button,
|
||
Text,
|
||
Loader,
|
||
Center,
|
||
Badge,
|
||
Paper,
|
||
} from '@mantine/core'
|
||
import { useForm } from '@mantine/form'
|
||
import {
|
||
useRole,
|
||
useCreateRole,
|
||
useUpdateRole
|
||
} from '@/features/roles/hooks/useRoles'
|
||
|
||
interface RoleEditModalProps {
|
||
roleId: number | null
|
||
opened: boolean
|
||
onClose: () => void
|
||
}
|
||
|
||
export default function RoleEditModal({
|
||
roleId,
|
||
opened,
|
||
onClose
|
||
}: RoleEditModalProps) {
|
||
const { data: role, isLoading: roleLoading } = useRole(roleId)
|
||
|
||
const createRole = useCreateRole()
|
||
const updateRole = useUpdateRole()
|
||
|
||
const isEditMode = roleId !== null
|
||
|
||
const form = useForm({
|
||
initialValues: {
|
||
name: '',
|
||
code: '',
|
||
description: '',
|
||
is_default: false,
|
||
is_director: false,
|
||
is_approver: false,
|
||
is_observer: false,
|
||
is_executor: false,
|
||
},
|
||
validate: {
|
||
name: (value: string | any[]) => (value.length < 2 ? 'Название должно содержать минимум 2 символа' : null),
|
||
code: (value: string | any[]) => {
|
||
if (value.length < 2) return 'Код должен содержать минимум 2 символа'
|
||
if (!/^[a-z0-9_]+$/.test(value as string)) return 'Код должен содержать только lowercase буквы, цифры и подчеркивания'
|
||
return null
|
||
},
|
||
},
|
||
})
|
||
|
||
// Заполняем форму данными роли
|
||
useEffect(() => {
|
||
if (role) {
|
||
form.setValues({
|
||
name: role.name || '',
|
||
code: role.code || '',
|
||
description: role.description || '',
|
||
is_default: role.is_default ?? false,
|
||
is_director: role.is_director ?? false,
|
||
is_approver: role.is_approver ?? false,
|
||
is_observer: role.is_observer ?? false,
|
||
is_executor: role.is_executor ?? false,
|
||
})
|
||
} else if (!isEditMode) {
|
||
form.reset()
|
||
}
|
||
}, [role, isEditMode])
|
||
|
||
const handleSubmit = async (values: typeof form.values) => {
|
||
if (isEditMode && roleId) {
|
||
// Режим редактирования
|
||
await updateRole.mutateAsync({
|
||
id: roleId,
|
||
...values,
|
||
})
|
||
} else {
|
||
// Режим создания
|
||
await createRole.mutateAsync(values)
|
||
}
|
||
onClose()
|
||
}
|
||
|
||
const isLoading = roleLoading
|
||
const isSubmitting = createRole.isPending || updateRole.isPending
|
||
|
||
return (
|
||
<Modal
|
||
opened={opened}
|
||
onClose={onClose}
|
||
title={
|
||
<Group>
|
||
<Text fw={700}>
|
||
{isEditMode ? 'Редактирование роли' : 'Создание роли'}
|
||
</Text>
|
||
{role && <Badge variant="light">ID: {role.id}</Badge>}
|
||
</Group>
|
||
}
|
||
size="lg"
|
||
>
|
||
{isLoading ? (
|
||
<Center p={40}>
|
||
<Loader />
|
||
</Center>
|
||
) : (
|
||
<form onSubmit={form.onSubmit(handleSubmit)}>
|
||
<Stack gap="md">
|
||
<TextInput
|
||
label="Название"
|
||
placeholder="Администратор"
|
||
required
|
||
{...form.getInputProps('name')}
|
||
/>
|
||
|
||
<TextInput
|
||
label="Код"
|
||
placeholder="admin"
|
||
required
|
||
description="Уникальный идентификатор роли (lowercase, цифры, подчеркивания)"
|
||
disabled={isEditMode}
|
||
{...form.getInputProps('code')}
|
||
/>
|
||
|
||
<Textarea
|
||
label="Описание"
|
||
placeholder="Описание роли..."
|
||
minRows={2}
|
||
{...form.getInputProps('description')}
|
||
/>
|
||
|
||
<Paper withBorder p="md">
|
||
<Text size="sm" fw={500} mb="sm">Типы ролей</Text>
|
||
<Stack gap="sm">
|
||
<Switch
|
||
label="Директор"
|
||
description="Пользователь с этой ролью является директором"
|
||
checked={form.values.is_director}
|
||
onChange={(e) => form.setFieldValue('is_director', e.currentTarget.checked)}
|
||
color="violet"
|
||
/>
|
||
<Switch
|
||
label="Согласующий"
|
||
description="Пользователь с этой ролью может согласовывать заявки"
|
||
checked={form.values.is_approver}
|
||
onChange={(e) => form.setFieldValue('is_approver', e.currentTarget.checked)}
|
||
color="blue"
|
||
/>
|
||
<Switch
|
||
label="Наблюдатель"
|
||
description="Пользователь с этой ролью может наблюдать за заявками"
|
||
checked={form.values.is_observer}
|
||
onChange={(e) => form.setFieldValue('is_observer', e.currentTarget.checked)}
|
||
color="green"
|
||
/>
|
||
<Switch
|
||
label="Исполнитель"
|
||
description="Пользователь с этой ролью может выполнять задачи"
|
||
checked={form.values.is_executor}
|
||
onChange={(e) => form.setFieldValue('is_executor', e.currentTarget.checked)}
|
||
color="orange"
|
||
/>
|
||
</Stack>
|
||
</Paper>
|
||
|
||
<Switch
|
||
label="Роль по умолчанию"
|
||
description="Назначается новым пользователям автоматически"
|
||
checked={form.values.is_default}
|
||
onChange={(e) => form.setFieldValue('is_default', e.currentTarget.checked)}
|
||
color="blue"
|
||
/>
|
||
|
||
<Group justify="flex-end" mt="md">
|
||
<Button variant="default" onClick={onClose} disabled={isSubmitting}>
|
||
Отмена
|
||
</Button>
|
||
<Button type="submit" loading={isSubmitting}>
|
||
{isEditMode ? 'Сохранить' : 'Создать'}
|
||
</Button>
|
||
</Group>
|
||
</Stack>
|
||
</form>
|
||
)}
|
||
</Modal>
|
||
)
|
||
} |