AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cart.tsx 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.applied && (
  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.applied.name})
  151. </span>
  152. <span className="tabular-nums">
  153. −{formatPrice(cart.applied.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.applied ? t.total : t.subtotal}
  161. </span>
  162. <span className="text-xl font-semibold tabular-nums">
  163. {formatPrice(cart.total)}
  164. </span>
  165. </div>
  166. <p className="mt-2 text-sm text-(--sf-muted)">
  167. {t.shippingNote}
  168. </p>
  169. {cart.offer_hint && (
  170. <p
  171. className="mt-2 text-sm font-medium"
  172. data-test="offer-hint"
  173. >
  174. {cart.offer_hint}
  175. </p>
  176. )}
  177. {freeShippingNote && (
  178. <p
  179. className="mt-2 text-sm font-medium"
  180. data-test="free-shipping-note"
  181. >
  182. {freeShippingNote}
  183. </p>
  184. )}
  185. <SfButton
  186. href="/checkout"
  187. className="mt-6 w-full py-3"
  188. >
  189. {t.checkout}
  190. </SfButton>
  191. <SfLink
  192. href="/shop"
  193. className="mt-5 inline-block text-sm underline underline-offset-2 hover:text-(--sf-primary)"
  194. >
  195. {t.continueShopping}
  196. </SfLink>
  197. </aside>
  198. </div>
  199. )}
  200. </Container>
  201. </main>
  202. );
  203. }
  204. /**
  205. * Type a discount code, or see the one in use with a way to take it out.
  206. */
  207. function DiscountBox() {
  208. const { cart, language, baseUrl } = useStorefront();
  209. const t = storefrontText(language);
  210. const form = useForm({ code: '' });
  211. function apply(event: FormEvent) {
  212. event.preventDefault();
  213. form.post(`${baseUrl}/cart/discount`, {
  214. preserveScroll: true,
  215. onSuccess: () => form.reset(),
  216. });
  217. }
  218. function remove() {
  219. router.delete(`${baseUrl}/cart/discount`, { preserveScroll: true });
  220. }
  221. if (cart.discount) {
  222. return (
  223. <div className="mb-5 border-b border-(--sf-border) pb-5">
  224. <div className="flex items-center justify-between gap-3 text-sm">
  225. <span>
  226. <span className="font-mono font-semibold">
  227. {cart.discount.code}
  228. </span>{' '}
  229. <span className="text-(--sf-muted)">
  230. · {cart.discount.label}
  231. </span>
  232. </span>
  233. <button
  234. type="button"
  235. onClick={remove}
  236. className="text-(--sf-muted) underline underline-offset-2 hover:text-(--sf-fg)"
  237. >
  238. {t.removeCode}
  239. </button>
  240. </div>
  241. {cart.discount.message && (
  242. <p className="mt-2 text-sm" role="status">
  243. {cart.discount.message}
  244. </p>
  245. )}
  246. </div>
  247. );
  248. }
  249. return (
  250. <form
  251. onSubmit={apply}
  252. className="mb-5 border-b border-(--sf-border) pb-5"
  253. noValidate
  254. >
  255. <label htmlFor="discount-code" className="text-sm font-medium">
  256. {t.discountCode}
  257. </label>
  258. <div className="mt-2 flex gap-2">
  259. <input
  260. id="discount-code"
  261. name="code"
  262. value={form.data.code}
  263. onChange={(event) =>
  264. form.setData('code', event.target.value)
  265. }
  266. placeholder={t.codePlaceholder}
  267. autoComplete="off"
  268. autoCapitalize="characters"
  269. aria-invalid={form.errors.code ? true : undefined}
  270. aria-describedby={
  271. form.errors.code ? 'discount-code-error' : undefined
  272. }
  273. 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"
  274. />
  275. <button
  276. type="submit"
  277. disabled={form.processing || form.data.code.trim() === ''}
  278. 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"
  279. >
  280. {t.apply}
  281. </button>
  282. </div>
  283. {form.errors.code && (
  284. <p
  285. id="discount-code-error"
  286. className="mt-2 text-sm text-red-700"
  287. role="alert"
  288. >
  289. {form.errors.code}
  290. </p>
  291. )}
  292. </form>
  293. );
  294. }