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.

product.tsx 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { router } from '@inertiajs/react';
  2. import { useState } from 'react';
  3. import { useStorefront } from '../context';
  4. import { ProductImage } from '../product-card';
  5. import { storefrontText } from '../text';
  6. import { formatPrice } from '../theme';
  7. import type { StoreProduct } from '../types';
  8. import { Container, QuantityStepper, SfButton, SfLink } from '../ui';
  9. /** One product's own page. Built by us, in the store's colours and fonts. */
  10. export function ProductPage({ product }: { product: StoreProduct }) {
  11. const { language } = useStorefront();
  12. const t = storefrontText(language);
  13. const [selected, setSelected] = useState(0);
  14. const shown = product.images[selected] ?? product.image;
  15. return (
  16. <main className="py-10 md:py-14">
  17. <Container>
  18. <SfLink
  19. href="/shop"
  20. className="text-sm text-(--sf-muted) transition-colors hover:text-(--sf-fg)"
  21. >
  22. ← {t.backToShop}
  23. </SfLink>
  24. <div className="mt-6 grid gap-10 md:grid-cols-2 md:gap-14">
  25. <div>
  26. <ProductImage
  27. src={shown}
  28. alt={product.name}
  29. className="aspect-square w-full"
  30. />
  31. {product.images.length > 1 && (
  32. <ul className="mt-3 grid grid-cols-5 gap-3">
  33. {product.images.map((image, index) => (
  34. <li key={image}>
  35. <button
  36. type="button"
  37. onClick={() => setSelected(index)}
  38. aria-label={`${product.name} ${index + 1}`}
  39. aria-pressed={index === selected}
  40. className={`block w-full rounded-(--sf-radius-card) outline-offset-2 focus-visible:outline-2 focus-visible:outline-(--sf-primary) ${index === selected ? 'ring-2 ring-(--sf-primary)' : 'opacity-70 hover:opacity-100'}`}
  41. >
  42. <ProductImage
  43. src={image}
  44. alt=""
  45. className="aspect-square w-full"
  46. />
  47. </button>
  48. </li>
  49. ))}
  50. </ul>
  51. )}
  52. </div>
  53. <div>
  54. <h1 className="sf-heading text-3xl leading-tight font-semibold sm:text-4xl">
  55. {product.name}
  56. </h1>
  57. <p className="mt-4 text-2xl font-semibold tabular-nums">
  58. {formatPrice(product.price)}
  59. {product.compare_at_price !== null && (
  60. <s className="ml-3 text-base font-normal text-(--sf-muted)">
  61. {formatPrice(product.compare_at_price)}
  62. </s>
  63. )}
  64. </p>
  65. <p className="mt-2 text-sm text-(--sf-muted)">
  66. {product.in_stock ? t.inStock : t.soldOut}
  67. </p>
  68. {product.description && (
  69. <p className="mt-6 leading-relaxed whitespace-pre-line text-(--sf-muted)">
  70. {product.description}
  71. </p>
  72. )}
  73. <AddToCart product={product} />
  74. </div>
  75. </div>
  76. </Container>
  77. </main>
  78. );
  79. }
  80. function AddToCart({ product }: { product: StoreProduct }) {
  81. const { baseUrl, language, cart } = useStorefront();
  82. const t = storefrontText(language);
  83. const [quantity, setQuantity] = useState(1);
  84. const [busy, setBusy] = useState(false);
  85. const [added, setAdded] = useState(false);
  86. const [error, setError] = useState<string | null>(null);
  87. const inCart =
  88. cart.lines.find((line) => line.product.id === product.id)?.quantity ??
  89. 0;
  90. const max = Math.max(0, Math.min(product.stock_left, 99) - inCart);
  91. const canAdd = product.in_stock && max > 0;
  92. function add() {
  93. setError(null);
  94. router.post(
  95. `${baseUrl}/cart/items`,
  96. { product_id: product.id, quantity: Math.min(quantity, max) },
  97. {
  98. preserveScroll: true,
  99. onStart: () => setBusy(true),
  100. onFinish: () => setBusy(false),
  101. onSuccess: () => {
  102. setAdded(true);
  103. setQuantity(1);
  104. },
  105. onError: (errors) =>
  106. setError(errors.quantity ?? errors.product_id ?? null),
  107. },
  108. );
  109. }
  110. return (
  111. <div className="mt-8">
  112. {canAdd && (
  113. <div className="mb-4 flex items-center gap-3">
  114. <span className="text-sm text-(--sf-muted)">
  115. {t.quantity}
  116. </span>
  117. <QuantityStepper
  118. value={Math.min(quantity, max)}
  119. max={max}
  120. onChange={setQuantity}
  121. decreaseLabel={t.decrease}
  122. increaseLabel={t.increase}
  123. />
  124. {product.stock_left <= 5 && (
  125. <span className="text-sm text-(--sf-muted)">
  126. {t.onlyLeft(product.stock_left)}
  127. </span>
  128. )}
  129. </div>
  130. )}
  131. <button
  132. type="button"
  133. onClick={add}
  134. disabled={!canAdd || busy}
  135. className="inline-flex w-full items-center justify-center rounded-(--sf-radius) bg-(--sf-primary) px-6 py-3 text-sm font-semibold text-(--sf-primary-fg) transition-opacity hover:opacity-85 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--sf-primary) disabled:cursor-not-allowed disabled:opacity-50 sm:w-auto"
  136. >
  137. {product.in_stock ? t.addToCart : t.soldOut}
  138. </button>
  139. <div aria-live="polite" className="mt-3 min-h-6 text-sm">
  140. {error && <p className="text-red-700">{error}</p>}
  141. {added && !error && (
  142. <p className="flex flex-wrap items-center gap-3">
  143. <span>{t.added}</span>
  144. <SfButton
  145. href="/cart"
  146. variant="outline"
  147. className="px-3 py-1.5"
  148. >
  149. {t.viewCart}
  150. </SfButton>
  151. </p>
  152. )}
  153. </div>
  154. </div>
  155. );
  156. }