Updated categories page
Deploy Frontend / build-and-deploy (push) Successful in 31s Details

This commit is contained in:
romanskijdev 2026-11-18 15:38:04 +03:00
parent a2cec7e2a3
commit 965af735a9
1 changed files with 37 additions and 2 deletions

View File

@ -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<string, string> = {
а: '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 (
<Drawer.Root opened={opened} onClose={onClose} position="right" size="min(480px, 100vw)">
<Drawer.Overlay backgroundOpacity={0.25} />
@ -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)
}}
/>
<Textarea