add discount and remove discount
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
VITE_TOKEN_NAME = 'dsc_token'
|
VITE_TOKEN_NAME = 'dsc_token'
|
||||||
VITE_REFRESH_TOKEN_NAME = 'dsc_refresh_token'
|
VITE_REFRESH_TOKEN_NAME = 'dsc_refresh_token'
|
||||||
VITE_BASE_URL = 'https://api.danakcorp.com'
|
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
@@ -256,7 +256,11 @@
|
|||||||
"thank_pay_2": "مبلغ این صورت حساب پرداخت شده است.",
|
"thank_pay_2": "مبلغ این صورت حساب پرداخت شده است.",
|
||||||
"discount_code": "کد تخفیف",
|
"discount_code": "کد تخفیف",
|
||||||
"apply": "اعمال",
|
"apply": "اعمال",
|
||||||
"pending": "تایید نشده"
|
"discount_applied": "کد تخفیف با موفقیت اعمال شد",
|
||||||
|
"discount_error": "کد تخفیف معتبر نیست",
|
||||||
|
"pending": "در انتظار تایید",
|
||||||
|
"discount_removed": "کد تخفیف با موفقیت حذف شد",
|
||||||
|
"remove_discount": "حذف تخفیف"
|
||||||
},
|
},
|
||||||
"active": "فعال",
|
"active": "فعال",
|
||||||
"inactive": "غیرفعال",
|
"inactive": "غیرفعال",
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { FC } from 'react'
|
import { FC, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { useParams } from 'react-router-dom'
|
import { useParams } from 'react-router-dom'
|
||||||
import { useGetInvoiceDetail } from './hooks/useReceiptData'
|
import { useApplyDiscount, useGetInvoiceDetail, useRemoveDiscount } from './hooks/useReceiptData'
|
||||||
import { ReceiptDetailItemType } from './types/ReceiptTypes'
|
import { ApplyDiscountType, ReceiptDetailItemType } from './types/ReceiptTypes'
|
||||||
import { NumberFormat } from '../../config/func'
|
import { NumberFormat } from '../../config/func'
|
||||||
import moment from 'moment-jalaali'
|
import moment from 'moment-jalaali'
|
||||||
import PageLoading from '../../components/PageLoading'
|
import PageLoading from '../../components/PageLoading'
|
||||||
@@ -16,13 +16,45 @@ import PayInvoice from './components/PayInvoice'
|
|||||||
import Input from '../../components/Input'
|
import Input from '../../components/Input'
|
||||||
import { Helmet } from 'react-helmet-async'
|
import { Helmet } from 'react-helmet-async'
|
||||||
import { useGetProfile } from '../profile/hooks/useProfileData'
|
import { useGetProfile } from '../profile/hooks/useProfileData'
|
||||||
|
import { toast } from 'react-toastify'
|
||||||
|
import { ErrorType } from '../../helpers/types'
|
||||||
|
|
||||||
const ReceiptsDetail: FC = () => {
|
const ReceiptsDetail: FC = () => {
|
||||||
|
|
||||||
const { id } = useParams()
|
const { id } = useParams()
|
||||||
const { t } = useTranslation('global')
|
const { t } = useTranslation('global')
|
||||||
|
const [code, setCode] = useState<string>('')
|
||||||
const getProfile = useGetProfile()
|
const getProfile = useGetProfile()
|
||||||
const getInvoce = useGetInvoiceDetail(id ? id : '')
|
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 (
|
return (
|
||||||
<div className='mt-4'>
|
<div className='mt-4'>
|
||||||
@@ -39,21 +71,34 @@ const ReceiptsDetail: FC = () => {
|
|||||||
</div>
|
</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='flex -mt-1 gap-2 items-center w-full'>
|
||||||
<div className='w-full xl:w-fit'>
|
<div className='w-full xl:w-fit'>
|
||||||
<Input
|
<Input
|
||||||
className='bg-transparent border border-border xl:max-w-40'
|
className='bg-transparent border border-border xl:max-w-40'
|
||||||
placeholder={t('receip.discount_code')}
|
placeholder={t('receip.discount_code')}
|
||||||
|
value={code}
|
||||||
|
onChange={(e) => setCode(e.target.value)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
label={t('receip.apply')}
|
label={t('receip.apply')}
|
||||||
className='bg-transparent text-black border border-black mt-1 px-5 w-fit'
|
className='bg-transparent text-black border border-black mt-1 px-5 w-fit'
|
||||||
|
onClick={handleApplyDiscount}
|
||||||
|
isLoading={applyDiscount.isPending}
|
||||||
/>
|
/>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
|
|
||||||
@@ -126,11 +171,16 @@ const ReceiptsDetail: FC = () => {
|
|||||||
<span>{NumberFormat(getInvoce.data?.data?.invoice?.tax)}</span> تومان
|
<span>{NumberFormat(getInvoce.data?.data?.invoice?.tax)}</span> تومان
|
||||||
</div>
|
</div>
|
||||||
</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>
|
||||||
<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> تومان
|
<span>{NumberFormat(getInvoce.data?.data?.invoice?.totalPrice)}</span> تومان
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import * as api from "../service/ReceiptService";
|
|||||||
import {
|
import {
|
||||||
ApplyDiscountType,
|
ApplyDiscountType,
|
||||||
ApproveInvoiceType,
|
ApproveInvoiceType,
|
||||||
|
RemoveDiscountType,
|
||||||
RequestApproveInvoiceType,
|
RequestApproveInvoiceType,
|
||||||
} from "../types/ReceiptTypes";
|
} from "../types/ReceiptTypes";
|
||||||
|
|
||||||
@@ -52,3 +53,10 @@ export const useApplyDiscount = () => {
|
|||||||
mutationFn: (variables: ApplyDiscountType) => api.applyDiscount(variables),
|
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;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const removeDiscount = async (id: string) => {
|
||||||
|
const { data } = await axios.post(`/invoices/${id}/cancel-discount`);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|||||||
@@ -44,5 +44,9 @@ export type ApproveInvoiceType = {
|
|||||||
|
|
||||||
export type ApplyDiscountType = {
|
export type ApplyDiscountType = {
|
||||||
id: string;
|
id: string;
|
||||||
discountCode: string;
|
code: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RemoveDiscountType = {
|
||||||
|
id: string;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user