| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- import { router } from '@inertiajs/react';
- import { Trash2 } from 'lucide-react';
- import { useState } 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.subtotal >= freeOver
- ? t.youGetFreeShipping
- : t.spendMoreForFree(formatPrice(freeOver - cart.subtotal));
- const [busy, setBusy] = useState(false);
-
- function change(line: CartLine, quantity: number) {
- const url = `${baseUrl}/cart/items/${line.product.id}`;
- 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.product.id}
- 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>
- <p className="mt-1 text-sm text-(--sf-muted) tabular-nums">
- {formatPrice(
- line.product.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}`}
- increaseLabel={`${t.increase}: ${line.product.name}`}
- />
- <button
- type="button"
- disabled={busy}
- onClick={() => change(line, 0)}
- aria-label={`${t.remove}: ${line.product.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">
- <div className="flex items-baseline justify-between gap-4">
- <span className="font-medium">
- {t.subtotal}
- </span>
- <span className="text-xl font-semibold tabular-nums">
- {formatPrice(cart.subtotal)}
- </span>
- </div>
- <p className="mt-2 text-sm text-(--sf-muted)">
- {t.shippingNote}
- </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>
- );
- }
|