diff --git a/src/pages/orders/CancellationCategories.tsx b/src/pages/orders/CancellationCategories.tsx index 0e55b8c..913cb80 100644 --- a/src/pages/orders/CancellationCategories.tsx +++ b/src/pages/orders/CancellationCategories.tsx @@ -1,29 +1,21 @@ -import { type FC, useState } from 'react' +import { type FC } from 'react' +import ModalCreateCancelReason from './components/ModalCreateCancelReason' +import { useGetCancelReasons, useDeleteCancelReason } from './hooks/useOrderData' +import { type CancelReasonType } from './types/Types' import PageLoading from '../../components/PageLoading' import Error from '../../components/Error' -import PaginationUi from '../../components/PaginationUi' import Td from '../../components/Td' import TrashWithConfrim from '../../components/TrashWithConfrim' -import { Edit } from 'iconsax-react' -import { Link } from 'react-router-dom' -import Button from '@/components/Button' -import { useNavigate } from 'react-router-dom' -import { type CancellationCategoryType, type PagerType } from './types/Types' const CancellationCategories: FC = () => { - const [currentPage, setCurrentPage] = useState(1); - // TODO: Add category data fetching hook - const isLoading = false; - const error = null; - const refetch = () => { }; - // TODO: Add delete mutation - const deleteCategoryMutation = { isPending: false, mutateAsync: async (_id: string) => { } }; - const navigate = useNavigate(); + const { data: cancelReasonsData, isLoading, error, refetch } = useGetCancelReasons() + const deleteCancelReasonMutation = useDeleteCancelReason(); - // Mock data for now - const categories: CancellationCategoryType[] = []; - const pager: PagerType = { page: currentPage, limit: 10, totalPages: 1, totalItems: 0, prevPage: null, nextPage: null }; + const handleDeleteCancelReason = async (reasonId: string) => { + await deleteCancelReasonMutation.mutateAsync(reasonId); + refetch() + }; if (isLoading) { return ( @@ -36,63 +28,52 @@ const CancellationCategories: FC = () => { if (error) { return (
- +
); } - const handleDeleteCategory = async (categoryId: string) => { - await deleteCategoryMutation.mutateAsync(categoryId); - refetch() - }; + const cancelReasons = cancelReasonsData?.results?.reasons || []; return (
-
- {categories.length === 0 ? ( + {cancelReasons.length === 0 ? ( ) : ( - categories.map((category: any) => ( - - + @@ -102,15 +83,8 @@ const CancellationCategories: FC = () => {
- +
- هیچ دسته‌بندی کنسلی یافت نشد + هیچ دلیل کنسلی یافت نشد
- + cancelReasons.map((reason: CancelReasonType) => ( +
+
- - {category.status} + + {reason.deleted ? 'حذف شده' : 'فعال'}
- - - - handleDeleteCategory(category.id)} - isLoading={deleteCategoryMutation.isPending} + onDelete={() => handleDeleteCancelReason(reason._id)} + isLoading={deleteCancelReasonMutation.isPending} />
- - { - setCurrentPage(page); - }} - />
) } -export default CancellationCategories +export default CancellationCategories \ No newline at end of file diff --git a/src/pages/orders/components/ModalCreateCancelReason.tsx b/src/pages/orders/components/ModalCreateCancelReason.tsx new file mode 100644 index 0000000..a9c1fd5 --- /dev/null +++ b/src/pages/orders/components/ModalCreateCancelReason.tsx @@ -0,0 +1,97 @@ +import Button from '@/components/Button' +import DefaulModal from '@/components/DefaulModal' +import Input from '@/components/Input' +import Select from '@/components/Select' +import { useState, type FC } from 'react' +import type { CreateCancelReasonType } from '../types/Types' +import { useCreateCancelReason } from '../hooks/useOrderData' +import { useFormik } from 'formik' +import * as Yup from 'yup' +import { useGetFines } from '@/pages/payment/hooks/usePaymentData' + +const ModalCreateCancelReason: FC = () => { + + const [open, setOpen] = useState(false) + const createCancelReasonMutation = useCreateCancelReason() + const { data: finesData } = useGetFines() + + const formik = useFormik({ + initialValues: { + fineRule: '', + title: '', + }, + validationSchema: Yup.object({ + fineRule: Yup.string().required('انتخاب قانون جریمه الزامی است'), + title: Yup.string().required('عنوان دلیل کنسلی الزامی است'), + }), + onSubmit: async (values) => { + try { + await createCancelReasonMutation.mutateAsync(values) + setOpen(false) + formik.resetForm() + } catch (error) { + console.error('خطا در ایجاد دلیل کنسلی:', error) + } + }, + }) + + return ( +
+