'use client'; import { Accordion, AccordionItem, Checkbox, Radio, RadioGroup } from '@nextui-org/react'; import { useCart } from 'components/cart/cart-context'; import CartItemView from 'components/cart/cart-item'; import { useCheckout } from 'components/checkout/checkout-provider'; import ShippingForm from 'components/checkout/form'; import Price from 'components/price'; import { Billing } from 'lib/woocomerce/models/billing'; import { PaymentGateways } from 'lib/woocomerce/models/payment'; import { Shipping } from 'lib/woocomerce/models/shipping'; import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; import { z } from 'zod'; const shippingSchema = z.object({ first_name: z.string().min(3), last_name: z.string().min(3), address_1: z.string().min(3), address_2: z.string().optional(), city: z.string().min(3), state: z.string().max(2).min(2), postcode: z.string().min(3), country: z.string().min(3), company: z.string().optional() }); export default function CheckoutPage() { const { cart } = useCart(); const router = useRouter(); const { checkout, setShipping, setBilling, setPayment } = useCheckout(); const [error, setError] = useState(undefined); const [sameBilling, setSameBilling] = useState(true); const [paymentGateways, setPaymentGateways] = useState([]); useEffect(() => { const fetchPaymentGateways = async () => { const paymentGateways = await (await fetch('/api/payments')).json(); setPaymentGateways(paymentGateways); }; fetchPaymentGateways(); }, []); const onSubmit = async (e: React.FormEvent) => { console.log('submit'); e.preventDefault(); try { if (sameBilling) { setBilling({ ...checkout?.billing, ...checkout?.shipping } as Billing); } if (!checkout) { return; } shippingSchema.parse(checkout.shipping); router.push('/checkout/review'); } catch (error) { console.log(error); if (error instanceof z.ZodError) { const errorObj: Record = {}; error.errors.forEach((err) => { const key = err.path[0] as string; errorObj[key] = err.message; }); console.log(errorObj); setError(errorObj as Shipping); } } }; return (

Checkout

{cart && (
    {cart.items?.length && cart.items .sort((a, b) => a.name.localeCompare(b.name)) .map((item, i) => { return (
  • ); })}

Total

)}
{ const updatedShipping = { ...checkout?.shipping, [e.target.name]: e.target.value } as Shipping; setShipping(updatedShipping as Shipping); setError(undefined); }} error={error} /> setSameBilling(v)} className="mt-2"> Hai bisogno di fatturazione? { const updatedBilling = { ...checkout?.shipping, [e.target.name]: e.target.value } as Billing; setBilling(updatedBilling); setError(undefined); }} />
{paymentGateways.map((gateway: any) => ( { setPayment(e.target.value, gateway.title); }} > {gateway.title} ))}
); }