diff --git a/src/app/[name]/(Dialogs)/cart/types/Types.ts b/src/app/[name]/(Dialogs)/cart/types/Types.ts index 15ced96..c796291 100644 --- a/src/app/[name]/(Dialogs)/cart/types/Types.ts +++ b/src/app/[name]/(Dialogs)/cart/types/Types.ts @@ -31,6 +31,9 @@ export interface CartData { totalItems: number; createdAt: string; updatedAt: string; + deliveryMethodId?: string; + addressId?: string; + paymentMethodId?: string; } export interface CartCategory { diff --git a/src/app/[name]/(Dialogs)/order/checkout/[id]/page.tsx b/src/app/[name]/(Dialogs)/order/checkout/[id]/page.tsx index 49a2aa2..c20b363 100644 --- a/src/app/[name]/(Dialogs)/order/checkout/[id]/page.tsx +++ b/src/app/[name]/(Dialogs)/order/checkout/[id]/page.tsx @@ -1,7 +1,7 @@ 'use client'; import useToggle from '@/hooks/helpers/useToggle'; -import React, { useCallback, useMemo, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { AddressSection } from './components/AddressSection'; import { CheckoutHeader } from './components/CheckoutHeader'; import { CouponSection } from './components/CouponSection'; @@ -9,9 +9,14 @@ import { PaymentSection } from './components/PaymentSection'; import { ShippingSection } from './components/ShippingSection'; import { SummarySection } from './components/SummarySection'; import { TableSection } from './components/TableSection'; -import { useGetShipmentMethod } from '../hooks/useOrderData'; +import { useGetShipmentMethod, useSetAddressCart, useSetPaymentMethod } from '../hooks/useOrderData'; +import { useGetCartItems } from '@/app/[name]/(Dialogs)/cart/hooks/useCartData'; +import { useGetProfile } from '@/app/[name]/(Profile)/profile/hooks/userProfileData'; +import { useCheckoutStore } from '../store/Store'; function OrderDetailInex() { + const { isSuccess } = useGetProfile(); + const { data: cartItems } = useGetCartItems(isSuccess); const [deliveryType, setDeliveryType] = React.useState('0'); const [paymentType, setPaymentType] = React.useState('0'); const [couponType, setCouponType] = React.useState('0'); @@ -20,6 +25,9 @@ function OrderDetailInex() { const [tableNumber, setTableNumber] = useState(0); const { state: cartModal, toggle: toggleCartModal } = useToggle(); const { data: shipmentMethod } = useGetShipmentMethod(); + const { mutate: setAddressCart } = useSetAddressCart(); + const { mutate: setPaymentMethod } = useSetPaymentMethod(); + const { setSelectedAddressId, setIsSelectedAddress, setIsSelectedPayment } = useCheckoutStore(); const incrementTableNumber = () => setTableNumber((prev) => prev + 1); const decrementTableNumber = () => setTableNumber((prev) => prev - 1); @@ -42,6 +50,35 @@ function OrderDetailInex() { setCouponCode(value); }, []); + useEffect(() => { + if (cartItems?.data?.deliveryMethodId) { + setDeliveryType(cartItems.data.deliveryMethodId); + } + }, [cartItems?.data?.deliveryMethodId]); + + useEffect(() => { + if (cartItems?.data?.paymentMethodId) { + setPaymentType(cartItems.data.paymentMethodId); + setPaymentMethod(cartItems.data.paymentMethodId, { + onSuccess: () => { + setIsSelectedPayment(true); + }, + }); + } + }, [cartItems?.data?.paymentMethodId, setPaymentMethod, setIsSelectedPayment]); + + useEffect(() => { + if (cartItems?.data?.addressId) { + const addressId = cartItems.data.addressId; + setAddressCart(addressId, { + onSuccess: () => { + setIsSelectedAddress(true); + setSelectedAddressId(addressId ?? null); + }, + }); + } + }, [cartItems?.data?.addressId, setAddressCart, setIsSelectedAddress, setSelectedAddressId]); + const selectedDeliveryMethod = useMemo(() => { if (!shipmentMethod?.data || !deliveryType) return null; return shipmentMethod.data.find((item) => item.id === deliveryType);