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
+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>