AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

featured-picker.tsx 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { router } from '@inertiajs/react';
  2. import { useState } from 'react';
  3. import FeaturedProductController from '@/actions/App/Http/Controllers/FeaturedProductController';
  4. import ProductController from '@/actions/App/Http/Controllers/ProductController';
  5. import { formatRinggit } from '@/lib/money';
  6. import type { FeaturedSection, ProductOption, Store } from '@/types';
  7. import { SectionName } from '@/components/stores/preview-highlight';
  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 data-test="featured-picker">
  45. <p className="text-sm text-muted-foreground">
  46. Choose which of your products this section shows. Its real name,
  47. price and photo are used, and the button opens its page.
  48. </p>
  49. <div className="mt-4 space-y-4">
  50. {products.length === 0 ? (
  51. <p className="text-sm text-muted-foreground">
  52. You have no active products yet.{' '}
  53. <a
  54. href={ProductController.create.url(store.slug)}
  55. className="underline underline-offset-2"
  56. >
  57. Add a product
  58. </a>{' '}
  59. to show it here. Until then the section shows example
  60. text.
  61. </p>
  62. ) : (
  63. <ul className="space-y-3">
  64. {sections.map((section) => {
  65. const id = `featured-${section.section_id}`;
  66. const missing =
  67. section.product_slug !== null &&
  68. !products.some(
  69. (product) =>
  70. product.slug === section.product_slug,
  71. );
  72. return (
  73. <li
  74. key={section.section_id}
  75. className="grid gap-1.5"
  76. >
  77. <SectionName
  78. target={{
  79. sectionId: section.section_id,
  80. pageId: section.page_id,
  81. label: 'Featured product',
  82. }}
  83. className="text-sm"
  84. >
  85. “{section.name}”
  86. {section.page_id !== 'home' && (
  87. <span className="text-muted-foreground">
  88. {' '}
  89. on {section.page_title}
  90. </span>
  91. )}
  92. </SectionName>
  93. <select
  94. id={id}
  95. aria-label={`Product for “${section.name}”`}
  96. value={
  97. missing
  98. ? ''
  99. : (section.product_slug ?? '')
  100. }
  101. disabled={
  102. busy ||
  103. saving === section.section_id
  104. }
  105. onChange={(event) =>
  106. choose(section, event.target.value)
  107. }
  108. className={fieldClass}
  109. >
  110. <option value="">
  111. No product (hide the price)
  112. </option>
  113. {products.map((product) => (
  114. <option
  115. key={product.slug}
  116. value={product.slug}
  117. >
  118. {product.name} ·{' '}
  119. {formatRinggit(product.price)}
  120. </option>
  121. ))}
  122. </select>
  123. {missing && (
  124. <p className="text-xs text-amber-700 dark:text-amber-400">
  125. The product this section showed is
  126. no longer active. Choose another
  127. one.
  128. </p>
  129. )}
  130. </li>
  131. );
  132. })}
  133. </ul>
  134. )}
  135. </div>
  136. </div>
  137. );
  138. }