AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cart.tsx 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { router } from '@inertiajs/react';
  2. import { Trash2 } from 'lucide-react';
  3. import { useState } from 'react';
  4. import { useStorefront } from '../context';
  5. import { ProductImage } from '../product-card';
  6. import { storefrontText } from '../text';
  7. import { formatPrice } from '../theme';
  8. import type { CartLine } from '../types';
  9. import { Container, QuantityStepper, SfButton, SfLink } from '../ui';
  10. /** The shopper's cart. Built by us, in the store's colours and fonts. */
  11. export function CartPage() {
  12. const { cart, language, baseUrl, shop } = useStorefront();
  13. const t = storefrontText(language);
  14. const freeOver = shop.free_shipping_over;
  15. const freeShippingNote =
  16. freeOver === null || freeOver === 0
  17. ? null
  18. : cart.subtotal >= freeOver
  19. ? t.youGetFreeShipping
  20. : t.spendMoreForFree(formatPrice(freeOver - cart.subtotal));
  21. const [busy, setBusy] = useState(false);
  22. function change(line: CartLine, quantity: number) {
  23. const url = `${baseUrl}/cart/items/${line.product.id}`;
  24. const options = {
  25. preserveScroll: true,
  26. onStart: () => setBusy(true),
  27. onFinish: () => setBusy(false),
  28. };
  29. if (quantity < 1) {
  30. router.delete(url, options);
  31. } else {
  32. router.patch(url, { quantity }, options);
  33. }
  34. }
  35. return (
  36. <main className="py-12 md:py-16">
  37. <Container>
  38. <h1 className="sf-heading text-4xl font-semibold sm:text-5xl">
  39. {t.yourCart}
  40. </h1>
  41. {cart.adjusted && (
  42. <p
  43. role="status"
  44. className="mt-6 rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-4 text-sm"
  45. >
  46. {t.cartAdjusted}
  47. </p>
  48. )}
  49. {cart.lines.length === 0 ? (
  50. <div className="mt-8">
  51. <p className="text-(--sf-muted)">{t.cartEmpty}</p>
  52. <SfButton href="/shop" className="mt-6">
  53. {t.continueShopping}
  54. </SfButton>
  55. </div>
  56. ) : (
  57. <div className="mt-10 grid items-start gap-8 lg:grid-cols-[1fr_340px] lg:gap-10">
  58. <ul className="space-y-4">
  59. {cart.lines.map((line, index) => (
  60. <li
  61. key={line.product.id}
  62. 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"
  63. >
  64. <SfLink
  65. href={`/products/${line.product.slug}`}
  66. className="shrink-0"
  67. >
  68. <ProductImage
  69. src={line.product.image}
  70. alt={line.product.name}
  71. seed={index}
  72. className="size-20 sm:size-28"
  73. />
  74. </SfLink>
  75. <div className="flex min-w-0 flex-1 flex-col justify-between gap-4">
  76. <div className="flex items-start justify-between gap-4">
  77. <div className="min-w-0">
  78. <SfLink
  79. href={`/products/${line.product.slug}`}
  80. className="font-medium hover:underline"
  81. >
  82. {line.product.name}
  83. </SfLink>
  84. <p className="mt-1 text-sm text-(--sf-muted) tabular-nums">
  85. {formatPrice(
  86. line.product.price,
  87. )}{' '}
  88. {t.each}
  89. </p>
  90. {line.quantity >= line.max && (
  91. <p className="mt-2 inline-block rounded-full bg-(--sf-surface) px-2.5 py-0.5 text-xs text-(--sf-muted)">
  92. {t.onlyLeft(line.max)}
  93. </p>
  94. )}
  95. </div>
  96. <p className="shrink-0 text-lg font-semibold tabular-nums">
  97. {formatPrice(line.line_total)}
  98. </p>
  99. </div>
  100. <div className="flex items-center justify-between gap-4">
  101. <QuantityStepper
  102. value={line.quantity}
  103. max={line.max}
  104. disabled={busy}
  105. onChange={(quantity) =>
  106. change(line, quantity)
  107. }
  108. decreaseLabel={`${t.decrease}: ${line.product.name}`}
  109. increaseLabel={`${t.increase}: ${line.product.name}`}
  110. />
  111. <button
  112. type="button"
  113. disabled={busy}
  114. onClick={() => change(line, 0)}
  115. aria-label={`${t.remove}: ${line.product.name}`}
  116. 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"
  117. >
  118. <Trash2
  119. className="size-4"
  120. aria-hidden="true"
  121. />
  122. {t.remove}
  123. </button>
  124. </div>
  125. </div>
  126. </li>
  127. ))}
  128. </ul>
  129. <aside className="h-fit rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-6">
  130. <div className="flex items-baseline justify-between gap-4">
  131. <span className="font-medium">
  132. {t.subtotal}
  133. </span>
  134. <span className="text-xl font-semibold tabular-nums">
  135. {formatPrice(cart.subtotal)}
  136. </span>
  137. </div>
  138. <p className="mt-2 text-sm text-(--sf-muted)">
  139. {t.shippingNote}
  140. </p>
  141. {freeShippingNote && (
  142. <p
  143. className="mt-2 text-sm font-medium"
  144. data-test="free-shipping-note"
  145. >
  146. {freeShippingNote}
  147. </p>
  148. )}
  149. <SfButton
  150. href="/checkout"
  151. className="mt-6 w-full py-3"
  152. >
  153. {t.checkout}
  154. </SfButton>
  155. <SfLink
  156. href="/shop"
  157. className="mt-5 inline-block text-sm underline underline-offset-2 hover:text-(--sf-primary)"
  158. >
  159. {t.continueShopping}
  160. </SfLink>
  161. </aside>
  162. </div>
  163. )}
  164. </Container>
  165. </main>
  166. );
  167. }