| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- import { router, useForm } from '@inertiajs/react';
- import { Check, ImageIcon, Upload } from 'lucide-react';
- import type { FormEvent } from 'react';
- import { useState } from 'react';
- import HeroImageController from '@/actions/App/Http/Controllers/HeroImageController';
- import InputError from '@/components/input-error';
- import { Button } from '@/components/ui/button';
- import { cn } from '@/lib/utils';
- import type { HeroChoice, HeroSectionInfo, Store } from '@/types';
-
- const layouts: { value: HeroSectionInfo['variant']; label: string }[] = [
- { value: 'image-full', label: 'Full-width photo' },
- { value: 'split', label: 'Photo beside words' },
- { value: 'centered', label: 'Words only' },
- ];
-
- /**
- * The Design tab's "Hero" box: the layout of each hero section and the photo it shows.
- * Automatic uses the store's newest product photo, so a store looks finished as soon as it has photos.
- */
- export default function HeroPicker({
- store,
- sections,
- choices,
- busy,
- }: {
- store: Store;
- sections: HeroSectionInfo[];
- choices: HeroChoice[];
- busy: boolean;
- }) {
- if (sections.length === 0) {
- return null;
- }
-
- return (
- <div
- className="space-y-5 rounded-xl border p-4"
- data-test="hero-picker"
- >
- <div className="flex items-start gap-3">
- <ImageIcon className="mt-0.5 size-5 shrink-0 text-muted-foreground" />
- <div>
- <h3 className="font-medium">Hero</h3>
- <p className="mt-0.5 text-sm text-muted-foreground">
- The big banner at the top. Choose its layout and photo,
- or upload your own banner photo.
- </p>
- </div>
- </div>
-
- {sections.map((section) => (
- <HeroRow
- key={section.section_id}
- store={store}
- section={section}
- choices={choices}
- busy={busy}
- showTitle={sections.length > 1}
- />
- ))}
- </div>
- );
- }
-
- function HeroRow({
- store,
- section,
- choices,
- busy,
- showTitle,
- }: {
- store: Store;
- section: HeroSectionInfo;
- choices: HeroChoice[];
- busy: boolean;
- showTitle: boolean;
- }) {
- const [saving, setSaving] = useState(false);
- const upload = useForm<{ photo: File | null }>({ photo: null });
- const ids = { upload: `hero-photo-${section.section_id}` };
- const shown = section.variant !== 'centered';
- const chosen =
- section.image !== null &&
- choices.some((choice) => choice.ref === section.image)
- ? section.image
- : null;
-
- function save(image: string | null, variant: HeroSectionInfo['variant']) {
- router.put(
- HeroImageController.update.url({
- slug: store.slug,
- sectionId: section.section_id,
- }),
- { image, variant },
- {
- preserveScroll: true,
- onStart: () => setSaving(true),
- onFinish: () => setSaving(false),
- },
- );
- }
-
- function send(event: FormEvent) {
- event.preventDefault();
- upload.post(
- HeroImageController.upload.url({
- slug: store.slug,
- sectionId: section.section_id,
- }),
- {
- preserveScroll: true,
- forceFormData: true,
- onSuccess: () => upload.reset(),
- },
- );
- }
-
- const disabled = busy || saving || upload.processing;
-
- return (
- <div className="space-y-4 border-t pt-4">
- {showTitle && (
- <p className="text-sm">
- “{section.headline}”
- {section.page_id !== 'home' && (
- <span className="text-muted-foreground">
- {' '}
- on {section.page_title}
- </span>
- )}
- </p>
- )}
-
- <fieldset className="space-y-2">
- <legend className="text-sm font-medium">Layout</legend>
- <div className="flex flex-wrap gap-2">
- {layouts.map((layout) => (
- <button
- key={layout.value}
- type="button"
- disabled={disabled}
- aria-pressed={section.variant === layout.value}
- onClick={() => save(chosen, layout.value)}
- className={cn(
- 'rounded-md border px-3 py-1.5 text-sm transition-colors disabled:opacity-50',
- section.variant === layout.value
- ? 'border-foreground bg-foreground text-background'
- : 'hover:bg-muted',
- )}
- >
- {layout.label}
- </button>
- ))}
- </div>
- </fieldset>
-
- {shown && (
- <fieldset className="space-y-2">
- <legend className="text-sm font-medium">Photo</legend>
- <ul className="grid grid-cols-3 gap-2 sm:grid-cols-5">
- <li>
- <PhotoButton
- label="Automatic"
- hint="Your newest product photo"
- selected={chosen === null}
- disabled={disabled}
- onClick={() => save(null, section.variant)}
- />
- </li>
- {choices.map((choice) => (
- <li key={choice.ref}>
- <PhotoButton
- label={choice.label}
- url={choice.url}
- selected={chosen === choice.ref}
- disabled={disabled}
- onClick={() =>
- save(choice.ref, section.variant)
- }
- />
- </li>
- ))}
- </ul>
- {choices.length === 0 && (
- <p className="text-sm text-muted-foreground">
- Add photos to your products, or upload a banner
- photo below. Until then the hero shows a soft
- pattern in your colours.
- </p>
- )}
- </fieldset>
- )}
-
- <form
- onSubmit={send}
- className="flex flex-wrap items-center gap-2"
- noValidate
- >
- <label htmlFor={ids.upload} className="sr-only">
- Upload a banner photo
- </label>
- <input
- id={ids.upload}
- type="file"
- accept="image/jpeg,image/png,image/webp"
- onChange={(event) =>
- upload.setData('photo', event.target.files?.[0] ?? null)
- }
- className="max-w-full text-sm file:mr-3 file:rounded-md file:border file:bg-transparent file:px-3 file:py-1.5 file:text-sm file:font-medium"
- />
- <Button
- size="sm"
- variant="outline"
- disabled={disabled || upload.data.photo === null}
- >
- <Upload />
- Upload banner photo
- </Button>
- <p className="w-full text-xs text-muted-foreground">
- JPG, PNG or WebP, up to 6 MB. A wide photo at least 1600
- pixels across looks best.
- </p>
- <InputError message={upload.errors.photo} />
- </form>
- </div>
- );
- }
-
- function PhotoButton({
- label,
- hint,
- url,
- selected,
- disabled,
- onClick,
- }: {
- label: string;
- hint?: string;
- url?: string;
- selected: boolean;
- disabled: boolean;
- onClick: () => void;
- }) {
- return (
- <button
- type="button"
- onClick={onClick}
- disabled={disabled}
- aria-pressed={selected}
- title={hint ?? label}
- className={cn(
- 'group relative block w-full overflow-hidden rounded-md border text-left disabled:opacity-50',
- selected && 'ring-2 ring-foreground ring-offset-2',
- )}
- >
- {url ? (
- <img
- src={url}
- alt=""
- className="aspect-square w-full object-cover"
- />
- ) : (
- <span className="flex aspect-square w-full items-center justify-center bg-muted p-2 text-center text-xs text-muted-foreground">
- {hint}
- </span>
- )}
- <span className="block truncate px-1.5 py-1 text-xs">{label}</span>
- {selected && (
- <span className="absolute top-1 right-1 rounded-full bg-foreground p-0.5 text-background">
- <Check className="size-3" />
- </span>
- )}
- </button>
- );
- }
|