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'; 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 = { 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 (

Bento tiles{' '} {bentos.length > 0 && ( ({bentos.length}) )}

Tiles of different sizes that lead shoppers to your range, collections, offers or story. Open to add or change them.

{/* Suggestions for every tile link field. */} {links.map((link) => ( ))}
{bentos.map((bento) => ( ))}
); } 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; 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.setData('heading', event.target.value) } />
    {form.data.tiles.map((tile, index) => (
  1. Tile {index + 1} {index === 0 && form.data.variant === 'mosaic' ? ' (the big one)' : ''}

    {form.data.tiles.length > MIN_TILES && ( )}
    setTile(index, 'title', event.target.value) } /> setTile(index, 'text', event.target.value) } /> setTile(index, 'href', event.target.value) } />
  2. ))}
{form.data.tiles.length < MAX_TILES && ( )}
{removing ? ( <> ) : ( )}
); }