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.

featured-picker.tsx 7.2KB

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