AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { Plus, X } from 'lucide-react';
  2. import { useState } from 'react';
  3. import InputError from '@/components/input-error';
  4. import { Button } from '@/components/ui/button';
  5. import { Checkbox } from '@/components/ui/checkbox';
  6. import { Input } from '@/components/ui/input';
  7. import { Label } from '@/components/ui/label';
  8. import type { BundleChoice, Product } from '@/types';
  9. type Row = { item: string; quantity: string };
  10. const MAX_ITEMS = 10;
  11. const selectClass =
  12. '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 md:text-sm';
  13. /**
  14. * A bundle: other products of the store sold together at one price.
  15. * Its stock is not typed in; it follows the stock of what is inside.
  16. * The inputs carry the names the server reads: is_bundle, bundle_items[i][item] and bundle_items[i][quantity].
  17. */
  18. export default function BundleEditor({
  19. product,
  20. choices,
  21. enabled,
  22. onEnabledChange,
  23. errors,
  24. }: {
  25. product?: Product;
  26. choices: BundleChoice[];
  27. enabled: boolean;
  28. onEnabledChange: (enabled: boolean) => void;
  29. errors: Record<string, string>;
  30. }) {
  31. const [rows, setRows] = useState<Row[]>(
  32. product && product.bundle_items.length > 0
  33. ? product.bundle_items.map((row) => ({
  34. item: row.item,
  35. quantity: String(row.quantity),
  36. }))
  37. : [{ item: '', quantity: '1' }],
  38. );
  39. const byKey = new Map(choices.map((choice) => [choice.key, choice]));
  40. const filled = rows
  41. .map((row) => ({
  42. choice: byKey.get(row.item),
  43. quantity: Math.max(1, Number(row.quantity) || 1),
  44. }))
  45. .filter((row) => row.choice !== undefined);
  46. // What the items cost bought one by one, and how many bundles their stock allows.
  47. const separately = filled.reduce(
  48. (sum, row) => sum + Number(row.choice!.price) * row.quantity,
  49. 0,
  50. );
  51. const stock =
  52. filled.length > 0
  53. ? Math.min(
  54. ...filled.map((row) =>
  55. Math.floor(row.choice!.stock / row.quantity),
  56. ),
  57. )
  58. : 0;
  59. function setRow(index: number, field: keyof Row, value: string) {
  60. setRows((current) =>
  61. current.map((row, i) =>
  62. i === index ? { ...row, [field]: value } : row,
  63. ),
  64. );
  65. }
  66. const rowErrors = (index: number) =>
  67. errors[`bundle_items.${index}.item`] ??
  68. errors[`bundle_items.${index}.quantity`];
  69. return (
  70. <div className="space-y-4 rounded-xl border p-4">
  71. <div className="flex items-start gap-3">
  72. <Checkbox
  73. id="is-bundle"
  74. className="mt-0.5"
  75. checked={enabled}
  76. onCheckedChange={(checked) =>
  77. onEnabledChange(checked === true)
  78. }
  79. />
  80. <div>
  81. <Label htmlFor="is-bundle">This is a bundle</Label>
  82. <p className="mt-0.5 text-sm text-muted-foreground">
  83. Sell some of your products together at one price. The
  84. stock follows the products inside.
  85. </p>
  86. </div>
  87. </div>
  88. {enabled && (
  89. <>
  90. <input type="hidden" name="is_bundle" value="1" />
  91. {choices.length === 0 ? (
  92. <p className="text-sm text-muted-foreground">
  93. Add the products that go inside first. Then come
  94. back to make the bundle.
  95. </p>
  96. ) : (
  97. <div className="space-y-3">
  98. <p className="text-sm font-medium">
  99. What is inside
  100. </p>
  101. {rows.map((row, index) => (
  102. <div key={index} className="space-y-1">
  103. <div className="flex items-center gap-2">
  104. <select
  105. name={`bundle_items[${index}][item]`}
  106. aria-label={`Product ${index + 1}`}
  107. value={row.item}
  108. onChange={(event) =>
  109. setRow(
  110. index,
  111. 'item',
  112. event.target.value,
  113. )
  114. }
  115. className={selectClass}
  116. >
  117. <option value="">
  118. Choose a product
  119. </option>
  120. {choices.map((choice) => (
  121. <option
  122. key={choice.key}
  123. value={choice.key}
  124. >
  125. {choice.label} · RM{' '}
  126. {choice.price} ·{' '}
  127. {choice.stock} in stock
  128. </option>
  129. ))}
  130. </select>
  131. <span className="text-sm text-muted-foreground">
  132. ×
  133. </span>
  134. <Input
  135. name={`bundle_items[${index}][quantity]`}
  136. aria-label={`How many of product ${index + 1}`}
  137. type="number"
  138. min={1}
  139. max={20}
  140. step={1}
  141. className="w-20"
  142. value={row.quantity}
  143. onChange={(event) =>
  144. setRow(
  145. index,
  146. 'quantity',
  147. event.target.value,
  148. )
  149. }
  150. />
  151. <Button
  152. type="button"
  153. variant="ghost"
  154. size="icon"
  155. aria-label={`Remove product ${index + 1}`}
  156. disabled={rows.length === 1}
  157. onClick={() =>
  158. setRows((current) =>
  159. current.filter(
  160. (_, i) => i !== index,
  161. ),
  162. )
  163. }
  164. >
  165. <X />
  166. </Button>
  167. </div>
  168. <InputError message={rowErrors(index)} />
  169. </div>
  170. ))}
  171. {rows.length < MAX_ITEMS && (
  172. <Button
  173. type="button"
  174. variant="outline"
  175. size="sm"
  176. onClick={() =>
  177. setRows((current) => [
  178. ...current,
  179. { item: '', quantity: '1' },
  180. ])
  181. }
  182. >
  183. <Plus />
  184. Add a product
  185. </Button>
  186. )}
  187. {filled.length > 0 && (
  188. <p className="text-sm text-muted-foreground tabular-nums">
  189. Bought one by one: RM{' '}
  190. {separately.toFixed(2)} · Bundles you can
  191. sell now: {stock}
  192. </p>
  193. )}
  194. </div>
  195. )}
  196. <InputError
  197. message={errors.bundle_items ?? errors.is_bundle}
  198. />
  199. </>
  200. )}
  201. </div>
  202. );
  203. }