| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- import { router, useForm } from '@inertiajs/react';
- import { ChevronDown, LayoutGrid, Plus, Trash2, X } from 'lucide-react';
- import type { FormEvent } from 'react';
- import { useState } from 'react';
- import BentoSectionController from '@/actions/App/Http/Controllers/BentoSectionController';
- import InputError from '@/components/input-error';
- import { Button } from '@/components/ui/button';
- import { Input } from '@/components/ui/input';
- import { Label } from '@/components/ui/label';
- import type {
- BentoSectionInfo,
- BentoTileInfo,
- HeroChoice,
- HomeSectionInfo,
- LinkSuggestion,
- Store,
- } from '@/types';
- import { SectionName } from '@/components/stores/preview-highlight';
-
- const selectClass =
- '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';
-
- const tones: Record<string, string> = {
- primary: 'Main colour',
- accent: 'Accent colour',
- dark: 'Dark',
- surface: 'Soft',
- };
-
- const MIN_TILES = 3;
- const MAX_TILES = 6;
-
- /**
- * The Design tab's Bento box: add tiles of different sizes (filled with a sensible start), then change
- * each tile's words, link, photo and colour.
- */
- export default function BentoBox({
- store,
- bentos,
- homeSections,
- photos,
- links,
- busy,
- }: {
- store: Store;
- bentos: BentoSectionInfo[];
- homeSections: HomeSectionInfo[];
- photos: HeroChoice[];
- links: LinkSuggestion[];
- busy: boolean;
- }) {
- const trust = homeSections.find((section) =>
- section.label.startsWith('trust bar'),
- );
- const [after, setAfter] = useState(trust?.id ?? homeSections[0]?.id ?? '');
- const [adding, setAdding] = useState(false);
-
- function add() {
- router.post(
- BentoSectionController.store.url(store.slug),
- { after },
- {
- preserveScroll: true,
- onStart: () => setAdding(true),
- onFinish: () => setAdding(false),
- },
- );
- }
-
- return (
- <details className="group rounded-xl border p-4" data-test="bento-box">
- <summary className="flex cursor-pointer list-none items-start gap-3 [&::-webkit-details-marker]:hidden">
- <LayoutGrid className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h3 className="font-medium">
- Bento tiles{' '}
- {bentos.length > 0 && (
- <span className="text-muted-foreground">
- ({bentos.length})
- </span>
- )}
- </h3>
- <p className="mt-0.5 text-sm text-muted-foreground">
- Tiles of different sizes that lead shoppers to your
- range, collections, offers or story. Open to add or
- change them.
- </p>
- </div>
- <ChevronDown
- className="mt-1 ml-auto size-4 shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
- aria-hidden="true"
- />
- </summary>
-
- {/* Suggestions for every tile link field. */}
- <datalist id="bento-links">
- {links.map((link) => (
- <option key={link.href} value={link.href}>
- {link.label}
- </option>
- ))}
- </datalist>
-
- <div className="mt-4 space-y-6">
- {bentos.map((bento) => (
- <BentoForm
- key={bento.section_id}
- store={store}
- bento={bento}
- photos={photos}
- busy={busy}
- />
- ))}
-
- <div className="flex flex-wrap items-end gap-3 border-t pt-4">
- <div className="grid min-w-48 flex-1 gap-1.5">
- <Label htmlFor="bento-after">
- {bentos.length > 0
- ? 'Add another set of tiles after'
- : 'Add bento tiles after'}
- </Label>
- <select
- id="bento-after"
- value={after}
- onChange={(event) => setAfter(event.target.value)}
- className={selectClass}
- >
- {homeSections.map((section) => (
- <option key={section.id} value={section.id}>
- {section.label}
- </option>
- ))}
- </select>
- </div>
- <Button
- type="button"
- size="sm"
- variant="outline"
- onClick={add}
- disabled={busy || adding}
- >
- <Plus />
- Add tiles
- </Button>
- </div>
- </div>
- </details>
- );
- }
-
- function BentoForm({
- store,
- bento,
- photos,
- busy,
- }: {
- store: Store;
- bento: BentoSectionInfo;
- photos: HeroChoice[];
- busy: boolean;
- }) {
- const form = useForm<{
- heading: string;
- variant: 'mosaic' | 'even';
- tiles: BentoTileInfo[];
- }>({
- heading: bento.heading ?? '',
- variant: bento.variant,
- tiles: bento.tiles.map((tile) => ({
- ...tile,
- text: tile.text ?? '',
- tone: tile.tone ?? 'surface',
- })),
- });
- const [removing, setRemoving] = useState(false);
- const errors = form.errors as Record<string, string | undefined>;
- const id = bento.section_id;
-
- function setTile(index: number, field: keyof BentoTileInfo, value: string) {
- form.setData(
- 'tiles',
- form.data.tiles.map((tile, i) =>
- i === index
- ? {
- ...tile,
- [field]:
- value === '' && field === 'image' ? null : value,
- }
- : tile,
- ),
- );
- }
-
- function submit(event: FormEvent) {
- event.preventDefault();
- form.put(
- BentoSectionController.update.url({
- slug: store.slug,
- sectionId: id,
- }),
- { preserveScroll: true },
- );
- }
-
- function remove() {
- router.delete(
- BentoSectionController.destroy.url({
- slug: store.slug,
- sectionId: id,
- }),
- { preserveScroll: true },
- );
- }
-
- const tileError = (index: number) =>
- Object.entries(errors).find(([key]) =>
- key.startsWith(`tiles.${index}.`),
- )?.[1];
- const disabled = busy || form.processing;
-
- return (
- <form onSubmit={submit} className="space-y-4" noValidate>
- <SectionName
- target={{ sectionId: id, pageId: 'home', label: 'Bento tiles' }}
- className="text-sm font-medium"
- >
- {bento.heading || 'Bento tiles'}
- </SectionName>
- <div className="grid gap-3 sm:grid-cols-2">
- <div className="grid gap-1.5">
- <Label htmlFor={`bento-heading-${id}`}>
- Heading above the tiles (optional)
- </Label>
- <Input
- id={`bento-heading-${id}`}
- value={form.data.heading}
- maxLength={80}
- onChange={(event) =>
- form.setData('heading', event.target.value)
- }
- />
- </div>
- <div className="grid gap-1.5">
- <Label htmlFor={`bento-variant-${id}`}>Layout</Label>
- <select
- id={`bento-variant-${id}`}
- value={form.data.variant}
- onChange={(event) =>
- form.setData(
- 'variant',
- event.target.value as 'mosaic' | 'even',
- )
- }
- className={selectClass}
- >
- <option value="mosaic">
- One big tile and smaller ones
- </option>
- <option value="even">Equal tiles</option>
- </select>
- </div>
- </div>
-
- <ol className="space-y-3">
- {form.data.tiles.map((tile, index) => (
- <li
- key={index}
- className="space-y-2 rounded-lg border p-3"
- data-test="bento-tile"
- >
- <div className="flex items-center justify-between gap-2">
- <p className="text-sm font-medium">
- Tile {index + 1}
- {index === 0 && form.data.variant === 'mosaic'
- ? ' (the big one)'
- : ''}
- </p>
- {form.data.tiles.length > MIN_TILES && (
- <Button
- type="button"
- variant="ghost"
- size="icon"
- aria-label={`Remove tile ${index + 1}`}
- onClick={() =>
- form.setData(
- 'tiles',
- form.data.tiles.filter(
- (_, i) => i !== index,
- ),
- )
- }
- >
- <X />
- </Button>
- )}
- </div>
- <div className="grid gap-2 sm:grid-cols-2">
- <Input
- aria-label={`Tile ${index + 1} title`}
- value={tile.title}
- maxLength={60}
- placeholder="Title"
- onChange={(event) =>
- setTile(index, 'title', event.target.value)
- }
- />
- <Input
- aria-label={`Tile ${index + 1} short text`}
- value={tile.text ?? ''}
- maxLength={140}
- placeholder="Short text (optional)"
- onChange={(event) =>
- setTile(index, 'text', event.target.value)
- }
- />
- <Input
- aria-label={`Tile ${index + 1} link`}
- list="bento-links"
- value={tile.href}
- placeholder="/shop"
- onChange={(event) =>
- setTile(index, 'href', event.target.value)
- }
- />
- <div className="grid grid-cols-2 gap-2">
- <select
- aria-label={`Tile ${index + 1} photo`}
- value={tile.image ?? ''}
- onChange={(event) =>
- setTile(
- index,
- 'image',
- event.target.value,
- )
- }
- className={selectClass}
- >
- <option value="">No photo</option>
- {photos.map((photo) => (
- <option
- key={photo.ref}
- value={photo.ref}
- >
- {photo.label}
- </option>
- ))}
- </select>
- <select
- aria-label={`Tile ${index + 1} colour`}
- value={tile.tone ?? 'surface'}
- disabled={Boolean(tile.image)}
- onChange={(event) =>
- setTile(
- index,
- 'tone',
- event.target.value,
- )
- }
- className={selectClass}
- >
- {Object.entries(tones).map(
- ([value, label]) => (
- <option key={value} value={value}>
- {label}
- </option>
- ),
- )}
- </select>
- </div>
- </div>
- <InputError message={tileError(index)} />
- </li>
- ))}
- </ol>
-
- {form.data.tiles.length < MAX_TILES && (
- <Button
- type="button"
- variant="outline"
- size="sm"
- onClick={() =>
- form.setData('tiles', [
- ...form.data.tiles,
- {
- title: '',
- text: '',
- href: '/shop',
- image: null,
- tone: 'surface',
- },
- ])
- }
- >
- <Plus />
- Add a tile
- </Button>
- )}
-
- <InputError message={errors.section ?? errors.tiles} />
-
- <div className="flex flex-wrap items-center gap-2">
- <Button size="sm" disabled={disabled}>
- Save tiles
- </Button>
- {removing ? (
- <>
- <Button
- type="button"
- size="sm"
- variant="destructive"
- onClick={remove}
- disabled={disabled}
- >
- Yes, remove these tiles
- </Button>
- <Button
- type="button"
- size="sm"
- variant="ghost"
- onClick={() => setRemoving(false)}
- >
- Keep them
- </Button>
- </>
- ) : (
- <Button
- type="button"
- size="sm"
- variant="ghost"
- onClick={() => setRemoving(true)}
- >
- <Trash2 />
- Remove
- </Button>
- )}
- </div>
- </form>
- );
- }
|