diff --git a/src/pages/product/List.tsx b/src/pages/product/List.tsx index 711066f..98b59e3 100644 --- a/src/pages/product/List.tsx +++ b/src/pages/product/List.tsx @@ -1,7 +1,6 @@ import Button from '@/components/Button' import Filters, { type FilterValues } from '@/components/Filters' import Table from '@/components/Table' -import Tabs from '@/components/Tabs' import { AddSquare, Edit, More2 } from 'iconsax-react' import { type FC, useMemo, useState } from 'react' import { useDeleteProduct, useDuplicateProduct, useGetCategory, useGetProducts } from './hooks/useProductData' @@ -11,39 +10,44 @@ import TrashWithConfrim from '@/components/TrashWithConfrim' import CopyWithConfirm from '@/components/CopyWithConfirm' import { toast } from 'react-toastify' import { extractErrorMessage } from '@/config/func' +import type { CategoryType } from './types/Types' + +const flattenCategoryOptions = (categories: CategoryType[]) => { + const options: { label: string; value: string }[] = [] + + for (const category of categories) { + options.push({ label: category.title, value: category.id }) + for (const child of category.children ?? []) { + options.push({ label: `— ${child.title}`, value: child.id }) + } + } + + return options +} const ProductList: FC = () => { const [page, setPage] = useState(1) const [search, setSearch] = useState('') const [categoryId, setCategoryId] = useState('') - const { data: categoriesData } = useGetCategory({ limit: 1000 }) + const { data: categoriesData } = useGetCategory({ limit: 1000, parentId: 'null' }) const { data: products, refetch } = useGetProducts({ page, search: search || undefined, categoryId: categoryId || undefined, }) - const categoryTabs = useMemo( - () => [ - { label: 'همه', value: '' }, - ...(categoriesData?.data?.map((category) => ({ - label: category.title, - value: category.id, - })) ?? []), - ], + const categoryOptions = useMemo( + () => flattenCategoryOptions((categoriesData?.data ?? []) as CategoryType[]), [categoriesData?.data], ) const handleFiltersChange = (filters: FilterValues) => { - setSearch((filters.search as string) ?? '') + setSearch(filters.search ?? '') + setCategoryId(filters.categoryId ?? '') setPage(1) } - const handleCategoryTabChange = (tab: string) => { - setCategoryId(tab) - setPage(1) - } const { mutate: deleteProduct, isPending: isDeleting } = useDeleteProduct() const { mutate: duplicateProduct, isPending: isDuplicating } = useDuplicateProduct() const [duplicatingId, setDuplicatingId] = useState(null) @@ -98,14 +102,6 @@ const ProductList: FC = () => { -
- -
-
{ name: 'search', type: 'input', placeholder: 'جستجوی محصول' + }, + { + name: 'categoryId', + type: 'select', + placeholder: 'دسته‌بندی', + options: categoryOptions, } ]} onChange={handleFiltersChange} diff --git a/src/pages/product/components/CategoriesSelect.tsx b/src/pages/product/components/CategoriesSelect.tsx index 5e13394..72fb8d2 100644 --- a/src/pages/product/components/CategoriesSelect.tsx +++ b/src/pages/product/components/CategoriesSelect.tsx @@ -1,6 +1,7 @@ -import { type FC, type SelectHTMLAttributes } from 'react' +import { type FC, type SelectHTMLAttributes, useMemo } from 'react' import { useGetCategory } from '../hooks/useProductData' import Select from '@/components/Select' +import type { CategoryType } from '../types/Types' type Props = { error_text?: string, @@ -9,6 +10,19 @@ type Props = { clearable?: boolean, } & SelectHTMLAttributes +const flattenCategoryOptions = (categories: CategoryType[]) => { + const options: { label: string; value: string }[] = [] + + for (const category of categories) { + options.push({ label: category.title, value: category.id }) + for (const child of category.children ?? []) { + options.push({ label: `— ${child.title}`, value: child.id }) + } + } + + return options +} + const CategoriesSelect: FC = ({ isDisableShowLable, placeholder, @@ -16,8 +30,12 @@ const CategoriesSelect: FC = ({ clearable = true, ...selectProps }) => { + const { data: categories } = useGetCategory({ limit: 1000, parentId: 'null' }) - const { data: categories } = useGetCategory() + const items = useMemo( + () => flattenCategoryOptions((categories?.data ?? []) as CategoryType[]), + [categories?.data], + ) return (