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ů.

featured-picker.tsx 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { router } from '@inertiajs/react';
  2. import { Link2 } from 'lucide-react';
  3. import { useState } from 'react';
  4. import FeaturedProductController from '@/actions/App/Http/Controllers/FeaturedProductController';
  5. import ProductController from '@/actions/App/Http/Controllers/ProductController';
  6. import { formatRinggit } from '@/lib/money';
  7. import type { FeaturedSection, ProductOption, Store } from '@/types';
  8. const fieldClass =
  9. 'h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-1 text-base shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:opacity-50 md:text-sm';
  10. /**
  11. * Lets the merchant choose which real product each "featured product" section shows,
  12. * so its name, price, photo and link always match the product list.
  13. */
  14. export default function FeaturedPicker({
  15. store,
  16. sections,
  17. products,
  18. busy,
  19. }: {
  20. store: Store;
  21. sections: FeaturedSection[];
  22. products: ProductOption[];
  23. busy: boolean;
  24. }) {
  25. const [saving, setSaving] = useState<string | null>(null);
  26. if (sections.length === 0) {
  27. return null;
  28. }
  29. function choose(section: FeaturedSection, slug: string) {
  30. router.put(
  31. FeaturedProductController.update.url({
  32. slug: store.slug,
  33. sectionId: section.section_id,
  34. }),
  35. { product_slug: slug || null },
  36. {
  37. preserveScroll: true,
  38. onStart: () => setSaving(section.section_id),
  39. onFinish: () => setSaving(null),
  40. },
  41. );
  42. }
  43. return (
  44. <div
  45. className="space-y-4 rounded-xl border p-4"
  46. data-test="featured-picker"
  47. >
  48. <div className="flex items-start gap-3">
  49. <Link2 className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
  50. <div>
  51. <h3 className="font-medium">Featured product</h3>
  52. <p className="mt-0.5 text-sm text-muted-foreground">
  53. Choose which of your products this section shows. Its
  54. real name, price and photo are used, and the button
  55. opens its page.
  56. </p>
  57. </div>
  58. </div>
  59. {products.length === 0 ? (
  60. <p className="text-sm text-muted-foreground">
  61. You have no active products yet.{' '}
  62. <a
  63. href={ProductController.create.url(store.slug)}
  64. className="underline underline-offset-2"
  65. >
  66. Add a product
  67. </a>{' '}
  68. to show it here. Until then the section shows example text.
  69. </p>
  70. ) : (
  71. <ul className="space-y-3">
  72. {sections.map((section) => {
  73. const id = `featured-${section.section_id}`;
  74. const missing =
  75. section.product_slug !== null &&
  76. !products.some(
  77. (product) =>
  78. product.slug === section.product_slug,
  79. );
  80. return (
  81. <li
  82. key={section.section_id}
  83. className="grid gap-1.5"
  84. >
  85. <label htmlFor={id} className="text-sm">
  86. “{section.name}”
  87. {section.page_id !== 'home' && (
  88. <span className="text-muted-foreground">
  89. {' '}
  90. on {section.page_title}
  91. </span>
  92. )}
  93. </label>
  94. <select
  95. id={id}
  96. value={
  97. missing
  98. ? ''
  99. : (section.product_slug ?? '')
  100. }
  101. disabled={
  102. busy || saving === section.section_id
  103. }
  104. onChange={(event) =>
  105. choose(section, event.target.value)
  106. }
  107. className={fieldClass}
  108. >
  109. <option value="">
  110. No product (hide the price)
  111. </option>
  112. {products.map((product) => (
  113. <option
  114. key={product.slug}
  115. value={product.slug}
  116. >
  117. {product.name} ·{' '}
  118. {formatRinggit(product.price)}
  119. </option>
  120. ))}
  121. </select>
  122. {missing && (
  123. <p className="text-xs text-amber-700 dark:text-amber-400">
  124. The product this section showed is no
  125. longer active. Choose another one.
  126. </p>
  127. )}
  128. </li>
  129. );
  130. })}
  131. </ul>
  132. )}
  133. </div>
  134. );
  135. }