AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

form.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { Form, Head, Link } from '@inertiajs/react';
  2. import { ImageOff } from 'lucide-react';
  3. import CollectionController from '@/actions/App/Http/Controllers/CollectionController';
  4. import ProductController from '@/actions/App/Http/Controllers/ProductController';
  5. import Heading from '@/components/heading';
  6. import InputError from '@/components/input-error';
  7. import { Button } from '@/components/ui/button';
  8. import { Checkbox } from '@/components/ui/checkbox';
  9. import {
  10. Dialog,
  11. DialogClose,
  12. DialogContent,
  13. DialogDescription,
  14. DialogFooter,
  15. DialogHeader,
  16. DialogTitle,
  17. DialogTrigger,
  18. } from '@/components/ui/dialog';
  19. import { Input } from '@/components/ui/input';
  20. import { Label } from '@/components/ui/label';
  21. import { dashboard } from '@/routes';
  22. import type { StoreSummary } from '@/types';
  23. type CollectionData = {
  24. id: number;
  25. name: string;
  26. slug: string;
  27. description: string | null;
  28. product_ids: number[];
  29. };
  30. /** Add a collection, or edit one when `collection` is given. */
  31. export default function CollectionForm({
  32. store,
  33. collection,
  34. products,
  35. }: {
  36. store: StoreSummary;
  37. collection: CollectionData | null;
  38. products: { id: number; name: string; image: string | null }[];
  39. }) {
  40. const action = collection
  41. ? CollectionController.update.form({
  42. slug: store.slug,
  43. collection: collection.id,
  44. })
  45. : CollectionController.store.form(store.slug);
  46. return (
  47. <>
  48. <Head
  49. title={`${collection ? collection.name : 'New collection'} - ${store.name}`}
  50. />
  51. <div className="mx-auto w-full max-w-2xl space-y-8 p-4">
  52. <Heading
  53. title={collection ? 'Edit collection' : 'New collection'}
  54. description="A group of products shoppers can browse, such as Sun care or Gifts under RM 50."
  55. />
  56. <Form {...action} className="space-y-6">
  57. {({ processing, errors }) => (
  58. <>
  59. <div className="grid gap-2">
  60. <Label htmlFor="name">Name</Label>
  61. <Input
  62. id="name"
  63. name="name"
  64. required
  65. maxLength={60}
  66. defaultValue={collection?.name}
  67. placeholder="Sun care"
  68. />
  69. {collection && (
  70. <p className="text-xs text-muted-foreground">
  71. Its address stays /collections/
  72. {collection.slug}, even if you rename
  73. it.
  74. </p>
  75. )}
  76. <InputError message={errors.name} />
  77. </div>
  78. <div className="grid gap-2">
  79. <Label htmlFor="description">
  80. Short description (optional)
  81. </Label>
  82. <textarea
  83. id="description"
  84. name="description"
  85. rows={2}
  86. maxLength={500}
  87. defaultValue={collection?.description ?? ''}
  88. placeholder="Daily protection made for humid weather."
  89. 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"
  90. />
  91. <InputError message={errors.description} />
  92. </div>
  93. <fieldset className="space-y-3">
  94. <legend className="text-sm font-medium">
  95. Products in this collection
  96. </legend>
  97. {products.length === 0 ? (
  98. <p className="text-sm text-muted-foreground">
  99. You have no products yet.{' '}
  100. <Link
  101. href={ProductController.create.url(
  102. store.slug,
  103. )}
  104. className="underline underline-offset-2"
  105. >
  106. Add a product
  107. </Link>{' '}
  108. first.
  109. </p>
  110. ) : (
  111. <ul className="grid gap-2 sm:grid-cols-2">
  112. {products.map((product) => (
  113. <li key={product.id}>
  114. <label
  115. htmlFor={`product-${product.id}`}
  116. className="flex cursor-pointer items-center gap-3 rounded-md border p-2 text-sm hover:bg-muted/40"
  117. >
  118. <Checkbox
  119. id={`product-${product.id}`}
  120. name="product_ids[]"
  121. value={String(
  122. product.id,
  123. )}
  124. defaultChecked={
  125. collection?.product_ids.includes(
  126. product.id,
  127. ) ?? false
  128. }
  129. />
  130. {product.image ? (
  131. <img
  132. src={product.image}
  133. alt=""
  134. className="size-9 rounded object-cover"
  135. />
  136. ) : (
  137. <span className="flex size-9 items-center justify-center rounded bg-muted text-muted-foreground">
  138. <ImageOff className="size-4" />
  139. </span>
  140. )}
  141. <span className="min-w-0 truncate">
  142. {product.name}
  143. </span>
  144. </label>
  145. </li>
  146. ))}
  147. </ul>
  148. )}
  149. <InputError
  150. message={
  151. errors.product_ids ??
  152. Object.entries(errors).find(([key]) =>
  153. key.startsWith('product_ids.'),
  154. )?.[1]
  155. }
  156. />
  157. </fieldset>
  158. <div className="flex items-center gap-3">
  159. <Button
  160. disabled={processing}
  161. data-test="save-collection-button"
  162. >
  163. {collection ? 'Save' : 'Create collection'}
  164. </Button>
  165. <Button variant="ghost" asChild>
  166. <Link
  167. href={ProductController.index.url(
  168. store.slug,
  169. )}
  170. >
  171. Cancel
  172. </Link>
  173. </Button>
  174. </div>
  175. </>
  176. )}
  177. </Form>
  178. {collection && (
  179. <div className="flex flex-wrap items-center justify-between gap-3 rounded-xl border border-destructive/30 p-4">
  180. <div>
  181. <p className="font-medium">
  182. Delete this collection
  183. </p>
  184. <p className="text-sm text-muted-foreground">
  185. The products stay. Only the group is removed.
  186. </p>
  187. </div>
  188. <Dialog>
  189. <DialogTrigger asChild>
  190. <Button variant="destructive" size="sm">
  191. Delete
  192. </Button>
  193. </DialogTrigger>
  194. <DialogContent>
  195. <DialogHeader>
  196. <DialogTitle>
  197. Delete {collection.name}?
  198. </DialogTitle>
  199. <DialogDescription>
  200. Links to this collection will stop
  201. working. Your products are not changed.
  202. </DialogDescription>
  203. </DialogHeader>
  204. <DialogFooter>
  205. <DialogClose asChild>
  206. <Button variant="outline">
  207. Cancel
  208. </Button>
  209. </DialogClose>
  210. <Form
  211. {...CollectionController.destroy.form({
  212. slug: store.slug,
  213. collection: collection.id,
  214. })}
  215. >
  216. {({ processing }) => (
  217. <Button
  218. variant="destructive"
  219. disabled={processing}
  220. >
  221. Delete collection
  222. </Button>
  223. )}
  224. </Form>
  225. </DialogFooter>
  226. </DialogContent>
  227. </Dialog>
  228. </div>
  229. )}
  230. </div>
  231. </>
  232. );
  233. }
  234. CollectionForm.layout = {
  235. breadcrumbs: [
  236. { title: 'Dashboard', href: dashboard() },
  237. { title: 'Collection', href: '#' },
  238. ],
  239. };