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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import { router, useForm } from '@inertiajs/react';
  2. import { Trash2 } from 'lucide-react';
  3. import { useState } from 'react';
  4. import type { FormEvent } from 'react';
  5. import { useStorefront } from '../context';
  6. import { ProductImage } from '../product-card';
  7. import { formatDeadline, storefrontText } from '../text';
  8. import { formatPrice } from '../theme';
  9. import type { CartLine, UnpaidOrder } from '../types';
  10. import { Container, QuantityStepper, SfButton, SfLink } from '../ui';
  11. /**
  12. * The shopper's cart. Built by us, in the store's colours and fonts.
  13. * When this shopper left the payment page without paying, a note at the top takes them back to it.
  14. */
  15. export function CartPage({ unpaidOrder }: { unpaidOrder: UnpaidOrder | null }) {
  16. const { cart, language, baseUrl, shop } = useStorefront();
  17. const t = storefrontText(language);
  18. const freeOver = shop.free_shipping_over;
  19. const freeShippingNote =
  20. freeOver === null || freeOver === 0
  21. ? null
  22. : cart.total >= freeOver
  23. ? t.youGetFreeShipping
  24. : t.spendMoreForFree(formatPrice(freeOver - cart.total));
  25. const [busy, setBusy] = useState(false);
  26. function change(line: CartLine, quantity: number) {
  27. const url = `${baseUrl}/cart/items/${line.key}`;
  28. const options = {
  29. preserveScroll: true,
  30. onStart: () => setBusy(true),
  31. onFinish: () => setBusy(false),
  32. };
  33. if (quantity < 1) {
  34. router.delete(url, options);
  35. } else {
  36. router.patch(url, { quantity }, options);
  37. }
  38. }
  39. return (
  40. <main className="py-12 md:py-16">
  41. <Container>
  42. <h1 className="sf-heading text-4xl font-semibold sm:text-5xl">
  43. {t.yourCart}
  44. </h1>
  45. {unpaidOrder && (
  46. <div
  47. role="status"
  48. data-test="unpaid-order"
  49. className="mt-6 flex flex-col gap-4 rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-4 text-sm sm:flex-row sm:items-center sm:justify-between sm:p-5"
  50. >
  51. <p>
  52. <span className="font-medium">
  53. {t.unpaidOrderBanner(unpaidOrder.number)}
  54. </span>
  55. {unpaidOrder.expires_at && (
  56. <span className="text-(--sf-muted)">
  57. {' '}
  58. {t.unpaidOrderDeadline(
  59. formatDeadline(
  60. unpaidOrder.expires_at,
  61. language,
  62. ),
  63. )}
  64. </span>
  65. )}
  66. </p>
  67. <SfButton
  68. href={`/orders/${unpaidOrder.public_id}/pay`}
  69. className="shrink-0"
  70. >
  71. {t.completePayment}
  72. </SfButton>
  73. </div>
  74. )}
  75. {cart.adjusted && (
  76. <p
  77. role="status"
  78. className="mt-6 rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-4 text-sm"
  79. >
  80. {t.cartAdjusted}
  81. </p>
  82. )}
  83. {cart.lines.length === 0 ? (
  84. <div className="mt-8">
  85. <p className="text-(--sf-muted)">{t.cartEmpty}</p>
  86. <SfButton href="/shop" className="mt-6">
  87. {t.continueShopping}
  88. </SfButton>
  89. </div>
  90. ) : (
  91. <div className="mt-10 grid items-start gap-8 lg:grid-cols-[1fr_340px] lg:gap-10">
  92. <ul className="space-y-4">
  93. {cart.lines.map((line, index) => (
  94. <li
  95. key={line.key}
  96. className="sf-card 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"
  97. >
  98. <SfLink
  99. href={`/products/${line.product.slug}`}
  100. className="shrink-0"
  101. >
  102. <ProductImage
  103. src={line.product.image}
  104. alt={line.product.name}
  105. seed={index}
  106. className="size-20 sm:size-28"
  107. />
  108. </SfLink>
  109. <div className="flex min-w-0 flex-1 flex-col justify-between gap-4">
  110. <div className="flex items-start justify-between gap-4">
  111. <div className="min-w-0">
  112. <SfLink
  113. href={`/products/${line.product.slug}`}
  114. className="font-medium hover:underline"
  115. >
  116. {line.product.name}
  117. </SfLink>
  118. {line.variant && (
  119. <p className="mt-0.5 text-sm text-(--sf-muted)">
  120. {line.variant.name}
  121. </p>
  122. )}
  123. <p className="mt-1 text-sm text-(--sf-muted) tabular-nums">
  124. {formatPrice(
  125. line.unit_price,
  126. )}{' '}
  127. {t.each}
  128. </p>
  129. {line.quantity >= line.max && (
  130. <p className="mt-2 inline-block rounded-full bg-(--sf-surface) px-2.5 py-0.5 text-xs text-(--sf-muted)">
  131. {t.onlyLeft(line.max)}
  132. </p>
  133. )}
  134. </div>
  135. <p className="shrink-0 text-lg font-semibold tabular-nums">
  136. {formatPrice(line.line_total)}
  137. </p>
  138. </div>
  139. <div className="flex items-center justify-between gap-4">
  140. <QuantityStepper
  141. value={line.quantity}
  142. max={line.max}
  143. disabled={busy}
  144. onChange={(quantity) =>
  145. change(line, quantity)
  146. }
  147. decreaseLabel={`${t.decrease}: ${line.product.name}${line.variant ? ` (${line.variant.name})` : ''}`}
  148. increaseLabel={`${t.increase}: ${line.product.name}${line.variant ? ` (${line.variant.name})` : ''}`}
  149. />
  150. <button
  151. type="button"
  152. disabled={busy}
  153. onClick={() => change(line, 0)}
  154. aria-label={`${t.remove}: ${line.product.name}${line.variant ? ` (${line.variant.name})` : ''}`}
  155. 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"
  156. >
  157. <Trash2
  158. className="size-4"
  159. aria-hidden="true"
  160. />
  161. {t.remove}
  162. </button>
  163. </div>
  164. </div>
  165. </li>
  166. ))}
  167. </ul>
  168. <aside className="sf-card h-fit rounded-(--sf-radius-card) border border-(--sf-border) bg-(--sf-surface) p-6">
  169. <DiscountBox />
  170. {cart.applied && (
  171. <div className="mb-3 space-y-1.5 text-sm">
  172. <div className="flex justify-between gap-4">
  173. <span>{t.subtotal}</span>
  174. <span className="tabular-nums">
  175. {formatPrice(cart.subtotal)}
  176. </span>
  177. </div>
  178. <div
  179. className="flex justify-between gap-4"
  180. data-test="cart-discount-line"
  181. >
  182. <span>
  183. {t.discount} ({cart.applied.name})
  184. </span>
  185. <span className="tabular-nums">
  186. −{formatPrice(cart.applied.amount)}
  187. </span>
  188. </div>
  189. </div>
  190. )}
  191. <div className="flex items-baseline justify-between gap-4">
  192. <span className="font-medium">
  193. {cart.applied ? t.total : t.subtotal}
  194. </span>
  195. <span className="text-xl font-semibold tabular-nums">
  196. {formatPrice(cart.total)}
  197. </span>
  198. </div>
  199. <p className="mt-2 text-sm text-(--sf-muted)">
  200. {t.shippingNote}
  201. </p>
  202. {cart.offer_hint && (
  203. <p
  204. className="mt-2 text-sm font-medium"
  205. data-test="offer-hint"
  206. >
  207. {cart.offer_hint}
  208. </p>
  209. )}
  210. {freeShippingNote && (
  211. <p
  212. className="mt-2 text-sm font-medium"
  213. data-test="free-shipping-note"
  214. >
  215. {freeShippingNote}
  216. </p>
  217. )}
  218. <SfButton
  219. href="/checkout"
  220. className="mt-6 w-full py-3"
  221. >
  222. {t.checkout}
  223. </SfButton>
  224. <SfLink
  225. href="/shop"
  226. className="mt-5 inline-block text-sm underline underline-offset-2 hover:text-(--sf-primary)"
  227. >
  228. {t.continueShopping}
  229. </SfLink>
  230. </aside>
  231. </div>
  232. )}
  233. </Container>
  234. </main>
  235. );
  236. }
  237. /**
  238. * Type a discount code, or see the one in use with a way to take it out.
  239. */
  240. function DiscountBox() {
  241. const { cart, language, baseUrl } = useStorefront();
  242. const t = storefrontText(language);
  243. const form = useForm({ code: '' });
  244. function apply(event: FormEvent) {
  245. event.preventDefault();
  246. form.post(`${baseUrl}/cart/discount`, {
  247. preserveScroll: true,
  248. onSuccess: () => form.reset(),
  249. });
  250. }
  251. function remove() {
  252. router.delete(`${baseUrl}/cart/discount`, { preserveScroll: true });
  253. }
  254. if (cart.discount) {
  255. return (
  256. <div className="mb-5 border-b border-(--sf-border) pb-5">
  257. <div className="flex items-center justify-between gap-3 text-sm">
  258. <span>
  259. <span className="font-mono font-semibold">
  260. {cart.discount.code}
  261. </span>{' '}
  262. <span className="text-(--sf-muted)">
  263. · {cart.discount.label}
  264. </span>
  265. </span>
  266. <button
  267. type="button"
  268. onClick={remove}
  269. className="text-(--sf-muted) underline underline-offset-2 hover:text-(--sf-fg)"
  270. >
  271. {t.removeCode}
  272. </button>
  273. </div>
  274. {cart.discount.message && (
  275. <p className="mt-2 text-sm" role="status">
  276. {cart.discount.message}
  277. </p>
  278. )}
  279. </div>
  280. );
  281. }
  282. return (
  283. <form
  284. onSubmit={apply}
  285. className="mb-5 border-b border-(--sf-border) pb-5"
  286. noValidate
  287. >
  288. <label htmlFor="discount-code" className="text-sm font-medium">
  289. {t.discountCode}
  290. </label>
  291. <div className="mt-2 flex gap-2">
  292. <input
  293. id="discount-code"
  294. name="code"
  295. value={form.data.code}
  296. onChange={(event) =>
  297. form.setData('code', event.target.value)
  298. }
  299. placeholder={t.codePlaceholder}
  300. autoComplete="off"
  301. autoCapitalize="characters"
  302. aria-invalid={form.errors.code ? true : undefined}
  303. aria-describedby={
  304. form.errors.code ? 'discount-code-error' : undefined
  305. }
  306. 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"
  307. />
  308. <button
  309. type="submit"
  310. disabled={form.processing || form.data.code.trim() === ''}
  311. 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"
  312. >
  313. {t.apply}
  314. </button>
  315. </div>
  316. {form.errors.code && (
  317. <p
  318. id="discount-code-error"
  319. className="mt-2 text-sm text-red-700"
  320. role="alert"
  321. >
  322. {form.errors.code}
  323. </p>
  324. )}
  325. </form>
  326. );
  327. }