bulk action + payment
This commit is contained in:
@@ -5,7 +5,7 @@ import { NextPage } from 'next'
|
||||
import TitleBack from '../components/TitleBack'
|
||||
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
|
||||
import DiscountCard from '../components/DiscountCard'
|
||||
import { usePaymentMethods, useGetCart, useShippingCost } from '../hooks/useCartData'
|
||||
import { usePaymentMethods, useGetCart, useShippingCost, useApplyDiscountCode, useCheckoutCart } from '../hooks/useCartData'
|
||||
import { useState } from 'react'
|
||||
import { toast } from '@/components/Toast'
|
||||
import { useGetProfile } from '@/app/profile/hooks/useProfileData'
|
||||
@@ -16,9 +16,11 @@ const CartPayment: NextPage = () => {
|
||||
const { data: shippingData, isLoading: shippingLoading } = useShippingCost()
|
||||
const { data: profileData, isLoading: profileLoading } = useGetProfile()
|
||||
|
||||
const applyDiscountMutation = useApplyDiscountCode()
|
||||
const checkoutMutation = useCheckoutCart()
|
||||
|
||||
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState<string>('')
|
||||
const [discountCode, setDiscountCode] = useState<string>('')
|
||||
const [isApplyingDiscount, setIsApplyingDiscount] = useState(false)
|
||||
|
||||
const isLoading = paymentMethodsLoading || cartLoading || shippingLoading || profileLoading
|
||||
|
||||
@@ -64,15 +66,13 @@ const CartPayment: NextPage = () => {
|
||||
return
|
||||
}
|
||||
|
||||
setIsApplyingDiscount(true)
|
||||
try {
|
||||
// TODO: پیادهسازی API اعمال کد تخفیف
|
||||
// await applyDiscountCode(discountCode)
|
||||
await applyDiscountMutation.mutateAsync({ discount_code: discountCode })
|
||||
toast('کد تخفیف با موفقیت اعمال شد', 'success')
|
||||
} catch {
|
||||
toast('کد تخفیف نامعتبر است', 'error')
|
||||
} finally {
|
||||
setIsApplyingDiscount(false)
|
||||
setDiscountCode('')
|
||||
} catch (error: unknown) {
|
||||
const errorMessage = (error as { response?: { data?: { message?: string } } })?.response?.data?.message || 'کد تخفیف نامعتبر است'
|
||||
toast(errorMessage, 'error')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,16 +87,14 @@ const CartPayment: NextPage = () => {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// TODO: پیادهسازی API پرداخت نهایی
|
||||
// await processPayment({
|
||||
// paymentMethodId: selectedPaymentMethod,
|
||||
// discountCode: discountCode || null,
|
||||
// shippingAddress: profile.userAddress
|
||||
// })
|
||||
toast('پرداخت با موفقیت انجام شد', 'success')
|
||||
} catch {
|
||||
toast('خطا در پردازش پرداخت', 'error')
|
||||
const response = await checkoutMutation.mutateAsync({
|
||||
payment_method_id: selectedPaymentMethod
|
||||
})
|
||||
|
||||
if (response?.results?.paymentData?.redirectUrl) {
|
||||
window.location.href = response.results.paymentData.redirectUrl
|
||||
} else {
|
||||
toast('خطا در دریافت لینک پرداخت', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +107,7 @@ const CartPayment: NextPage = () => {
|
||||
discountCode={discountCode}
|
||||
setDiscountCode={setDiscountCode}
|
||||
onSubmit={handleDiscountCodeSubmit}
|
||||
isLoading={isApplyingDiscount}
|
||||
isLoading={applyDiscountMutation.isPending}
|
||||
/>
|
||||
<div className='mt-5 border border-border rounded-2xl p-4 sm:p-6 font-light'>
|
||||
<div>انتخاب روش پرداخت</div>
|
||||
@@ -153,6 +151,7 @@ const CartPayment: NextPage = () => {
|
||||
onConfirm={handlePayment}
|
||||
confirmLabel="پرداخت"
|
||||
className="w-full"
|
||||
isLoading={checkoutMutation.isPending}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user