list coupons
This commit is contained in:
+106
-7
@@ -1,22 +1,121 @@
|
||||
import { type FC } from 'react'
|
||||
import { useGetCoupons } from './hooks/useCouponData'
|
||||
import { type FC, useState } from 'react'
|
||||
import { useGetCoupons, useDeleteCoupon } from './hooks/useCouponData'
|
||||
import { type CouponResponseData } 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 Button from '@/components/Button'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { Pages } from '@/config/Pages'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
|
||||
const CouponList: FC = () => {
|
||||
|
||||
const { data, isLoading, error } = useGetCoupons()
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const { data: couponsData, isLoading, error, refetch } = useGetCoupons(currentPage)
|
||||
const deleteCouponMutation = useDeleteCoupon();
|
||||
const navigate = useNavigate();
|
||||
|
||||
if (isLoading) {
|
||||
return <PageLoading />
|
||||
return (
|
||||
<div className="flex justify-center items-center min-h-[400px]">
|
||||
<PageLoading />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <Error errorText="خطا در بارگذاری تخفیفات" />
|
||||
return (
|
||||
<div className="flex justify-center items-center min-h-[400px]">
|
||||
<Error errorText="خطا در بارگذاری تخفیفات" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const handleDeleteCoupon = async (couponId: string) => {
|
||||
await deleteCouponMutation.mutateAsync(couponId);
|
||||
refetch()
|
||||
};
|
||||
|
||||
const coupons = couponsData?.results?.coupons || [];
|
||||
const pager = couponsData?.results?.pager;
|
||||
|
||||
return (
|
||||
<div>List</div>
|
||||
<div>
|
||||
<div className='flex justify-end mt-5'>
|
||||
<Button
|
||||
label='افزودن کوپن'
|
||||
onClick={() => navigate(`${Pages.coupon.create}`)}
|
||||
className='w-fit'
|
||||
/>
|
||||
</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={'استفاده شده'} />
|
||||
<Td text={'حداقل خرید'} />
|
||||
<Td text={'وضعیت'} />
|
||||
<Td text={'تاریخ انقضا'} />
|
||||
<Td text={'عملیات'} />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{coupons.length === 0 ? (
|
||||
<tr className='tr'>
|
||||
<td colSpan={9} className="text-center py-8 text-gray-500">
|
||||
هیچ کوپنی یافت نشد
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
coupons.map((coupon: CouponResponseData) => (
|
||||
<tr key={coupon._id} className='tr'>
|
||||
<Td text={coupon.code} />
|
||||
<Td text={coupon.discountPercentage ? `${coupon.discountPercentage}%` : '-'} />
|
||||
<Td text={coupon.discountAmount ? `${coupon.discountAmount.toLocaleString('fa-IR')} تومان` : '-'} />
|
||||
<Td text={coupon.usageLimit.toLocaleString('fa-IR')} />
|
||||
<Td text={coupon.usageCount.toLocaleString('fa-IR')} />
|
||||
<Td text={coupon.minPurchaseAmount ? `${coupon.minPurchaseAmount.toLocaleString('fa-IR')} تومان` : '-'} />
|
||||
<Td text="">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={`px-2 py-1 rounded-full text-xs ${coupon.isActive ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'}`}>
|
||||
{coupon.isActive ? 'فعال' : 'غیرفعال'}
|
||||
</span>
|
||||
</div>
|
||||
</Td>
|
||||
<Td text={coupon.expirationDate} />
|
||||
<Td text="">
|
||||
<div className="flex items-center gap-2">
|
||||
<Link to={`${Pages.coupon.detail}${coupon._id}`}>
|
||||
<Edit color='#8C90A3' size={16} className="cursor-pointer hover:text-blue-500" />
|
||||
</Link>
|
||||
|
||||
<TrashWithConfrim
|
||||
onDelete={() => handleDeleteCoupon(coupon._id)}
|
||||
isLoading={deleteCouponMutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
</Td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<PaginationUi
|
||||
pager={pager}
|
||||
onPageChange={(page) => {
|
||||
setCurrentPage(page);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { type FC } from 'react'
|
||||
|
||||
const UpdateCoupon: FC = () => {
|
||||
return (
|
||||
<div>UpdateCoupon</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UpdateCoupon
|
||||
@@ -19,3 +19,14 @@ export const useCreateCoupon = () => {
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const useDeleteCoupon = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (couponId: string) => api.deleteCoupon(couponId),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["coupons"] });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import axios from "../../../config/axios";
|
||||
import type { CreateCouponType, CouponType } from "../types/Types";
|
||||
import type {
|
||||
CreateCouponType,
|
||||
CouponType,
|
||||
CouponsResponse,
|
||||
} from "../types/Types";
|
||||
|
||||
export const getCoupons = async (page: number = 1) => {
|
||||
export const getCoupons = async (
|
||||
page: number = 1
|
||||
): Promise<CouponsResponse> => {
|
||||
const { data } = await axios.get(`/admin/coupon/native?page=${page}`);
|
||||
return data;
|
||||
};
|
||||
@@ -12,3 +18,7 @@ export const createCoupon = async (
|
||||
const { data } = await axios.post("/admin/coupon", couponData);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const deleteCoupon = async (couponId: string): Promise<void> => {
|
||||
await axios.delete(`/admin/coupon/${couponId}`);
|
||||
};
|
||||
|
||||
@@ -19,3 +19,43 @@ export interface CouponType extends CreateCouponType {
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface CouponResponseData {
|
||||
_id: string;
|
||||
shopId: string;
|
||||
discountPercentage: number;
|
||||
discountAmount: number;
|
||||
usageLimit: number;
|
||||
usageCount: number;
|
||||
userUsageLimit: number;
|
||||
code: string;
|
||||
minPurchaseAmount: number;
|
||||
category: string[];
|
||||
includedProducts: number[];
|
||||
excludedProducts: number[];
|
||||
description: string;
|
||||
isActive: boolean;
|
||||
includeSpecialSale: boolean;
|
||||
expirationDate: string;
|
||||
deleted: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export type PagerType = {
|
||||
page: number;
|
||||
limit: number;
|
||||
totalItems: number;
|
||||
totalPages: number;
|
||||
prevPage: boolean | null;
|
||||
nextPage: string | null;
|
||||
};
|
||||
|
||||
export interface CouponsResponse {
|
||||
status: number;
|
||||
success: boolean;
|
||||
results: {
|
||||
coupons: CouponResponseData[];
|
||||
pager: PagerType;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -187,8 +187,8 @@ export type PagerType = {
|
||||
limit: number;
|
||||
totalItems: number;
|
||||
totalPages: number;
|
||||
prevPage: boolean;
|
||||
nextPage: boolean;
|
||||
prevPage: boolean | null;
|
||||
nextPage: string | null;
|
||||
};
|
||||
|
||||
export type OrdersResponseType = {
|
||||
|
||||
@@ -267,6 +267,14 @@ export type CreateProductVariantRequestType = {
|
||||
stock: number;
|
||||
};
|
||||
|
||||
export type CreateProductVariantResponseType = {
|
||||
status: number;
|
||||
success: boolean;
|
||||
results: {
|
||||
variant: ProductVariantDetailType;
|
||||
};
|
||||
};
|
||||
|
||||
// تایپهای مربوط به جزئیات محصول
|
||||
export type ProductSpecificationType = {
|
||||
title: string;
|
||||
@@ -373,6 +381,14 @@ export type UpdateVariantRequestType = {
|
||||
specialSale_endDate: string;
|
||||
};
|
||||
|
||||
export type UpdateVariantResponseType = {
|
||||
status: number;
|
||||
success: boolean;
|
||||
results: {
|
||||
variant: ProductVariantDetailType;
|
||||
};
|
||||
};
|
||||
|
||||
// تایپهای مربوط به آپدیت محصول سه مرحلهای
|
||||
|
||||
// مرحله اول: آپدیت جزئیات محصول
|
||||
|
||||
Reference in New Issue
Block a user