set default value in payment cart

This commit is contained in:
hamid zarghami
2025-12-07 10:44:53 +03:30
parent afbd680e73
commit a8fa442b3f
2 changed files with 42 additions and 2 deletions
@@ -31,6 +31,9 @@ export interface CartData {
totalItems: number;
createdAt: string;
updatedAt: string;
deliveryMethodId?: string;
addressId?: string;
paymentMethodId?: string;
}
export interface CartCategory {
@@ -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<string>('0');
const [paymentType, setPaymentType] = React.useState<string>('0');
const [couponType, setCouponType] = React.useState<string>('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);