cancel reason
This commit is contained in:
@@ -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 (
|
||||
<div className="flex justify-center items-center min-h-[400px]">
|
||||
<Error errorText="خطا در بارگذاری دستهبندی کنسلیها" />
|
||||
<Error errorText="خطا در بارگذاری دلایل کنسلی" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const handleDeleteCategory = async (categoryId: string) => {
|
||||
await deleteCategoryMutation.mutateAsync(categoryId);
|
||||
refetch()
|
||||
};
|
||||
const cancelReasons = cancelReasonsData?.results?.reasons || [];
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className='flex justify-end mt-5'>
|
||||
<Button
|
||||
label='افزودن دستهبندی کنسلی'
|
||||
onClick={() => navigate('/orders/cancellation-categories/create')}
|
||||
className='w-fit'
|
||||
/>
|
||||
<ModalCreateCancelReason />
|
||||
</div>
|
||||
<div className='relative overflow-x-auto rounded-3xl mt-5 w-full'>
|
||||
<table className='w-full text-sm'>
|
||||
<thead className='thead'>
|
||||
<tr>
|
||||
<Td text={'عنوان'} />
|
||||
<Td text={'توضیحات'} />
|
||||
<Td text={'قانون جریمه'} />
|
||||
<Td text={'وضعیت'} />
|
||||
<Td text={'عملیات'} />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{categories.length === 0 ? (
|
||||
{cancelReasons.length === 0 ? (
|
||||
<tr className='tr'>
|
||||
<td colSpan={4} className="text-center py-8 text-gray-500">
|
||||
هیچ دستهبندی کنسلی یافت نشد
|
||||
هیچ دلیل کنسلی یافت نشد
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
categories.map((category: any) => (
|
||||
<tr key={category.id} className='tr'>
|
||||
<Td text={category.title} />
|
||||
<Td text={category.description} />
|
||||
cancelReasons.map((reason: CancelReasonType) => (
|
||||
<tr key={reason._id} className='tr'>
|
||||
<Td text={reason.title} />
|
||||
<Td text={reason.fineRule ? `${reason.fineRule.title} (${reason.fineRule.fine_percentage}%)` : 'بدون جریمه'} />
|
||||
<Td text="">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`px-2 py-1 rounded-full text-xs`}>
|
||||
{category.status}
|
||||
<span className={`px-2 py-1 rounded-full text-xs ${reason.deleted ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-800'}`}>
|
||||
{reason.deleted ? 'حذف شده' : 'فعال'}
|
||||
</span>
|
||||
</div>
|
||||
</Td>
|
||||
<Td text="">
|
||||
<div className="flex items-center gap-2">
|
||||
<Link to={`/orders/cancellation-categories/${category.id}`}>
|
||||
<Edit color='#8C90A3' size={16} className="cursor-pointer hover:text-blue-500" />
|
||||
</Link>
|
||||
|
||||
<TrashWithConfrim
|
||||
onDelete={() => handleDeleteCategory(category.id)}
|
||||
isLoading={deleteCategoryMutation.isPending}
|
||||
onDelete={() => handleDeleteCancelReason(reason._id)}
|
||||
isLoading={deleteCancelReasonMutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
</Td>
|
||||
@@ -102,15 +83,8 @@ const CancellationCategories: FC = () => {
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<PaginationUi
|
||||
pager={pager}
|
||||
onPageChange={(page) => {
|
||||
setCurrentPage(page);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CancellationCategories
|
||||
export default CancellationCategories
|
||||
@@ -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<boolean>(false)
|
||||
const createCancelReasonMutation = useCreateCancelReason()
|
||||
const { data: finesData } = useGetFines()
|
||||
|
||||
const formik = useFormik<CreateCancelReasonType>({
|
||||
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 (
|
||||
<div>
|
||||
<Button
|
||||
label='افزودن دلیل کنسلی'
|
||||
onClick={() => setOpen(true)}
|
||||
className='w-fit px-5 whitespace-nowrap'
|
||||
/>
|
||||
<DefaulModal
|
||||
open={open}
|
||||
close={() => setOpen(false)}
|
||||
title_header='افزودن دلیل کنسلی'
|
||||
isHeader
|
||||
>
|
||||
<form onSubmit={formik.handleSubmit} className='space-y-4 w-[400px] mt-6'>
|
||||
<Input
|
||||
label='عنوان دلیل کنسلی'
|
||||
placeholder='عنوان دلیل کنسلی را وارد کنید'
|
||||
name='title'
|
||||
value={formik.values.title}
|
||||
onChange={formik.handleChange}
|
||||
onBlur={formik.handleBlur}
|
||||
error_text={formik.touched.title && formik.errors.title ? formik.errors.title : undefined}
|
||||
/>
|
||||
|
||||
<Select
|
||||
label='قانون جریمه'
|
||||
placeholder='قانون جریمه را انتخاب کنید'
|
||||
name='fineRule'
|
||||
value={formik.values.fineRule}
|
||||
onChange={formik.handleChange}
|
||||
onBlur={formik.handleBlur}
|
||||
error_text={formik.touched.fineRule && formik.errors.fineRule ? formik.errors.fineRule : undefined}
|
||||
items={finesData?.results?.fineRules?.map(fine => ({
|
||||
value: fine._id,
|
||||
label: `${fine.title} (${fine.fine_percentage}%)`
|
||||
})) || []}
|
||||
/>
|
||||
|
||||
<div className='flex gap-2 pt-4'>
|
||||
<Button
|
||||
type='submit'
|
||||
label={createCancelReasonMutation.isPending ? 'در حال ذخیره...' : 'ذخیره'}
|
||||
disabled={createCancelReasonMutation.isPending}
|
||||
className='flex-1'
|
||||
/>
|
||||
<Button
|
||||
type='button'
|
||||
label='لغو'
|
||||
variant='outline'
|
||||
onClick={() => setOpen(false)}
|
||||
className='flex-1'
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
</DefaulModal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ModalCreateCancelReason
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import * as api from "../service/OrderService";
|
||||
|
||||
export const useGetNativeOrders = (
|
||||
@@ -30,3 +30,22 @@ export const useGetOrderDetailUser = (id: string) => {
|
||||
enabled: !!id,
|
||||
});
|
||||
};
|
||||
|
||||
export const useCreateCancelReason = () => {
|
||||
return useMutation({
|
||||
mutationFn: api.createCancelReason,
|
||||
});
|
||||
};
|
||||
|
||||
export const useGetCancelReasons = () => {
|
||||
return useQuery({
|
||||
queryKey: ["cancel-reasons"],
|
||||
queryFn: api.getCancelReasons,
|
||||
});
|
||||
};
|
||||
|
||||
export const useDeleteCancelReason = () => {
|
||||
return useMutation({
|
||||
mutationFn: api.deleteCancelReason,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import axios from "../../../config/axios";
|
||||
import type {
|
||||
CancelReasonsResponseType,
|
||||
CreateCancelReasonType,
|
||||
NativeOrdersResponseType,
|
||||
OrderDetailResponseType,
|
||||
} from "../types/Types";
|
||||
@@ -30,3 +32,20 @@ export const getOrderDetailUser = async (
|
||||
const { data } = await axios.get(`/admin/orders/${id}/user`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const createCancelReason = async (params: CreateCancelReasonType) => {
|
||||
const { data } = await axios.post(`/admin/orders/cancel/reasons`, params);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getCancelReasons = async (): Promise<
|
||||
CancelReasonsResponseType
|
||||
> => {
|
||||
const { data } = await axios.get(`/cancel/reasons`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const deleteCancelReason = async (id: string) => {
|
||||
const { data } = await axios.delete(`/cancel/reasons/${id}`);
|
||||
return data;
|
||||
};
|
||||
|
||||
@@ -280,3 +280,34 @@ export type OrderDetailResponseType = {
|
||||
order: OrderDetailType;
|
||||
};
|
||||
};
|
||||
|
||||
export type CreateCancelReasonType = {
|
||||
fineRule: string;
|
||||
title: string;
|
||||
};
|
||||
|
||||
export type FineRuleType = {
|
||||
_id: string;
|
||||
title: string;
|
||||
fine_percentage: number;
|
||||
deleted: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
export type CancelReasonType = {
|
||||
_id: string;
|
||||
title: string;
|
||||
deleted: boolean;
|
||||
fineRule?: FineRuleType;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
export type CancelReasonsResponseType = {
|
||||
status: number;
|
||||
success: boolean;
|
||||
results: {
|
||||
reasons: CancelReasonType[];
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user