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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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. <details
  45. className="group rounded-xl border p-4"
  46. data-test="featured-picker"
  47. >
  48. <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
  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. <ChevronDown
  59. className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
  60. aria-hidden="true"
  61. />
  62. </summary>
  63. <div className="mt-4 space-y-4">
  64. {products.length === 0 ? (
  65. <p className="text-sm text-muted-foreground">
  66. You have no active products yet.{' '}
  67. <a
  68. href={ProductController.create.url(store.slug)}
  69. className="underline underline-offset-2"
  70. >
  71. Add a product
  72. </a>{' '}
  73. to show it here. Until then the section shows example
  74. text.
  75. </p>
  76. ) : (
  77. <ul className="space-y-3">
  78. {sections.map((section) => {
  79. const id = `featured-${section.section_id}`;
  80. const missing =
  81. section.product_slug !== null &&
  82. !products.some(
  83. (product) =>
  84. product.slug === section.product_slug,
  85. );
  86. return (
  87. <li
  88. key={section.section_id}
  89. className="grid gap-1.5"
  90. >
  91. <label htmlFor={id} className="text-sm">
  92. “{section.name}”
  93. {section.page_id !== 'home' && (
  94. <span className="text-muted-foreground">
  95. {' '}
  96. on {section.page_title}
  97. </span>
  98. )}
  99. </label>
  100. <select
  101. id={id}
  102. value={
  103. missing
  104. ? ''
  105. : (section.product_slug ?? '')
  106. }
  107. disabled={
  108. busy ||
  109. saving === section.section_id
  110. }
  111. onChange={(event) =>
  112. choose(section, event.target.value)
  113. }
  114. className={fieldClass}
  115. >
  116. <option value="">
  117. No product (hide the price)
  118. </option>
  119. {products.map((product) => (
  120. <option
  121. key={product.slug}
  122. value={product.slug}
  123. >
  124. {product.name} ·{' '}
  125. {formatRinggit(product.price)}
  126. </option>
  127. ))}
  128. </select>
  129. {missing && (
  130. <p className="text-xs text-amber-700 dark:text-amber-400">
  131. The product this section showed is
  132. no longer active. Choose another
  133. one.
  134. </p>
  135. )}
  136. </li>
  137. );
  138. })}
  139. </ul>
  140. )}
  141. </div>
  142. </details>
  143. );
  144. }