| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { router } from '@inertiajs/react';
- import { useState } from 'react';
- import { useStorefront } from '../context';
- import { ProductImage } from '../product-card';
- import { storefrontText } from '../text';
- import { formatPrice } from '../theme';
- import type { StoreProduct } from '../types';
- import { Container, QuantityStepper, SfButton, SfLink } from '../ui';
-
- /** One product's own page. Built by us, in the store's colours and fonts. */
- export function ProductPage({ product }: { product: StoreProduct }) {
- const { language } = useStorefront();
- const t = storefrontText(language);
- const [selected, setSelected] = useState(0);
- const shown = product.images[selected] ?? product.image;
-
- return (
- <main className="py-10 md:py-14">
- <Container>
- <SfLink
- href="/shop"
- className="text-sm text-(--sf-muted) transition-colors hover:text-(--sf-fg)"
- >
- ← {t.backToShop}
- </SfLink>
-
- <div className="mt-6 grid gap-10 md:grid-cols-2 md:gap-14">
- <div>
- <ProductImage
- src={shown}
- alt={product.name}
- className="aspect-square w-full"
- />
-
- {product.images.length > 1 && (
- <ul className="mt-3 grid grid-cols-5 gap-3">
- {product.images.map((image, index) => (
- <li key={image}>
- <button
- type="button"
- onClick={() => setSelected(index)}
- aria-label={`${product.name} ${index + 1}`}
- aria-pressed={index === selected}
- className={`block w-full rounded-(--sf-radius-card) outline-offset-2 focus-visible:outline-2 focus-visible:outline-(--sf-primary) ${index === selected ? 'ring-2 ring-(--sf-primary)' : 'opacity-70 hover:opacity-100'}`}
- >
- <ProductImage
- src={image}
- alt=""
- className="aspect-square w-full"
- />
- </button>
- </li>
- ))}
- </ul>
- )}
- </div>
-
- <div>
- <h1 className="sf-heading text-3xl leading-tight font-semibold sm:text-4xl">
- {product.name}
- </h1>
-
- <p className="mt-4 text-2xl font-semibold tabular-nums">
- {formatPrice(product.price)}
- {product.compare_at_price !== null && (
- <s className="ml-3 text-base font-normal text-(--sf-muted)">
- {formatPrice(product.compare_at_price)}
- </s>
- )}
- </p>
-
- <p className="mt-2 text-sm text-(--sf-muted)">
- {product.in_stock ? t.inStock : t.soldOut}
- </p>
-
- {product.description && (
- <p className="mt-6 leading-relaxed whitespace-pre-line text-(--sf-muted)">
- {product.description}
- </p>
- )}
-
- <AddToCart product={product} />
- </div>
- </div>
- </Container>
- </main>
- );
- }
-
- function AddToCart({ product }: { product: StoreProduct }) {
- const { baseUrl, language, cart } = useStorefront();
- const t = storefrontText(language);
- const [quantity, setQuantity] = useState(1);
- const [busy, setBusy] = useState(false);
- const [added, setAdded] = useState(false);
- const [error, setError] = useState<string | null>(null);
-
- const inCart =
- cart.lines.find((line) => line.product.id === product.id)?.quantity ??
- 0;
- const max = Math.max(0, Math.min(product.stock_left, 99) - inCart);
- const canAdd = product.in_stock && max > 0;
-
- function add() {
- setError(null);
- router.post(
- `${baseUrl}/cart/items`,
- { product_id: product.id, quantity: Math.min(quantity, max) },
- {
- preserveScroll: true,
- onStart: () => setBusy(true),
- onFinish: () => setBusy(false),
- onSuccess: () => {
- setAdded(true);
- setQuantity(1);
- },
- onError: (errors) =>
- setError(errors.quantity ?? errors.product_id ?? null),
- },
- );
- }
-
- return (
- <div className="mt-8">
- {canAdd && (
- <div className="mb-4 flex items-center gap-3">
- <span className="text-sm text-(--sf-muted)">
- {t.quantity}
- </span>
- <QuantityStepper
- value={Math.min(quantity, max)}
- max={max}
- onChange={setQuantity}
- decreaseLabel={t.decrease}
- increaseLabel={t.increase}
- />
- {product.stock_left <= 5 && (
- <span className="text-sm text-(--sf-muted)">
- {t.onlyLeft(product.stock_left)}
- </span>
- )}
- </div>
- )}
-
- <button
- type="button"
- onClick={add}
- disabled={!canAdd || busy}
- className="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"
- >
- {product.in_stock ? t.addToCart : t.soldOut}
- </button>
-
- <div aria-live="polite" className="mt-3 min-h-6 text-sm">
- {error && <p className="text-red-700">{error}</p>}
- {added && !error && (
- <p className="flex flex-wrap items-center gap-3">
- <span>{t.added}</span>
- <SfButton
- href="/cart"
- variant="outline"
- className="px-3 py-1.5"
- >
- {t.viewCart}
- </SfButton>
- </p>
- )}
- </div>
- </div>
- );
- }
|