payment section

This commit is contained in:
hamid zarghami
2025-12-03 16:16:18 +03:30
parent bf756e64e7
commit 77535934d5
4 changed files with 33 additions and 8 deletions
@@ -92,9 +92,9 @@ export const AddressSection = () => {
} }
// اگر آدرس انتخاب شده وجود ندارد، نمایش نده // اگر آدرس انتخاب شده وجود ندارد، نمایش نده
if (!selectedAddress) { // if (!selectedAddress) {
return null; // return null;
} // }
return ( return (
<> <>
@@ -112,9 +112,9 @@ export const AddressSection = () => {
</div> </div>
<p className='mt-6 text-sm2'> <p className='mt-6 text-sm2'>
<div className='font-bold'> <div className='font-bold'>
{selectedAddress.title} {selectedAddress?.title}
</div> </div>
{formatAddress(selectedAddress)} {selectedAddress && formatAddress(selectedAddress)}
</p> </p>
</div> </div>
</section> </section>
@@ -3,14 +3,16 @@
import { Card, Icon, Wallet2 } from 'iconsax-react'; import { Card, Icon, Wallet2 } from 'iconsax-react';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useGetPaymentMethod } from '../../hooks/useOrderData'; import { useGetPaymentMethod, useSetPaymentMethod } from '../../hooks/useOrderData';
import { useCheckoutStore } from '../../store/Store';
type PaymentSectionProps = { type PaymentSectionProps = {
paymentType: string; paymentType: string;
onPaymentTypeChange: (id: string) => void; onPaymentTypeChange: (id: string) => void;
}; };
const getIconByMethod = (method: "CardOnDelivery" | "Cash" | "Online", gateway: string | null): Icon => { // eslint-disable-next-line @typescript-eslint/no-unused-vars
const getIconByMethod = (method: "CardOnDelivery" | "Cash" | "Online", _gateway: string | null): Icon => {
if (method === "Online" || method === "CardOnDelivery") { if (method === "Online" || method === "CardOnDelivery") {
return Card; return Card;
} }
@@ -24,6 +26,8 @@ export const PaymentSection = ({ paymentType, onPaymentTypeChange }: PaymentSect
const { t } = useTranslation('parallels', { keyPrefix: 'OrderDetail' }); const { t } = useTranslation('parallels', { keyPrefix: 'OrderDetail' });
const { data: paymentMethod } = useGetPaymentMethod(); const { data: paymentMethod } = useGetPaymentMethod();
const { mutate: setPaymentMethod } = useSetPaymentMethod();
const { setIsSelectedPayment } = useCheckoutStore();
const paymentOptions = useMemo(() => { const paymentOptions = useMemo(() => {
if (!paymentMethod?.data) return []; if (!paymentMethod?.data) return [];
@@ -56,7 +60,14 @@ export const PaymentSection = ({ paymentType, onPaymentTypeChange }: PaymentSect
</label> </label>
<input <input
checked={paymentType === id} checked={paymentType === id}
onChange={() => onPaymentTypeChange(id)} onChange={() => {
onPaymentTypeChange(id);
setPaymentMethod(id, {
onSuccess: () => {
setIsSelectedPayment(true);
},
});
}}
type='radio' type='radio'
name='paymentMethod' name='paymentMethod'
id={`paymentMethod${id}`} id={`paymentMethod${id}`}
@@ -4,6 +4,7 @@ import {
getShipmentMethod, getShipmentMethod,
setAddressCart, setAddressCart,
setDeliveryMethod, setDeliveryMethod,
setPaymentMethod,
} from "../service/OrderService"; } from "../service/OrderService";
export const useGetShipmentMethod = () => { export const useGetShipmentMethod = () => {
@@ -31,3 +32,9 @@ export const useSetDeliveryMethod = () => {
mutationFn: setDeliveryMethod, mutationFn: setDeliveryMethod,
}); });
}; };
export const useSetPaymentMethod = () => {
return useMutation({
mutationFn: setPaymentMethod,
});
};
@@ -29,3 +29,10 @@ export const setDeliveryMethod = async (id: string) => {
}); });
return data; return data;
}; };
export const setPaymentMethod = async (id: string) => {
const { data } = await api.patch("/public/cart/payment-method", {
paymentMethodId: id,
});
return data;
};