sub category list

This commit is contained in:
hamid zarghami
2026-02-15 09:52:14 +03:30
parent 8732374c9a
commit 1ff85d6cb4
2 changed files with 32 additions and 4 deletions
+26 -3
View File
@@ -17,6 +17,27 @@ import {
import { useSingleUpload } from '../uploader/hooks/useUploaderData' import { useSingleUpload } from '../uploader/hooks/useUploaderData'
import { extractErrorMessage } from '@/config/func' import { extractErrorMessage } from '@/config/func'
function findCategoryById(cats: Category[], id: string | null): Category | undefined {
if (!id) return undefined
for (const c of cats) {
if (c.id === id) return c
const found = findCategoryById(c.children || [], id)
if (found) return found
}
return undefined
}
function flattenCategories(cats: Category[]): Category[] {
const result: Category[] = []
for (const c of cats) {
result.push(c)
if (c.children?.length) {
result.push(...flattenCategories(c.children))
}
}
return result
}
type CategoryFormValues = { type CategoryFormValues = {
title: string title: string
icon: File[] icon: File[]
@@ -39,7 +60,8 @@ const CategoryProduct: FC = () => {
const categories = categoriesData?.data || [] const categories = categoriesData?.data || []
const filteredCategories = categories.filter((category) => { const flattenedCategories = flattenCategories(categories)
const filteredCategories = flattenedCategories.filter((category) => {
if (filters.search) { if (filters.search) {
return category.title.toLowerCase().includes(filters.search.toLowerCase()) return category.title.toLowerCase().includes(filters.search.toLowerCase())
} }
@@ -122,7 +144,8 @@ const CategoryProduct: FC = () => {
formik.setValues({ formik.setValues({
title: category.title, title: category.title,
icon: [], icon: [],
order: category.order || 0 order: category.order || 0,
parentId: category.parent || undefined
}) })
setIsActive(category.isActive) setIsActive(category.isActive)
setIconFiles([]) setIconFiles([])
@@ -153,7 +176,7 @@ const CategoryProduct: FC = () => {
} }
const handleStatusChange = (id: string, value: boolean) => { const handleStatusChange = (id: string, value: boolean) => {
const category = categories.find((cat) => cat.id === id) const category = findCategoryById(categories, id)
if (category) { if (category) {
const categoryData: CreateCategoryType = { const categoryData: CreateCategoryType = {
title: category.title, title: category.title,
@@ -44,7 +44,12 @@ const CategoryTableColumns = ({ categories, onStatusChange, onEdit, onDelete, is
}, },
{ {
title: 'عنوان', title: 'عنوان',
key: 'title' key: 'title',
render: (item: Category) => (
<div className={item.parent ? 'pr-6' : ''}>
{item.title}
</div>
)
}, },
{ {
title: 'والد', title: 'والد',