From a1995fabb822a1afa5e9a5eb08acd748d6277cc0 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Thu, 9 Jul 2026 17:35:08 +0330 Subject: [PATCH] cat --- src/pages/product/category/Create.tsx | 9 ++-- src/pages/product/category/List.tsx | 64 ++++++++++++--------------- src/pages/product/category/Update.tsx | 23 +++++++--- src/pages/product/types/Types.ts | 2 +- 4 files changed, 52 insertions(+), 46 deletions(-) diff --git a/src/pages/product/category/Create.tsx b/src/pages/product/category/Create.tsx index 3bb5914..9f55d34 100644 --- a/src/pages/product/category/Create.tsx +++ b/src/pages/product/category/Create.tsx @@ -27,10 +27,12 @@ const ProductCategory: FC = () => { const [file, setFile] = useState() const handleSave = (values: CreateCategoryType) => { - if (values.parentId) { - values.parentId = Number(values.parentId) + const payload: CreateCategoryType = { + ...values, + parentId: values.parentId || undefined, } - mutate(values, { + + mutate(payload, { onSuccess: () => { toast.success('با موفقیت ذخیره شد') navigate(Paths.product.category.list) @@ -111,6 +113,7 @@ const ProductCategory: FC = () => { }) || []} onChange={formik.handleChange} name='parentId' + value={formik.values.parentId || ''} /> diff --git a/src/pages/product/category/List.tsx b/src/pages/product/category/List.tsx index e91bb90..c732a60 100644 --- a/src/pages/product/category/List.tsx +++ b/src/pages/product/category/List.tsx @@ -1,4 +1,4 @@ -import { type FC } from 'react' +import { type FC, useMemo } from 'react' import { useDeleteCategory, useGetCategory } from '../hooks/useProductData' import Table from '@/components/Table' import type { ColumnType, RowDataType } from '@/components/types/TableTypes' @@ -16,6 +16,7 @@ const CategoryList: FC = () => { const { data, refetch } = useGetCategory() const { mutate, isPending } = useDeleteCategory() + const categories = (data?.data ?? []) as (CategoryType & RowDataType)[] const handleDelete = (id: string) => { mutate(id, { @@ -28,57 +29,50 @@ const CategoryList: FC = () => { }) } - const column: ColumnType[] = [ + const columns = useMemo[]>(() => [ { title: 'تصویر', key: 'avatarUrl', - render: (item) => { - if (item.avatarUrl) { - return ( - - ) - } - } + render: (item) => item.avatarUrl ? {item.title} : '-' }, { title: 'عنوان', key: 'title', }, { - title: 'تعداد زیر درسته', + title: 'والد', + key: 'parent', + render: (item) => item.parent?.title || '-', + }, + { + title: 'تعداد زیر دسته', key: 'children', - render: (item) => { - return ( -
{item.children?.length}
- ) - } + render: (item) =>
{item.children?.length || 0}
}, { title: 'اولویت', - key: 'order' + key: 'order', + render: (item) => item.order ?? '-', }, { title: '', key: 'action', - render: (item) => { - return ( -
- - - - handleDelete(item.id + '')} - isloading={isPending} + render: (item) => ( +
+ + - -
- ) - } + + handleDelete(item.id + '')} + isloading={isPending} + /> +
+ ) } - ] + ], [isPending]) return (
@@ -100,8 +94,8 @@ const CategoryList: FC = () => {
- columns={column} - data={data?.data || [] as (CategoryType & RowDataType)[]} + columns={columns} + data={categories} />
diff --git a/src/pages/product/category/Update.tsx b/src/pages/product/category/Update.tsx index 5260d8c..023eed3 100644 --- a/src/pages/product/category/Update.tsx +++ b/src/pages/product/category/Update.tsx @@ -12,10 +12,13 @@ import SwitchComponent from '@/components/Switch' import { useSingleUpload } from '@/pages/uploader/hooks/useUploader' import { toast } from 'react-toastify' import { extractErrorMessage } from '@/config/func' -import { useParams } from 'react-router-dom' +import { useNavigate, useParams } from 'react-router-dom' +import { Paths } from '@/config/Paths' +import BackButton from '@/components/BackButton' const UpdateCategory: FC = () => { + const navigate = useNavigate() const { id } = useParams() const { data } = useGetCategory() const { data: dataDetail } = useGetCategoryDetail(id!) @@ -26,12 +29,15 @@ const UpdateCategory: FC = () => { const [file, setFile] = useState() const handleSave = (values: CreateCategoryType) => { - if (values.parentId) { - values.parentId = Number(values.parentId) + const payload: CreateCategoryType = { + ...values, + parentId: values.parentId || undefined, } - mutate({ id: id!, params: values }, { + + mutate({ id: id!, params: payload }, { onSuccess: () => { toast.success('با موفقیت ذخیره شد') + navigate(Paths.product.category.list) }, onError: (error) => { toast.error(extractErrorMessage(error)) @@ -69,7 +75,7 @@ const UpdateCategory: FC = () => { if (dataDetail?.data) { formik.setValues({ title: dataDetail?.data?.title, - parentId: Number(dataDetail?.data?.parent?.id) || undefined, + parentId: dataDetail?.data?.parent?.id || undefined, isActive: dataDetail?.data?.isActive, order: Number(dataDetail?.data?.order), avatarUrl: dataDetail?.data?.avatarUrl || undefined @@ -83,8 +89,10 @@ const UpdateCategory: FC = () => { return (
- -

ویرایش دسته

+
+ +

ویرایش دسته

+
diff --git a/src/pages/product/types/Types.ts b/src/pages/product/types/Types.ts index 678815d..261f53b 100644 --- a/src/pages/product/types/Types.ts +++ b/src/pages/product/types/Types.ts @@ -20,7 +20,7 @@ export type CategoryResponse = BaseResponse; export type CreateCategoryType = { title: string, - parentId?: number, + parentId?: string, isActive: boolean, avatarUrl?: string, order: number,