admin_ruspet/src/pages/admins/components/EditAdminModal.tsx

137 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useEffect } from 'react'
import {
Modal,
Stack,
TextInput,
Button,
Text,
Group,
Badge,
Switch,
Paper,
Alert,
} from '@mantine/core'
import { useForm } from '@mantine/form'
import { IconShield, IconAlertCircle } from '@tabler/icons-react'
import { useUpdateAdmin } from '@/features/admins/hooks/useAdmins'
import { useAuth } from '@/hooks/useAuth'
import type { Admin } from '@/types/api'
interface EditAdminModalProps {
admin: Admin | null
opened: boolean
onClose: () => void
}
export default function EditAdminModal({ admin, opened, onClose }: EditAdminModalProps) {
const updateAdmin = useUpdateAdmin()
const { user: currentUser } = useAuth()
const form = useForm({
initialValues: {
name: '',
is_root: false,
},
})
useEffect(() => {
if (admin) {
form.setValues({
name: admin.name || '',
is_root: admin.is_root ?? false,
})
}
}, [admin])
const isCurrentUser = currentUser?.id === admin?.id
const handleSubmit = async (values: typeof form.values) => {
if (!admin?.id) return
await updateAdmin.mutateAsync({
id: admin.id,
name: values.name || undefined,
is_root: values.is_root,
})
onClose()
}
return (
<Modal
opened={opened}
onClose={onClose}
title={
<Group>
<Text fw={700}>Редактирование администратора</Text>
{admin && <Badge variant="light">{admin.login}</Badge>}
{admin?.is_root && (
<Badge color="red" variant="filled" leftSection={<IconShield size={12} />}>
ROOT
</Badge>
)}
</Group>
}
size="md"
>
<form onSubmit={form.onSubmit(handleSubmit)}>
<Stack gap="md">
<TextInput
label="Логин"
value={admin?.login || ''}
disabled
description="Логин нельзя изменить"
/>
<TextInput
label="Имя"
placeholder="Иванов Иван"
description="Отображаемое имя администратора"
{...form.getInputProps('name')}
/>
<Paper withBorder p="md" bg={form.values.is_root ? 'red.0' : 'gray.0'}>
<Switch
label={
<Group gap="xs">
<IconShield size={16} color="red" />
<Text fw={600}>Root-права</Text>
</Group>
}
description="Может создавать администраторов и управлять root-правами"
checked={form.values.is_root}
onChange={(e) => form.setFieldValue('is_root', e.currentTarget.checked)}
color="red"
disabled={isCurrentUser}
/>
</Paper>
{isCurrentUser && (
<Alert icon={<IconAlertCircle size={16} />} color="yellow" variant="light">
<Text size="sm">
Вы не можете изменить свои собственные root-права.
</Text>
</Alert>
)}
{form.values.is_root !== (admin?.is_root ?? false) && !isCurrentUser && (
<Alert icon={<IconAlertCircle size={16} />} color="red" variant="light">
<Text size="sm">
<b>Внимание!</b> Вы изменяете root-права. {form.values.is_root
? 'Этот администратор получит полный доступ к системе.'
: 'Этот администратор потеряет возможность создавать других администраторов.'}
</Text>
</Alert>
)}
<Group justify="flex-end" mt="md">
<Button variant="default" onClick={onClose} disabled={updateAdmin.isPending}>
Отмена
</Button>
<Button type="submit" loading={updateAdmin.isPending}>
Сохранить
</Button>
</Group>
</Stack>
</form>
</Modal>
)
}