AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cart.tsx 14KB

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