set default value in payment cart
This commit is contained in:
@@ -31,6 +31,9 @@ export interface CartData {
|
|||||||
totalItems: number;
|
totalItems: number;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
|
deliveryMethodId?: string;
|
||||||
|
addressId?: string;
|
||||||
|
paymentMethodId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CartCategory {
|
export interface CartCategory {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import useToggle from '@/hooks/helpers/useToggle';
|
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 { AddressSection } from './components/AddressSection';
|
||||||
import { CheckoutHeader } from './components/CheckoutHeader';
|
import { CheckoutHeader } from './components/CheckoutHeader';
|
||||||
import { CouponSection } from './components/CouponSection';
|
import { CouponSection } from './components/CouponSection';
|
||||||
@@ -9,9 +9,14 @@ import { PaymentSection } from './components/PaymentSection';
|
|||||||
import { ShippingSection } from './components/ShippingSection';
|
import { ShippingSection } from './components/ShippingSection';
|
||||||
import { SummarySection } from './components/SummarySection';
|
import { SummarySection } from './components/SummarySection';
|
||||||
import { TableSection } from './components/TableSection';
|
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() {
|
function OrderDetailInex() {
|
||||||
|
const { isSuccess } = useGetProfile();
|
||||||
|
const { data: cartItems } = useGetCartItems(isSuccess);
|
||||||
const [deliveryType, setDeliveryType] = React.useState<string>('0');
|
const [deliveryType, setDeliveryType] = React.useState<string>('0');
|
||||||
const [paymentType, setPaymentType] = React.useState<string>('0');
|
const [paymentType, setPaymentType] = React.useState<string>('0');
|
||||||
const [couponType, setCouponType] = React.useState<string>('0');
|
const [couponType, setCouponType] = React.useState<string>('0');
|
||||||
@@ -20,6 +25,9 @@ function OrderDetailInex() {
|
|||||||
const [tableNumber, setTableNumber] = useState(0);
|
const [tableNumber, setTableNumber] = useState(0);
|
||||||
const { state: cartModal, toggle: toggleCartModal } = useToggle();
|
const { state: cartModal, toggle: toggleCartModal } = useToggle();
|
||||||
const { data: shipmentMethod } = useGetShipmentMethod();
|
const { data: shipmentMethod } = useGetShipmentMethod();
|
||||||
|
const { mutate: setAddressCart } = useSetAddressCart();
|
||||||
|
const { mutate: setPaymentMethod } = useSetPaymentMethod();
|
||||||
|
const { setSelectedAddressId, setIsSelectedAddress, setIsSelectedPayment } = useCheckoutStore();
|
||||||
|
|
||||||
const incrementTableNumber = () => setTableNumber((prev) => prev + 1);
|
const incrementTableNumber = () => setTableNumber((prev) => prev + 1);
|
||||||
const decrementTableNumber = () => setTableNumber((prev) => prev - 1);
|
const decrementTableNumber = () => setTableNumber((prev) => prev - 1);
|
||||||
@@ -42,6 +50,35 @@ function OrderDetailInex() {
|
|||||||
setCouponCode(value);
|
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(() => {
|
const selectedDeliveryMethod = useMemo(() => {
|
||||||
if (!shipmentMethod?.data || !deliveryType) return null;
|
if (!shipmentMethod?.data || !deliveryType) return null;
|
||||||
return shipmentMethod.data.find((item) => item.id === deliveryType);
|
return shipmentMethod.data.find((item) => item.id === deliveryType);
|
||||||
|
|||||||
Reference in New Issue
Block a user