add discount and remove discount

This commit is contained in:
hamid zarghami
2025-04-21 15:42:43 +03:30
parent 1e134c1e19
commit 7b6b38ab3a
6 changed files with 81 additions and 10 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
VITE_TOKEN_NAME = 'dsc_token'
VITE_REFRESH_TOKEN_NAME = 'dsc_refresh_token'
VITE_BASE_URL = 'https://api.danakcorp.com'
# VITE_BASE_URL = 'http://192.168.0.213:4000'
# VITE_BASE_URL = 'http://192.168.1.117:4000'
+5 -1
View File
@@ -256,7 +256,11 @@
"thank_pay_2": "مبلغ این صورت حساب پرداخت شده است.",
"discount_code": "کد تخفیف",
"apply": "اعمال",
"pending": "تایید نشده"
"discount_applied": "کد تخفیف با موفقیت اعمال شد",
"discount_error": "کد تخفیف معتبر نیست",
"pending": "در انتظار تایید",
"discount_removed": "کد تخفیف با موفقیت حذف شد",
"remove_discount": "حذف تخفیف"
},
"active": "فعال",
"inactive": "غیرفعال",
+57 -7
View File
@@ -1,8 +1,8 @@
import { FC } from 'react'
import { FC, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useParams } from 'react-router-dom'
import { useGetInvoiceDetail } from './hooks/useReceiptData'
import { ReceiptDetailItemType } from './types/ReceiptTypes'
import { useApplyDiscount, useGetInvoiceDetail, useRemoveDiscount } from './hooks/useReceiptData'
import { ApplyDiscountType, ReceiptDetailItemType } from './types/ReceiptTypes'
import { NumberFormat } from '../../config/func'
import moment from 'moment-jalaali'
import PageLoading from '../../components/PageLoading'
@@ -16,13 +16,45 @@ import PayInvoice from './components/PayInvoice'
import Input from '../../components/Input'
import { Helmet } from 'react-helmet-async'
import { useGetProfile } from '../profile/hooks/useProfileData'
import { toast } from 'react-toastify'
import { ErrorType } from '../../helpers/types'
const ReceiptsDetail: FC = () => {
const { id } = useParams()
const { t } = useTranslation('global')
const [code, setCode] = useState<string>('')
const getProfile = useGetProfile()
const getInvoce = useGetInvoiceDetail(id ? id : '')
const applyDiscount = useApplyDiscount()
const removeDiscount = useRemoveDiscount()
const handleApplyDiscount = () => {
const params: ApplyDiscountType = {
id: id ? id : '',
code: code
}
applyDiscount.mutate(params, {
onSuccess: () => {
toast.success(t('receip.discount_applied'))
getInvoce.refetch()
},
onError: (error: ErrorType) => {
toast.error(error?.response?.data?.error?.message[0])
}
})
}
const handleRemoveDiscount = () => {
removeDiscount.mutate({ id: id ? id : '' }, {
onSuccess: () => {
toast.success(t('receip.discount_removed'))
getInvoce.refetch()
},
onError: (error: ErrorType) => {
toast.error(error?.response?.data?.error?.message[0])
}
})
}
return (
<div className='mt-4'>
@@ -39,21 +71,34 @@ const ReceiptsDetail: FC = () => {
</div>
{
getInvoce.data?.data?.invoice?.status === 'PENDING' || getInvoce.data?.data?.invoice?.status === 'APPROVED' ?
!getInvoce.data?.data?.invoice?.discount && ['PENDING', 'APPROVED', 'WAIT_PAYMENT'].includes(getInvoce.data?.data?.invoice?.status) ?
<div className='flex -mt-1 gap-2 items-center w-full'>
<div className='w-full xl:w-fit'>
<Input
className='bg-transparent border border-border xl:max-w-40'
placeholder={t('receip.discount_code')}
value={code}
onChange={(e) => setCode(e.target.value)}
/>
</div>
<Button
label={t('receip.apply')}
className='bg-transparent text-black border border-black mt-1 px-5 w-fit'
onClick={handleApplyDiscount}
isLoading={applyDiscount.isPending}
/>
</div>
: null
: getInvoce.data?.data?.invoice?.discount &&
<div className='flex -mt-1 gap-2 items-center w-full'>
<div className='w-full xl:w-fit'>
<Button
label={t('receip.remove_discount')}
className='bg-transparent text-black border border-black mt-1 px-5 w-fit'
onClick={handleRemoveDiscount}
/>
</div>
</div>
}
</div>
@@ -126,11 +171,16 @@ const ReceiptsDetail: FC = () => {
<span>{NumberFormat(getInvoce.data?.data?.invoice?.tax)}</span> تومان
</div>
</div>
<div className='flex justify-between items-center text-xs border-t border-[#EAEDF5] h-14'>
<div className='flex justify-between gap-1 items-center text-xs border-t border-[#EAEDF5] h-14'>
<div>
جمع کل
</div>
<div>
<div className='flex gap-1'>
{
getInvoce.data?.data?.invoice?.originalPrice !== getInvoce.data?.data?.invoice?.totalPrice && (
<><span className='line-through'>{NumberFormat(getInvoce.data?.data?.invoice?.originalPrice)}</span></>
)
}
<span>{NumberFormat(getInvoce.data?.data?.invoice?.totalPrice)}</span> تومان
</div>
</div>
@@ -3,6 +3,7 @@ import * as api from "../service/ReceiptService";
import {
ApplyDiscountType,
ApproveInvoiceType,
RemoveDiscountType,
RequestApproveInvoiceType,
} from "../types/ReceiptTypes";
@@ -52,3 +53,10 @@ export const useApplyDiscount = () => {
mutationFn: (variables: ApplyDiscountType) => api.applyDiscount(variables),
});
};
export const useRemoveDiscount = () => {
return useMutation({
mutationFn: (variables: RemoveDiscountType) =>
api.removeDiscount(variables.id),
});
};
@@ -40,3 +40,8 @@ export const applyDiscount = async (params: ApplyDiscountType) => {
);
return data;
};
export const removeDiscount = async (id: string) => {
const { data } = await axios.post(`/invoices/${id}/cancel-discount`);
return data;
};
+5 -1
View File
@@ -44,5 +44,9 @@ export type ApproveInvoiceType = {
export type ApplyDiscountType = {
id: string;
discountCode: string;
code: string;
};
export type RemoveDiscountType = {
id: string;
};