import Area from '@components/common/Area.js'; import { Card, CardContent, CardHeader, CardTitle } from '@components/common/ui/Card.js'; import { useCartDispatch, useCartState } from '@components/frontStore/cart/CartContext.js'; import { useCheckout, useCheckoutDispatch } from '@components/frontStore/checkout/CheckoutContext.js'; import { BillingAddress } from '@components/frontStore/checkout/payment/BillingAddress.js'; import { PaymentMethods } from '@components/frontStore/checkout/payment/PaymentMethods.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { CreditCard } from 'lucide-react'; import React, { useEffect } from 'react'; import { useWatch } from 'react-hook-form'; import { toast } from 'react-toastify'; export function Payment() { const { data: { noShippingRequired, billingAddress, availablePaymentMethods }, loadingStates: { addingBillingAddress } } = useCartState(); const { addBillingAddress } = useCartDispatch(); const { updateCheckoutData } = useCheckoutDispatch(); const { form } = useCheckout(); const paymentMethod = useWatch({ name: 'paymentMethod', control: form.control }); useEffect(() => { const updatePaymentMethod = async () => { try { const paymentMethod = form.getValues('paymentMethod'); const methodDetails = availablePaymentMethods?.find( (method) => method.code === paymentMethod ); if (!methodDetails) { throw new Error('Please select a valid payment method'); } updateCheckoutData({ paymentMethod: methodDetails.code }); } catch (error) { toast.error( error instanceof Error ? error.message : _('Failed to update shipment') ); } }; if (paymentMethod) { updatePaymentMethod(); } }, [paymentMethod]); return ( <>
{_('Payment Information')}
{(billingAddress || noShippingRequired === false) && ( <> ({ ...method }))} isLoading={addingBillingAddress} /> )}
); }