| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import { router, useForm } from '@inertiajs/react';
- import { Trash2 } from 'lucide-react';
- import { useState } from 'react';
- import type { FormEvent } from 'react';
- import { useStorefront } from '../context';
- import { ProductImage } from '../product-card';
- import { storefrontText } from '../text';
- import { formatPrice } from '../theme';
- import type { CartLine } from '../types';
- import { Container, QuantityStepper, SfButton, SfLink } from '../ui';
-
- /** The shopper's cart. Built by us, in the store's colours and fonts. */
- export function CartPage() {
- const { cart, language, baseUrl, shop } = useStorefront();
-
- const t = storefrontText(language);
- const freeOver = shop.free_shipping_over;
- const freeShippingNote =
- freeOver === null || freeOver === 0
- ? null
- : cart.total >= freeOver
- ? t.youGetFreeShipping
- : t.spendMoreForFree(formatPrice(freeOver - cart.total));
- const [busy, setBusy] = useState(false);
-
- function change(line: CartLine, quantity: number) {
- const url = `${baseUrl}/cart/items/${line.key}`;
- const options = {
- preserveScroll: true,
- onStart: () => setBusy(true),
- onFinish: () => setBusy(false),
- };
-
- if (quantity < 1) {
- router.delete(url, options);
- } else {
- router.patch(url, { quantity }, options);
- }
- }
-
- return (
- <main className="py-12 md:py-16">
- <Container>
- <h1 className="sf-heading text-4xl font-semibold sm:text-5xl">
- {t.yourCart}
- </h1>
-
- {cart.adjusted && (
- <p
- role="status"
- className="mt-6 rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-4 text-sm"
- >
- {t.cartAdjusted}
- </p>
- )}
-
- {cart.lines.length === 0 ? (
- <div className="mt-8">
- <p className="text-(--sf-muted)">{t.cartEmpty}</p>
- <SfButton href="/shop" className="mt-6">
- {t.continueShopping}
- </SfButton>
- </div>
- ) : (
- <div className="mt-10 grid items-start gap-8 lg:grid-cols-[1fr_340px] lg:gap-10">
- <ul className="space-y-4">
- {cart.lines.map((line, index) => (
- <li
- key={line.key}
- className="flex gap-4 rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-bg) p-4 shadow-[0_1px_2px_rgb(0_0_0/0.04)] transition-shadow hover:shadow-[0_4px_16px_rgb(0_0_0/0.06)] sm:gap-5 sm:p-5"
- >
- <SfLink
- href={`/products/${line.product.slug}`}
- className="shrink-0"
- >
- <ProductImage
- src={line.product.image}
- alt={line.product.name}
- seed={index}
- className="size-20 sm:size-28"
- />
- </SfLink>
-
- <div className="flex min-w-0 flex-1 flex-col justify-between gap-4">
- <div className="flex items-start justify-between gap-4">
- <div className="min-w-0">
- <SfLink
- href={`/products/${line.product.slug}`}
- className="font-medium hover:underline"
- >
- {line.product.name}
- </SfLink>
- {line.variant && (
- <p className="mt-0.5 text-sm text-(--sf-muted)">
- {line.variant.name}
- </p>
- )}
- <p className="mt-1 text-sm text-(--sf-muted) tabular-nums">
- {formatPrice(
- line.unit_price,
- )}{' '}
- {t.each}
- </p>
- {line.quantity >= line.max && (
- <p className="mt-2 inline-block rounded-full bg-(--sf-surface) px-2.5 py-0.5 text-xs text-(--sf-muted)">
- {t.onlyLeft(line.max)}
- </p>
- )}
- </div>
- <p className="shrink-0 text-lg font-semibold tabular-nums">
- {formatPrice(line.line_total)}
- </p>
- </div>
-
- <div className="flex items-center justify-between gap-4">
- <QuantityStepper
- value={line.quantity}
- max={line.max}
- disabled={busy}
- onChange={(quantity) =>
- change(line, quantity)
- }
- decreaseLabel={`${t.decrease}: ${line.product.name}${line.variant ? ` (${line.variant.name})` : ''}`}
- increaseLabel={`${t.increase}: ${line.product.name}${line.variant ? ` (${line.variant.name})` : ''}`}
- />
- <button
- type="button"
- disabled={busy}
- onClick={() => change(line, 0)}
- aria-label={`${t.remove}: ${line.product.name}${line.variant ? ` (${line.variant.name})` : ''}`}
- className="inline-flex items-center gap-1.5 rounded-(--sf-radius) px-2.5 py-1.5 text-sm text-(--sf-muted) transition-colors hover:bg-(--sf-surface) hover:text-(--sf-fg) disabled:opacity-50"
- >
- <Trash2
- className="size-4"
- aria-hidden="true"
- />
- {t.remove}
- </button>
- </div>
- </div>
- </li>
- ))}
- </ul>
-
- <aside className="h-fit rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-6">
- <DiscountBox />
-
- {cart.applied && (
- <div className="mb-3 space-y-1.5 text-sm">
- <div className="flex justify-between gap-4">
- <span>{t.subtotal}</span>
- <span className="tabular-nums">
- {formatPrice(cart.subtotal)}
- </span>
- </div>
- <div
- className="flex justify-between gap-4"
- data-test="cart-discount-line"
- >
- <span>
- {t.discount} ({cart.applied.name})
- </span>
- <span className="tabular-nums">
- −{formatPrice(cart.applied.amount)}
- </span>
- </div>
- </div>
- )}
-
- <div className="flex items-baseline justify-between gap-4">
- <span className="font-medium">
- {cart.applied ? t.total : t.subtotal}
- </span>
- <span className="text-xl font-semibold tabular-nums">
- {formatPrice(cart.total)}
- </span>
- </div>
- <p className="mt-2 text-sm text-(--sf-muted)">
- {t.shippingNote}
- </p>
- {cart.offer_hint && (
- <p
- className="mt-2 text-sm font-medium"
- data-test="offer-hint"
- >
- {cart.offer_hint}
- </p>
- )}
- {freeShippingNote && (
- <p
- className="mt-2 text-sm font-medium"
- data-test="free-shipping-note"
- >
- {freeShippingNote}
- </p>
- )}
-
- <SfButton
- href="/checkout"
- className="mt-6 w-full py-3"
- >
- {t.checkout}
- </SfButton>
-
- <SfLink
- href="/shop"
- className="mt-5 inline-block text-sm underline underline-offset-2 hover:text-(--sf-primary)"
- >
- {t.continueShopping}
- </SfLink>
- </aside>
- </div>
- )}
- </Container>
- </main>
- );
- }
-
- /**
- * Type a discount code, or see the one in use with a way to take it out.
- */
- function DiscountBox() {
- const { cart, language, baseUrl } = useStorefront();
- const t = storefrontText(language);
- const form = useForm({ code: '' });
-
- function apply(event: FormEvent) {
- event.preventDefault();
- form.post(`${baseUrl}/cart/discount`, {
- preserveScroll: true,
- onSuccess: () => form.reset(),
- });
- }
-
- function remove() {
- router.delete(`${baseUrl}/cart/discount`, { preserveScroll: true });
- }
-
- if (cart.discount) {
- return (
- <div className="mb-5 border-b border-(--sf-border) pb-5">
- <div className="flex items-center justify-between gap-3 text-sm">
- <span>
- <span className="font-mono font-semibold">
- {cart.discount.code}
- </span>{' '}
- <span className="text-(--sf-muted)">
- · {cart.discount.label}
- </span>
- </span>
- <button
- type="button"
- onClick={remove}
- className="text-(--sf-muted) underline underline-offset-2 hover:text-(--sf-fg)"
- >
- {t.removeCode}
- </button>
- </div>
- {cart.discount.message && (
- <p className="mt-2 text-sm" role="status">
- {cart.discount.message}
- </p>
- )}
- </div>
- );
- }
-
- return (
- <form
- onSubmit={apply}
- className="mb-5 border-b border-(--sf-border) pb-5"
- noValidate
- >
- <label htmlFor="discount-code" className="text-sm font-medium">
- {t.discountCode}
- </label>
- <div className="mt-2 flex gap-2">
- <input
- id="discount-code"
- name="code"
- value={form.data.code}
- onChange={(event) =>
- form.setData('code', event.target.value)
- }
- placeholder={t.codePlaceholder}
- autoComplete="off"
- autoCapitalize="characters"
- aria-invalid={form.errors.code ? true : undefined}
- aria-describedby={
- form.errors.code ? 'discount-code-error' : undefined
- }
- className="min-w-0 flex-1 rounded-(--sf-radius) border border-(--sf-border) bg-(--sf-bg) px-3 py-2 text-base uppercase outline-none focus-visible:border-(--sf-primary) focus-visible:ring-2 focus-visible:ring-(--sf-primary)/30 sm:text-sm"
- />
- <button
- type="submit"
- disabled={form.processing || form.data.code.trim() === ''}
- className="rounded-(--sf-radius) border border-(--sf-border) bg-(--sf-bg) px-4 py-2 text-sm font-semibold transition-colors hover:bg-(--sf-surface) disabled:opacity-50"
- >
- {t.apply}
- </button>
- </div>
- {form.errors.code && (
- <p
- id="discount-code-error"
- className="mt-2 text-sm text-red-700"
- role="alert"
- >
- {form.errors.code}
- </p>
- )}
- </form>
- );
- }
|