diff --git a/src/pages/categories/components/CategoryDrawer.tsx b/src/pages/categories/components/CategoryDrawer.tsx index 087ccb7..9f2a881 100644 --- a/src/pages/categories/components/CategoryDrawer.tsx +++ b/src/pages/categories/components/CategoryDrawer.tsx @@ -1,4 +1,4 @@ -import { useEffect } from 'react' +import { useEffect, useState } from 'react' import { Badge, Box, Button, Drawer, Group, Stack, Switch, Text, Textarea, TextInput, Skeleton } from '@mantine/core' import { useForm } from '@mantine/form' @@ -11,6 +11,23 @@ interface Props { onClose: () => void } +function generateSlug(text: string): string { + const ru: Record = { + а: 'a', б: 'b', в: 'v', г: 'g', д: 'd', е: 'e', ё: 'e', ж: 'zh', + з: 'z', и: 'i', й: 'y', к: 'k', л: 'l', м: 'm', н: 'n', о: 'o', + п: 'p', р: 'r', с: 's', т: 't', у: 'u', ф: 'f', х: 'h', ц: 'c', + ч: 'ch', ш: 'sh', щ: 'sch', ъ: '', ы: 'y', ь: '', э: 'e', ю: 'yu', я: 'ya', + } + return text + .toLowerCase() + .split('') + .map((char) => ru[char] || char) + .join('') + .replace(/[^a-z0-9\s_-]/g, '') // оставляем только латиницу, цифры, пробелы и дефисы + .trim() + .replace(/[\s-]+/g, '_') // заменяем пробелы и дефисы на нижнее подчеркивание +} + export default function CategoryDrawer({ categoryId, opened, onClose }: Props) { const { data: category, isLoading: categoryLoading } = useCategory(categoryId) @@ -18,6 +35,7 @@ export default function CategoryDrawer({ categoryId, opened, onClose }: Props) { const updateCategory = useUpdateCategory() const isEditMode = categoryId !== null + const [isCodeAutoGenerated, setIsCodeAutoGenerated] = useState(true) const form = useForm({ initialValues: { @@ -45,11 +63,22 @@ export default function CategoryDrawer({ categoryId, opened, onClose }: Props) { is_active: category.is_active ?? true, }) form.resetDirty() + setIsCodeAutoGenerated(false) } else if (!isEditMode && opened) { form.reset() + setIsCodeAutoGenerated(true) } + // eslint-disable-next-line react-hooks/exhaustive-deps }, [category, isEditMode, opened]) + // Автогенерация кода при изменении названия (только если пользователь не начал вводить код вручную) + useEffect(() => { + if (!isEditMode && isCodeAutoGenerated) { + form.setFieldValue('code', generateSlug(form.values.name)) + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [form.values.name, isEditMode, isCodeAutoGenerated]) + const handleSubmit = async (values: typeof form.values) => { if (isEditMode && categoryId) { await updateCategory.mutateAsync({ id: categoryId, ...values }) @@ -62,6 +91,8 @@ export default function CategoryDrawer({ categoryId, opened, onClose }: Props) { const isLoading = isEditMode && categoryLoading const isSubmitting = createCategory.isPending || updateCategory.isPending + const codeProps = form.getInputProps('code') + return ( @@ -103,7 +134,11 @@ export default function CategoryDrawer({ categoryId, opened, onClose }: Props) { description="Уникальный системный идентификатор (только lowercase)" withAsterisk disabled={isEditMode} - {...form.getInputProps('code')} + {...codeProps} + onChange={(e) => { + codeProps.onChange(e) + setIsCodeAutoGenerated(false) + }} />