| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- import { Form, Head, Link } from '@inertiajs/react';
- import { ImageOff } from 'lucide-react';
- import CollectionController from '@/actions/App/Http/Controllers/CollectionController';
- import ProductController from '@/actions/App/Http/Controllers/ProductController';
- import Heading from '@/components/heading';
- import InputError from '@/components/input-error';
- import { Button } from '@/components/ui/button';
- import { Checkbox } from '@/components/ui/checkbox';
- import {
- Dialog,
- DialogClose,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogTitle,
- DialogTrigger,
- } from '@/components/ui/dialog';
- import { Input } from '@/components/ui/input';
- import { Label } from '@/components/ui/label';
- import { dashboard } from '@/routes';
- import type { StoreSummary } from '@/types';
-
- type CollectionData = {
- id: number;
- name: string;
- slug: string;
- description: string | null;
- product_ids: number[];
- };
-
- /** Add a collection, or edit one when `collection` is given. */
- export default function CollectionForm({
- store,
- collection,
- products,
- }: {
- store: StoreSummary;
- collection: CollectionData | null;
- products: { id: number; name: string; image: string | null }[];
- }) {
- const action = collection
- ? CollectionController.update.form({
- slug: store.slug,
- collection: collection.id,
- })
- : CollectionController.store.form(store.slug);
-
- return (
- <>
- <Head
- title={`${collection ? collection.name : 'New collection'} - ${store.name}`}
- />
-
- <div className="mx-auto w-full max-w-2xl space-y-8 p-4">
- <Heading
- title={collection ? 'Edit collection' : 'New collection'}
- description="A group of products shoppers can browse, such as Sun care or Gifts under RM 50."
- />
-
- <Form {...action} className="space-y-6">
- {({ processing, errors }) => (
- <>
- <div className="grid gap-2">
- <Label htmlFor="name">Name</Label>
- <Input
- id="name"
- name="name"
- required
- maxLength={60}
- defaultValue={collection?.name}
- placeholder="Sun care"
- />
- {collection && (
- <p className="text-xs text-muted-foreground">
- Its address stays /collections/
- {collection.slug}, even if you rename
- it.
- </p>
- )}
- <InputError message={errors.name} />
- </div>
-
- <div className="grid gap-2">
- <Label htmlFor="description">
- Short description (optional)
- </Label>
- <textarea
- id="description"
- name="description"
- rows={2}
- maxLength={500}
- defaultValue={collection?.description ?? ''}
- placeholder="Daily protection made for humid weather."
- className="w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-xs outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 md:text-sm"
- />
- <InputError message={errors.description} />
- </div>
-
- <fieldset className="space-y-3">
- <legend className="text-sm font-medium">
- Products in this collection
- </legend>
- {products.length === 0 ? (
- <p className="text-sm text-muted-foreground">
- You have no products yet.{' '}
- <Link
- href={ProductController.create.url(
- store.slug,
- )}
- className="underline underline-offset-2"
- >
- Add a product
- </Link>{' '}
- first.
- </p>
- ) : (
- <ul className="grid gap-2 sm:grid-cols-2">
- {products.map((product) => (
- <li key={product.id}>
- <label
- htmlFor={`product-${product.id}`}
- className="flex cursor-pointer items-center gap-3 rounded-md border p-2 text-sm hover:bg-muted/40"
- >
- <Checkbox
- id={`product-${product.id}`}
- name="product_ids[]"
- value={String(
- product.id,
- )}
- defaultChecked={
- collection?.product_ids.includes(
- product.id,
- ) ?? false
- }
- />
- {product.image ? (
- <img
- src={product.image}
- alt=""
- className="size-9 rounded object-cover"
- />
- ) : (
- <span className="flex size-9 items-center justify-center rounded bg-muted text-muted-foreground">
- <ImageOff className="size-4" />
- </span>
- )}
- <span className="min-w-0 truncate">
- {product.name}
- </span>
- </label>
- </li>
- ))}
- </ul>
- )}
- <InputError
- message={
- errors.product_ids ??
- Object.entries(errors).find(([key]) =>
- key.startsWith('product_ids.'),
- )?.[1]
- }
- />
- </fieldset>
-
- <div className="flex items-center gap-3">
- <Button
- disabled={processing}
- data-test="save-collection-button"
- >
- {collection ? 'Save' : 'Create collection'}
- </Button>
- <Button variant="ghost" asChild>
- <Link
- href={ProductController.index.url(
- store.slug,
- )}
- >
- Cancel
- </Link>
- </Button>
- </div>
- </>
- )}
- </Form>
-
- {collection && (
- <div className="flex flex-wrap items-center justify-between gap-3 rounded-xl border border-destructive/30 p-4">
- <div>
- <p className="font-medium">
- Delete this collection
- </p>
- <p className="text-sm text-muted-foreground">
- The products stay. Only the group is removed.
- </p>
- </div>
- <Dialog>
- <DialogTrigger asChild>
- <Button variant="destructive" size="sm">
- Delete
- </Button>
- </DialogTrigger>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>
- Delete {collection.name}?
- </DialogTitle>
- <DialogDescription>
- Links to this collection will stop
- working. Your products are not changed.
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <DialogClose asChild>
- <Button variant="outline">
- Cancel
- </Button>
- </DialogClose>
- <Form
- {...CollectionController.destroy.form({
- slug: store.slug,
- collection: collection.id,
- })}
- >
- {({ processing }) => (
- <Button
- variant="destructive"
- disabled={processing}
- >
- Delete collection
- </Button>
- )}
- </Form>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- </div>
- )}
- </div>
- </>
- );
- }
-
- CollectionForm.layout = {
- breadcrumbs: [
- { title: 'Dashboard', href: dashboard() },
- { title: 'Collection', href: '#' },
- ],
- };
|