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.

bento-box.tsx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import { router, useForm } from '@inertiajs/react';
  2. import { Plus, Trash2, X } from 'lucide-react';
  3. import type { FormEvent } from 'react';
  4. import { useState } from 'react';
  5. import BentoSectionController from '@/actions/App/Http/Controllers/BentoSectionController';
  6. import InputError from '@/components/input-error';
  7. import { Button } from '@/components/ui/button';
  8. import { Input } from '@/components/ui/input';
  9. import { Label } from '@/components/ui/label';
  10. import type {
  11. BentoSectionInfo,
  12. BentoTileInfo,
  13. HeroChoice,
  14. HomeSectionInfo,
  15. LinkSuggestion,
  16. Store,
  17. } from '@/types';
  18. import { SectionName } from '@/components/stores/preview-highlight';
  19. const selectClass =
  20. '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';
  21. const tones: Record<string, string> = {
  22. primary: 'Main colour',
  23. accent: 'Accent colour',
  24. dark: 'Dark',
  25. surface: 'Soft',
  26. };
  27. const MIN_TILES = 3;
  28. const MAX_TILES = 6;
  29. /**
  30. * The Design tab's Bento box: add tiles of different sizes (filled with a sensible start), then change
  31. * each tile's words, link, photo and colour.
  32. */
  33. export default function BentoBox({
  34. store,
  35. bentos,
  36. homeSections,
  37. photos,
  38. links,
  39. busy,
  40. }: {
  41. store: Store;
  42. bentos: BentoSectionInfo[];
  43. homeSections: HomeSectionInfo[];
  44. photos: HeroChoice[];
  45. links: LinkSuggestion[];
  46. busy: boolean;
  47. }) {
  48. const trust = homeSections.find((section) =>
  49. section.label.startsWith('trust bar'),
  50. );
  51. const [after, setAfter] = useState(trust?.id ?? homeSections[0]?.id ?? '');
  52. const [adding, setAdding] = useState(false);
  53. function add() {
  54. router.post(
  55. BentoSectionController.store.url(store.slug),
  56. { after },
  57. {
  58. preserveScroll: true,
  59. onStart: () => setAdding(true),
  60. onFinish: () => setAdding(false),
  61. },
  62. );
  63. }
  64. return (
  65. <div data-test="bento-box">
  66. <p className="text-sm text-muted-foreground">
  67. Tiles of different sizes that lead shoppers to your range,
  68. collections, offers or story.
  69. </p>
  70. {/* Suggestions for every tile link field. */}
  71. <datalist id="bento-links">
  72. {links.map((link) => (
  73. <option key={link.href} value={link.href}>
  74. {link.label}
  75. </option>
  76. ))}
  77. </datalist>
  78. <div className="mt-4 space-y-6">
  79. {bentos.map((bento) => (
  80. <BentoForm
  81. key={bento.section_id}
  82. store={store}
  83. bento={bento}
  84. photos={photos}
  85. busy={busy}
  86. />
  87. ))}
  88. <div className="flex flex-wrap items-end gap-3 border-t pt-4">
  89. <div className="grid min-w-48 flex-1 gap-1.5">
  90. <Label htmlFor="bento-after">
  91. {bentos.length > 0
  92. ? 'Add another set of tiles after'
  93. : 'Add bento tiles after'}
  94. </Label>
  95. <select
  96. id="bento-after"
  97. value={after}
  98. onChange={(event) => setAfter(event.target.value)}
  99. className={selectClass}
  100. >
  101. {homeSections.map((section) => (
  102. <option key={section.id} value={section.id}>
  103. {section.label}
  104. </option>
  105. ))}
  106. </select>
  107. </div>
  108. <Button
  109. type="button"
  110. size="sm"
  111. variant="outline"
  112. onClick={add}
  113. disabled={busy || adding}
  114. >
  115. <Plus />
  116. Add tiles
  117. </Button>
  118. </div>
  119. </div>
  120. </div>
  121. );
  122. }
  123. function BentoForm({
  124. store,
  125. bento,
  126. photos,
  127. busy,
  128. }: {
  129. store: Store;
  130. bento: BentoSectionInfo;
  131. photos: HeroChoice[];
  132. busy: boolean;
  133. }) {
  134. const form = useForm<{
  135. heading: string;
  136. variant: 'mosaic' | 'even';
  137. tiles: BentoTileInfo[];
  138. }>({
  139. heading: bento.heading ?? '',
  140. variant: bento.variant,
  141. tiles: bento.tiles.map((tile) => ({
  142. ...tile,
  143. text: tile.text ?? '',
  144. tone: tile.tone ?? 'surface',
  145. })),
  146. });
  147. const [removing, setRemoving] = useState(false);
  148. const errors = form.errors as Record<string, string | undefined>;
  149. const id = bento.section_id;
  150. function setTile(index: number, field: keyof BentoTileInfo, value: string) {
  151. form.setData(
  152. 'tiles',
  153. form.data.tiles.map((tile, i) =>
  154. i === index
  155. ? {
  156. ...tile,
  157. [field]:
  158. value === '' && field === 'image' ? null : value,
  159. }
  160. : tile,
  161. ),
  162. );
  163. }
  164. function submit(event: FormEvent) {
  165. event.preventDefault();
  166. form.put(
  167. BentoSectionController.update.url({
  168. slug: store.slug,
  169. sectionId: id,
  170. }),
  171. { preserveScroll: true },
  172. );
  173. }
  174. function remove() {
  175. router.delete(
  176. BentoSectionController.destroy.url({
  177. slug: store.slug,
  178. sectionId: id,
  179. }),
  180. { preserveScroll: true },
  181. );
  182. }
  183. const tileError = (index: number) =>
  184. Object.entries(errors).find(([key]) =>
  185. key.startsWith(`tiles.${index}.`),
  186. )?.[1];
  187. const disabled = busy || form.processing;
  188. return (
  189. <form onSubmit={submit} className="space-y-4" noValidate>
  190. <SectionName
  191. target={{ sectionId: id, pageId: 'home', label: 'Bento tiles' }}
  192. className="text-sm font-medium"
  193. >
  194. {bento.heading || 'Bento tiles'}
  195. </SectionName>
  196. <div className="grid gap-3 sm:grid-cols-2">
  197. <div className="grid gap-1.5">
  198. <Label htmlFor={`bento-heading-${id}`}>
  199. Heading above the tiles (optional)
  200. </Label>
  201. <Input
  202. id={`bento-heading-${id}`}
  203. value={form.data.heading}
  204. maxLength={80}
  205. onChange={(event) =>
  206. form.setData('heading', event.target.value)
  207. }
  208. />
  209. </div>
  210. <div className="grid gap-1.5">
  211. <Label htmlFor={`bento-variant-${id}`}>Layout</Label>
  212. <select
  213. id={`bento-variant-${id}`}
  214. value={form.data.variant}
  215. onChange={(event) =>
  216. form.setData(
  217. 'variant',
  218. event.target.value as 'mosaic' | 'even',
  219. )
  220. }
  221. className={selectClass}
  222. >
  223. <option value="mosaic">
  224. One big tile and smaller ones
  225. </option>
  226. <option value="even">Equal tiles</option>
  227. </select>
  228. </div>
  229. </div>
  230. <ol className="space-y-3">
  231. {form.data.tiles.map((tile, index) => (
  232. <li
  233. key={index}
  234. className="space-y-2 rounded-lg border p-3"
  235. data-test="bento-tile"
  236. >
  237. <div className="flex items-center justify-between gap-2">
  238. <p className="text-sm font-medium">
  239. Tile {index + 1}
  240. {index === 0 && form.data.variant === 'mosaic'
  241. ? ' (the big one)'
  242. : ''}
  243. </p>
  244. {form.data.tiles.length > MIN_TILES && (
  245. <Button
  246. type="button"
  247. variant="ghost"
  248. size="icon"
  249. aria-label={`Remove tile ${index + 1}`}
  250. onClick={() =>
  251. form.setData(
  252. 'tiles',
  253. form.data.tiles.filter(
  254. (_, i) => i !== index,
  255. ),
  256. )
  257. }
  258. >
  259. <X />
  260. </Button>
  261. )}
  262. </div>
  263. <div className="grid gap-2 sm:grid-cols-2">
  264. <Input
  265. aria-label={`Tile ${index + 1} title`}
  266. value={tile.title}
  267. maxLength={60}
  268. placeholder="Title"
  269. onChange={(event) =>
  270. setTile(index, 'title', event.target.value)
  271. }
  272. />
  273. <Input
  274. aria-label={`Tile ${index + 1} short text`}
  275. value={tile.text ?? ''}
  276. maxLength={140}
  277. placeholder="Short text (optional)"
  278. onChange={(event) =>
  279. setTile(index, 'text', event.target.value)
  280. }
  281. />
  282. <Input
  283. aria-label={`Tile ${index + 1} link`}
  284. list="bento-links"
  285. value={tile.href}
  286. placeholder="/shop"
  287. onChange={(event) =>
  288. setTile(index, 'href', event.target.value)
  289. }
  290. />
  291. <div className="grid grid-cols-2 gap-2">
  292. <select
  293. aria-label={`Tile ${index + 1} photo`}
  294. value={tile.image ?? ''}
  295. onChange={(event) =>
  296. setTile(
  297. index,
  298. 'image',
  299. event.target.value,
  300. )
  301. }
  302. className={selectClass}
  303. >
  304. <option value="">No photo</option>
  305. {photos.map((photo) => (
  306. <option
  307. key={photo.ref}
  308. value={photo.ref}
  309. >
  310. {photo.label}
  311. </option>
  312. ))}
  313. </select>
  314. <select
  315. aria-label={`Tile ${index + 1} colour`}
  316. value={tile.tone ?? 'surface'}
  317. disabled={Boolean(tile.image)}
  318. onChange={(event) =>
  319. setTile(
  320. index,
  321. 'tone',
  322. event.target.value,
  323. )
  324. }
  325. className={selectClass}
  326. >
  327. {Object.entries(tones).map(
  328. ([value, label]) => (
  329. <option key={value} value={value}>
  330. {label}
  331. </option>
  332. ),
  333. )}
  334. </select>
  335. </div>
  336. </div>
  337. <InputError message={tileError(index)} />
  338. </li>
  339. ))}
  340. </ol>
  341. {form.data.tiles.length < MAX_TILES && (
  342. <Button
  343. type="button"
  344. variant="outline"
  345. size="sm"
  346. onClick={() =>
  347. form.setData('tiles', [
  348. ...form.data.tiles,
  349. {
  350. title: '',
  351. text: '',
  352. href: '/shop',
  353. image: null,
  354. tone: 'surface',
  355. },
  356. ])
  357. }
  358. >
  359. <Plus />
  360. Add a tile
  361. </Button>
  362. )}
  363. <InputError message={errors.section ?? errors.tiles} />
  364. <div className="flex flex-wrap items-center gap-2">
  365. <Button size="sm" disabled={disabled}>
  366. Save tiles
  367. </Button>
  368. {removing ? (
  369. <>
  370. <Button
  371. type="button"
  372. size="sm"
  373. variant="destructive"
  374. onClick={remove}
  375. disabled={disabled}
  376. >
  377. Yes, remove these tiles
  378. </Button>
  379. <Button
  380. type="button"
  381. size="sm"
  382. variant="ghost"
  383. onClick={() => setRemoving(false)}
  384. >
  385. Keep them
  386. </Button>
  387. </>
  388. ) : (
  389. <Button
  390. type="button"
  391. size="sm"
  392. variant="ghost"
  393. onClick={() => setRemoving(true)}
  394. >
  395. <Trash2 />
  396. Remove
  397. </Button>
  398. )}
  399. </div>
  400. </form>
  401. );
  402. }