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 PageLoading from '../../components/PageLoading'
|
||||||
import Error from '../../components/Error'
|
import Error from '../../components/Error'
|
||||||
import PaginationUi from '../../components/PaginationUi'
|
|
||||||
import Td from '../../components/Td'
|
import Td from '../../components/Td'
|
||||||
import TrashWithConfrim from '../../components/TrashWithConfrim'
|
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 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 handleDeleteCancelReason = async (reasonId: string) => {
|
||||||
const categories: CancellationCategoryType[] = [];
|
await deleteCancelReasonMutation.mutateAsync(reasonId);
|
||||||
const pager: PagerType = { page: currentPage, limit: 10, totalPages: 1, totalItems: 0, prevPage: null, nextPage: null };
|
refetch()
|
||||||
|
};
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
@@ -36,63 +28,52 @@ const CancellationCategories: FC = () => {
|
|||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<div className="flex justify-center items-center min-h-[400px]">
|
<div className="flex justify-center items-center min-h-[400px]">
|
||||||
<Error errorText="خطا در بارگذاری دستهبندی کنسلیها" />
|
<Error errorText="خطا در بارگذاری دلایل کنسلی" />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleDeleteCategory = async (categoryId: string) => {
|
const cancelReasons = cancelReasonsData?.results?.reasons || [];
|
||||||
await deleteCategoryMutation.mutateAsync(categoryId);
|
|
||||||
refetch()
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className='flex justify-end mt-5'>
|
<div className='flex justify-end mt-5'>
|
||||||
<Button
|
<ModalCreateCancelReason />
|
||||||
label='افزودن دستهبندی کنسلی'
|
|
||||||
onClick={() => navigate('/orders/cancellation-categories/create')}
|
|
||||||
className='w-fit'
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<div className='relative overflow-x-auto rounded-3xl mt-5 w-full'>
|
<div className='relative overflow-x-auto rounded-3xl mt-5 w-full'>
|
||||||
<table className='w-full text-sm'>
|
<table className='w-full text-sm'>
|
||||||
<thead className='thead'>
|
<thead className='thead'>
|
||||||
<tr>
|
<tr>
|
||||||
<Td text={'عنوان'} />
|
<Td text={'عنوان'} />
|
||||||
<Td text={'توضیحات'} />
|
<Td text={'قانون جریمه'} />
|
||||||
<Td text={'وضعیت'} />
|
<Td text={'وضعیت'} />
|
||||||
<Td text={'عملیات'} />
|
<Td text={'عملیات'} />
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{categories.length === 0 ? (
|
{cancelReasons.length === 0 ? (
|
||||||
<tr className='tr'>
|
<tr className='tr'>
|
||||||
<td colSpan={4} className="text-center py-8 text-gray-500">
|
<td colSpan={4} className="text-center py-8 text-gray-500">
|
||||||
هیچ دستهبندی کنسلی یافت نشد
|
هیچ دلیل کنسلی یافت نشد
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
) : (
|
) : (
|
||||||
categories.map((category: any) => (
|
cancelReasons.map((reason: CancelReasonType) => (
|
||||||
<tr key={category.id} className='tr'>
|
<tr key={reason._id} className='tr'>
|
||||||
<Td text={category.title} />
|
<Td text={reason.title} />
|
||||||
<Td text={category.description} />
|
<Td text={reason.fineRule ? `${reason.fineRule.title} (${reason.fineRule.fine_percentage}%)` : 'بدون جریمه'} />
|
||||||
<Td text="">
|
<Td text="">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<span className={`px-2 py-1 rounded-full text-xs`}>
|
<span className={`px-2 py-1 rounded-full text-xs ${reason.deleted ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-800'}`}>
|
||||||
{category.status}
|
{reason.deleted ? 'حذف شده' : 'فعال'}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</Td>
|
</Td>
|
||||||
<Td text="">
|
<Td text="">
|
||||||
<div className="flex items-center gap-2">
|
<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
|
<TrashWithConfrim
|
||||||
onDelete={() => handleDeleteCategory(category.id)}
|
onDelete={() => handleDeleteCancelReason(reason._id)}
|
||||||
isLoading={deleteCategoryMutation.isPending}
|
isLoading={deleteCancelReasonMutation.isPending}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</Td>
|
</Td>
|
||||||
@@ -102,15 +83,8 @@ const CancellationCategories: FC = () => {
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<PaginationUi
|
|
||||||
pager={pager}
|
|
||||||
onPageChange={(page) => {
|
|
||||||
setCurrentPage(page);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
</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";
|
import * as api from "../service/OrderService";
|
||||||
|
|
||||||
export const useGetNativeOrders = (
|
export const useGetNativeOrders = (
|
||||||
@@ -30,3 +30,22 @@ export const useGetOrderDetailUser = (id: string) => {
|
|||||||
enabled: !!id,
|
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 axios from "../../../config/axios";
|
||||||
import type {
|
import type {
|
||||||
|
CancelReasonsResponseType,
|
||||||
|
CreateCancelReasonType,
|
||||||
NativeOrdersResponseType,
|
NativeOrdersResponseType,
|
||||||
OrderDetailResponseType,
|
OrderDetailResponseType,
|
||||||
} from "../types/Types";
|
} from "../types/Types";
|
||||||
@@ -30,3 +32,20 @@ export const getOrderDetailUser = async (
|
|||||||
const { data } = await axios.get(`/admin/orders/${id}/user`);
|
const { data } = await axios.get(`/admin/orders/${id}/user`);
|
||||||
return data;
|
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;
|
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