import { useForm } from '@inertiajs/react'; import type { FormEvent, ReactNode } from 'react'; import { useStorefront } from '../context'; import { storefrontText } from '../text'; import { formatPrice } from '../theme'; import { Container, SfLink } from '../ui'; const inputClass = 'w-full rounded-(--sf-radius) border border-(--sf-border) bg-(--sf-bg) px-3 py-2.5 text-base text-(--sf-fg) outline-none focus-visible:border-(--sf-primary) focus-visible:ring-2 focus-visible:ring-(--sf-primary)/30'; function Field({ id, label, error, children, }: { id: string; label: string; error?: string; children: ReactNode; }) { return (
{children} {error && (

{error}

)}
); } /** The shopper's details and address, before going to the payment page. */ export function CheckoutPage({ states, shippingByState, }: { states: string[]; shippingByState: Record; }) { const { cart, language, baseUrl } = useStorefront(); const t = storefrontText(language); const form = useForm({ name: '', email: '', phone: '', line1: '', line2: '', city: '', postcode: '', state: '', }); // "cart" errors come from the server: an empty cart, or the payment page could not be opened. const errors = form.errors as typeof form.errors & { cart?: string }; const shipping = form.data.state ? (shippingByState[form.data.state] ?? null) : null; function submit(event: FormEvent) { event.preventDefault(); form.post(`${baseUrl}/checkout`, { preserveScroll: true }); } function text( name: keyof typeof form.data, label: string, props: React.InputHTMLAttributes = {}, ) { return ( form.setData(name, event.target.value)} aria-invalid={form.errors[name] ? true : undefined} aria-describedby={ form.errors[name] ? `${name}-error` : undefined } className={inputClass} {...props} /> ); } return (
← {t.backToCart}

{t.checkout}

{errors.cart && (

{errors.cart}

)}
{t.contactDetails} {text('name', t.fullName, { autoComplete: 'name', required: true, })}
{text('email', t.email, { type: 'email', autoComplete: 'email', required: true, })} {text('phone', t.phone, { type: 'tel', autoComplete: 'tel', required: true, })}
{t.deliveryAddress} {text('line1', t.addressLine1, { autoComplete: 'address-line1', required: true, })} {text('line2', t.addressLine2, { autoComplete: 'address-line2', })}
{text('postcode', t.postcode, { inputMode: 'numeric', autoComplete: 'postal-code', maxLength: 5, required: true, })} {text('city', t.city, { autoComplete: 'address-level2', required: true, })}

{t.paySecure}

); }