return order + success page return order + ...
This commit is contained in:
@@ -2,19 +2,96 @@
|
||||
import CartSummary from '@/components/CartSummary'
|
||||
import Layout from '@/hoc/Layout'
|
||||
import { NextPage } from 'next'
|
||||
import CartItem from '../components/CartItem'
|
||||
// import CartItem from '../components/CartItem'
|
||||
import TitleBack from '../components/TitleBack'
|
||||
import ShippingMethodSelector from '../components/ShippingMethodSelector'
|
||||
import { Location } from 'iconsax-react'
|
||||
import { useGetCart, useShippingCost } from '../hooks/useCartData'
|
||||
import { CartItemType } from '../types/Types'
|
||||
import { useGetCart, useSaveShipmentCart, useShippingCost } from '../hooks/useCartData'
|
||||
// import { CartItemType } from '../types/Types'
|
||||
import { useGetProfile } from '@/app/profile/hooks/useProfileData'
|
||||
import Link from 'next/link'
|
||||
import { useState, useEffect } from 'react'
|
||||
|
||||
const CartShipping: NextPage = () => {
|
||||
|
||||
const { data: cartData, isLoading: cartLoading, error: cartError } = useGetCart()
|
||||
const { data: shippingData, isLoading: shippingLoading, error: shippingError } = useShippingCost()
|
||||
const { data: profileData, isLoading: profileLoading, error: profileError } = useGetProfile()
|
||||
const { mutate: saveShipmentCart } = useSaveShipmentCart()
|
||||
|
||||
// State برای ذخیره انتخابهای روش ارسال
|
||||
const [selectedShipments, setSelectedShipments] = useState<Record<string, number>>({})
|
||||
const [isSavingShipment, setIsSavingShipment] = useState(false)
|
||||
|
||||
// بارگذاری انتخابهای قبلی از localStorage
|
||||
useEffect(() => {
|
||||
const savedShipments = localStorage.getItem('selectedShipments')
|
||||
if (savedShipments) {
|
||||
try {
|
||||
setSelectedShipments(JSON.parse(savedShipments))
|
||||
} catch (error) {
|
||||
console.error('Error parsing saved shipments:', error)
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
|
||||
// ذخیره انتخابها در localStorage
|
||||
useEffect(() => {
|
||||
if (Object.keys(selectedShipments).length > 0) {
|
||||
localStorage.setItem('selectedShipments', JSON.stringify(selectedShipments))
|
||||
}
|
||||
}, [selectedShipments])
|
||||
|
||||
// هندلر انتخاب روش ارسال
|
||||
const handleShipmentSelect = (shopId: string, shipperId: number) => {
|
||||
setSelectedShipments(prev => ({
|
||||
...prev,
|
||||
[shopId]: shipperId
|
||||
}))
|
||||
}
|
||||
|
||||
// چک کردن اینکه آیا همه فروشگاهها روش ارسال انتخاب کردهاند
|
||||
const allShipmentsSelected = shippingData?.results?.shipping?.every(shop =>
|
||||
selectedShipments[shop.shopId] !== undefined
|
||||
) || false
|
||||
|
||||
// هندلر پاک کردن همه انتخابها
|
||||
const handleClearSelections = () => {
|
||||
setSelectedShipments({})
|
||||
localStorage.removeItem('selectedShipments')
|
||||
}
|
||||
|
||||
// هندلر ذخیره انتخابهای ارسال و انتقال به صفحه پرداخت
|
||||
const handleProceedToPayment = async () => {
|
||||
if (!allShipmentsSelected) return
|
||||
|
||||
setIsSavingShipment(true)
|
||||
|
||||
try {
|
||||
// تبدیل selectedShipments به فرمت مورد نیاز API
|
||||
const shipmentsInfo = Object.entries(selectedShipments).map(([shopId, shipperId]) => ({
|
||||
shopId,
|
||||
shipmentId: shipperId
|
||||
}))
|
||||
|
||||
// ذخیره انتخابها در API
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
saveShipmentCart({ shipmentsInfo }, {
|
||||
onSuccess: () => resolve(),
|
||||
onError: (error) => reject(error)
|
||||
})
|
||||
})
|
||||
|
||||
// انتقال به صفحه پرداخت
|
||||
window.location.href = '/cart/payment'
|
||||
} catch (error) {
|
||||
console.error('Error saving shipment cart:', error)
|
||||
// در صورت خطا همچنان به صفحه پرداخت برویم
|
||||
window.location.href = '/cart/payment'
|
||||
} finally {
|
||||
setIsSavingShipment(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (cartLoading || shippingLoading || profileLoading) {
|
||||
return (
|
||||
@@ -50,9 +127,11 @@ const CartShipping: NextPage = () => {
|
||||
)
|
||||
}
|
||||
|
||||
// محاسبه هزینه ارسال
|
||||
// محاسبه هزینه ارسال بر اساس انتخابهای کاربر
|
||||
const totalShippingCost = shippingData?.results?.shipping?.reduce((total, shop) => {
|
||||
return total + (shop.shippers?.[0]?.totalShippingCost || 0)
|
||||
const selectedShipperId = selectedShipments[shop.shopId]
|
||||
const selectedShipper = shop.shippers?.find(shipper => shipper.shipperId === selectedShipperId)
|
||||
return total + (selectedShipper?.totalShippingCost || shop.shippers?.[0]?.totalShippingCost || 0)
|
||||
}, 0) || 0
|
||||
|
||||
// محاسبه قیمت نهایی
|
||||
@@ -82,7 +161,15 @@ const CartShipping: NextPage = () => {
|
||||
</div>
|
||||
<Link href={'/profile'} className='text-primary text-sm self-end sm:self-auto'>ویرایش</Link>
|
||||
</div>
|
||||
<div className="flex-1 mt-5 border border-border p-4 sm:p-5 rounded-2xl">
|
||||
|
||||
<ShippingMethodSelector
|
||||
shippingData={shippingData?.results?.shipping || []}
|
||||
selectedShipments={selectedShipments}
|
||||
onShipmentSelect={handleShipmentSelect}
|
||||
onClearSelections={handleClearSelections}
|
||||
/>
|
||||
|
||||
{/* <div className="flex-1 mt-5 border border-border p-4 sm:p-5 rounded-2xl">
|
||||
{items.map((item: CartItemType, index: number) => {
|
||||
if (!item || !item.product || !item.variant) {
|
||||
return null
|
||||
@@ -95,7 +182,7 @@ const CartShipping: NextPage = () => {
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div> */}
|
||||
</div>
|
||||
<div className="w-full lg:w-[360px] lg:flex-shrink-0">
|
||||
<CartSummary
|
||||
@@ -104,10 +191,17 @@ const CartShipping: NextPage = () => {
|
||||
discount={cart?.total_discount || 0}
|
||||
shippingCost={totalShippingCost}
|
||||
total={finalTotal}
|
||||
confirmHref="/cart/payment"
|
||||
onConfirm={handleProceedToPayment}
|
||||
confirmLabel="پرداخت"
|
||||
className="w-full"
|
||||
confirmDisabled={!allShipmentsSelected}
|
||||
isLoading={isSavingShipment}
|
||||
/>
|
||||
{!allShipmentsSelected && (
|
||||
<div className="mt-2 text-sm text-red-600 text-center">
|
||||
لطفا برای همه فروشگاهها روش ارسال انتخاب کنید
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user