| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- 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 (
- <div className="grid gap-1.5">
- <label htmlFor={id} className="text-sm font-medium">
- {label}
- </label>
- {children}
- {error && (
- <p id={`${id}-error`} className="text-sm text-red-700">
- {error}
- </p>
- )}
- </div>
- );
- }
-
- /** The shopper's details and address, before going to the payment page. */
- export function CheckoutPage({
- states,
- shippingByState,
- }: {
- states: string[];
- shippingByState: Record<string, number>;
- }) {
- 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<HTMLInputElement> = {},
- ) {
- return (
- <Field id={name} label={label} error={form.errors[name]}>
- <input
- id={name}
- name={name}
- value={form.data[name]}
- onChange={(event) => form.setData(name, event.target.value)}
- aria-invalid={form.errors[name] ? true : undefined}
- aria-describedby={
- form.errors[name] ? `${name}-error` : undefined
- }
- className={inputClass}
- {...props}
- />
- </Field>
- );
- }
-
- return (
- <main className="py-10 md:py-14">
- <Container>
- <SfLink
- href="/cart"
- className="text-sm text-(--sf-muted) transition-colors hover:text-(--sf-fg)"
- >
- ← {t.backToCart}
- </SfLink>
-
- <h1 className="sf-heading mt-4 text-4xl font-semibold sm:text-5xl">
- {t.checkout}
- </h1>
-
- <div className="mt-10 grid gap-10 lg:grid-cols-[1fr_360px] lg:gap-14">
- <form onSubmit={submit} className="space-y-10" noValidate>
- {errors.cart && (
- <p
- role="alert"
- className="rounded-(--sf-radius-card) border border-red-300 bg-red-50 p-4 text-sm text-red-800"
- >
- {errors.cart}
- </p>
- )}
-
- <fieldset className="space-y-5">
- <legend className="sf-heading text-xl font-semibold">
- {t.contactDetails}
- </legend>
- {text('name', t.fullName, {
- autoComplete: 'name',
- required: true,
- })}
- <div className="grid gap-5 sm:grid-cols-2">
- {text('email', t.email, {
- type: 'email',
- autoComplete: 'email',
- required: true,
- })}
- {text('phone', t.phone, {
- type: 'tel',
- autoComplete: 'tel',
- required: true,
- })}
- </div>
- </fieldset>
-
- <fieldset className="space-y-5">
- <legend className="sf-heading text-xl font-semibold">
- {t.deliveryAddress}
- </legend>
- {text('line1', t.addressLine1, {
- autoComplete: 'address-line1',
- required: true,
- })}
- {text('line2', t.addressLine2, {
- autoComplete: 'address-line2',
- })}
- <div className="grid gap-5 sm:grid-cols-3">
- {text('postcode', t.postcode, {
- inputMode: 'numeric',
- autoComplete: 'postal-code',
- maxLength: 5,
- required: true,
- })}
- {text('city', t.city, {
- autoComplete: 'address-level2',
- required: true,
- })}
- <Field
- id="state"
- label={t.state}
- error={form.errors.state}
- >
- <select
- id="state"
- name="state"
- value={form.data.state}
- onChange={(event) =>
- form.setData(
- 'state',
- event.target.value,
- )
- }
- autoComplete="address-level1"
- required
- aria-invalid={
- form.errors.state ? true : undefined
- }
- className={inputClass}
- >
- <option value="" disabled>
- {t.chooseState}
- </option>
- {states.map((state) => (
- <option key={state} value={state}>
- {state}
- </option>
- ))}
- </select>
- </Field>
- </div>
- </fieldset>
-
- <div>
- <button
- type="submit"
- disabled={form.processing}
- className="sf-btn-primary inline-flex w-full items-center justify-center rounded-(--sf-radius) bg-(--sf-primary) px-6 py-3 text-sm font-semibold text-(--sf-primary-fg) transition-opacity hover:opacity-85 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--sf-primary) disabled:cursor-not-allowed disabled:opacity-50 sm:w-auto"
- >
- {t.payNow}
- </button>
- <p className="mt-2 text-xs text-(--sf-muted)">
- {t.paySecure}
- </p>
- </div>
- </form>
-
- <aside className="sf-card h-fit rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-6">
- <h2 className="font-medium">{t.orderSummary}</h2>
- <ul className="mt-4 space-y-3 text-sm">
- {cart.lines.map((line) => (
- <li
- key={line.key}
- className="flex justify-between gap-4"
- >
- <span>
- {line.product.name}
- {line.variant &&
- ` (${line.variant.name})`}{' '}
- <span className="text-(--sf-muted)">
- × {line.quantity}
- </span>
- </span>
- <span className="tabular-nums">
- {formatPrice(line.line_total)}
- </span>
- </li>
- ))}
- </ul>
- <div className="mt-5 space-y-2 border-t border-(--sf-border) pt-4 text-sm">
- <div className="flex justify-between gap-4">
- <span>{t.subtotal}</span>
- <span className="tabular-nums">
- {formatPrice(cart.subtotal)}
- </span>
- </div>
- {cart.applied && (
- <div className="flex justify-between gap-4">
- <span>
- {t.discount} ({cart.applied.name})
- </span>
- <span className="tabular-nums">
- −{formatPrice(cart.applied.amount)}
- </span>
- </div>
- )}
- <div className="flex justify-between gap-4">
- <span>{t.shipping}</span>
- <span
- className="tabular-nums"
- aria-live="polite"
- >
- {shipping === null
- ? '—'
- : shipping === 0
- ? t.shippingFree
- : formatPrice(shipping)}
- </span>
- </div>
- {shipping === null && (
- <p className="text-xs text-(--sf-muted)">
- {t.chooseStateForShipping}
- </p>
- )}
- </div>
- <div className="mt-4 flex items-baseline justify-between gap-4 border-t border-(--sf-border) pt-4">
- <span className="font-medium">{t.total}</span>
- <span className="text-xl font-semibold tabular-nums">
- {formatPrice(cart.total + (shipping ?? 0))}
- </span>
- </div>
- </aside>
- </div>
- </Container>
- </main>
- );
- }
|