AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

automatic-form.tsx 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import { Form, Head, Link } from '@inertiajs/react';
  2. import { useState } from 'react';
  3. import AutomaticDiscountController from '@/actions/App/Http/Controllers/AutomaticDiscountController';
  4. import DiscountCodeController from '@/actions/App/Http/Controllers/DiscountCodeController';
  5. import { BackButton } from '@/components/back-button';
  6. import Heading from '@/components/heading';
  7. import InputError from '@/components/input-error';
  8. import { Button } from '@/components/ui/button';
  9. import { Checkbox } from '@/components/ui/checkbox';
  10. import {
  11. Dialog,
  12. DialogClose,
  13. DialogContent,
  14. DialogDescription,
  15. DialogFooter,
  16. DialogHeader,
  17. DialogTitle,
  18. DialogTrigger,
  19. } from '@/components/ui/dialog';
  20. import { Input } from '@/components/ui/input';
  21. import { Label } from '@/components/ui/label';
  22. import { dashboard } from '@/routes';
  23. import type { AutomaticDiscount, StoreSummary } from '@/types';
  24. /** Add an automatic discount, or edit one when `offer` is given. */
  25. export default function AutomaticDiscountForm({
  26. store,
  27. offer,
  28. products,
  29. }: {
  30. store: StoreSummary;
  31. offer: AutomaticDiscount | null;
  32. products: { id: number; name: string }[];
  33. }) {
  34. const [type, setType] = useState<'percent' | 'fixed'>(
  35. offer?.type ?? 'percent',
  36. );
  37. const [appliesTo, setAppliesTo] = useState<'all' | 'chosen'>(
  38. offer?.product_ids ? 'chosen' : 'all',
  39. );
  40. const action = offer
  41. ? AutomaticDiscountController.update.form({
  42. slug: store.slug,
  43. offer: offer.id,
  44. })
  45. : AutomaticDiscountController.store.form(store.slug);
  46. return (
  47. <>
  48. <Head
  49. title={`${offer ? offer.name : 'New automatic discount'} - ${store.name}`}
  50. />
  51. <div className="mx-auto w-full max-w-2xl space-y-8 p-4">
  52. <div className="space-y-4">
  53. <BackButton
  54. href={DiscountCodeController.index.url(store.slug)}
  55. >
  56. Back to discounts
  57. </BackButton>
  58. <Heading
  59. title={
  60. offer
  61. ? 'Edit automatic discount'
  62. : 'New automatic discount'
  63. }
  64. description="No code needed. It applies by itself when a cart qualifies."
  65. />
  66. </div>
  67. <Form {...action} className="space-y-6">
  68. {({ processing, errors }) => (
  69. <>
  70. <div className="grid gap-2">
  71. <Label htmlFor="name">Name</Label>
  72. <Input
  73. id="name"
  74. name="name"
  75. required
  76. maxLength={40}
  77. defaultValue={offer?.name}
  78. placeholder="Raya Sale"
  79. aria-describedby="name-help"
  80. />
  81. <p
  82. id="name-help"
  83. className="text-xs text-muted-foreground"
  84. >
  85. Shoppers see it on product pages, in the
  86. cart and on their order.
  87. </p>
  88. <InputError message={errors.name} />
  89. </div>
  90. <fieldset className="grid gap-3">
  91. <legend className="mb-1 text-sm font-medium">
  92. Discount
  93. </legend>
  94. <div className="flex flex-wrap gap-4 text-sm">
  95. <label className="flex items-center gap-2">
  96. <input
  97. type="radio"
  98. name="type"
  99. value="percent"
  100. checked={type === 'percent'}
  101. onChange={() => setType('percent')}
  102. />
  103. Percentage off
  104. </label>
  105. <label className="flex items-center gap-2">
  106. <input
  107. type="radio"
  108. name="type"
  109. value="fixed"
  110. checked={type === 'fixed'}
  111. onChange={() => setType('fixed')}
  112. />
  113. Fixed amount off
  114. </label>
  115. </div>
  116. <div className="grid gap-2 sm:max-w-xs">
  117. <Label htmlFor="value">
  118. {type === 'percent'
  119. ? 'Percentage (%)'
  120. : 'Amount (RM)'}
  121. </Label>
  122. <Input
  123. id="value"
  124. name="value"
  125. required
  126. inputMode={
  127. type === 'percent'
  128. ? 'numeric'
  129. : 'decimal'
  130. }
  131. defaultValue={offer?.value}
  132. placeholder={
  133. type === 'percent' ? '10' : '5.00'
  134. }
  135. />
  136. <InputError message={errors.value} />
  137. </div>
  138. <InputError message={errors.type} />
  139. </fieldset>
  140. <fieldset className="grid gap-3">
  141. <legend className="mb-1 text-sm font-medium">
  142. When it applies (leave both empty for a sale
  143. on every cart)
  144. </legend>
  145. <div className="grid gap-6 sm:grid-cols-2">
  146. <div className="grid gap-2">
  147. <Label htmlFor="minimum_quantity">
  148. Buy at least (items)
  149. </Label>
  150. <Input
  151. id="minimum_quantity"
  152. name="minimum_quantity"
  153. type="number"
  154. min={1}
  155. step={1}
  156. defaultValue={
  157. offer?.minimum_quantity ?? ''
  158. }
  159. placeholder="2"
  160. />
  161. <InputError
  162. message={errors.minimum_quantity}
  163. />
  164. </div>
  165. <div className="grid gap-2">
  166. <Label htmlFor="minimum_subtotal">
  167. Spend at least (RM)
  168. </Label>
  169. <Input
  170. id="minimum_subtotal"
  171. name="minimum_subtotal"
  172. inputMode="decimal"
  173. defaultValue={
  174. offer?.minimum_subtotal ?? ''
  175. }
  176. placeholder="100.00"
  177. />
  178. <InputError
  179. message={errors.minimum_subtotal}
  180. />
  181. </div>
  182. </div>
  183. </fieldset>
  184. <fieldset className="grid gap-3">
  185. <legend className="mb-1 text-sm font-medium">
  186. Products
  187. </legend>
  188. <div className="flex flex-wrap gap-4 text-sm">
  189. <label className="flex items-center gap-2">
  190. <input
  191. type="radio"
  192. name="applies_to"
  193. value="all"
  194. checked={appliesTo === 'all'}
  195. onChange={() => setAppliesTo('all')}
  196. />
  197. All products
  198. </label>
  199. <label className="flex items-center gap-2">
  200. <input
  201. type="radio"
  202. name="applies_to"
  203. value="chosen"
  204. checked={appliesTo === 'chosen'}
  205. onChange={() =>
  206. setAppliesTo('chosen')
  207. }
  208. />
  209. Only some products
  210. </label>
  211. </div>
  212. {appliesTo === 'chosen' && (
  213. <div className="space-y-2 rounded-md border p-3">
  214. <p className="text-xs text-muted-foreground">
  215. Only these count towards the
  216. minimum, and only these get cheaper.
  217. </p>
  218. <ul className="grid gap-2 sm:grid-cols-2">
  219. {products.map((product) => (
  220. <li
  221. key={product.id}
  222. className="flex items-center gap-2 text-sm"
  223. >
  224. <Checkbox
  225. id={`product-${product.id}`}
  226. name="product_ids[]"
  227. value={String(
  228. product.id,
  229. )}
  230. defaultChecked={
  231. offer?.product_ids?.includes(
  232. product.id,
  233. ) ?? false
  234. }
  235. />
  236. <Label
  237. htmlFor={`product-${product.id}`}
  238. className="font-normal"
  239. >
  240. {product.name}
  241. </Label>
  242. </li>
  243. ))}
  244. </ul>
  245. </div>
  246. )}
  247. <InputError
  248. message={
  249. errors.product_ids ??
  250. Object.entries(errors).find(([key]) =>
  251. key.startsWith('product_ids.'),
  252. )?.[1]
  253. }
  254. />
  255. </fieldset>
  256. <div className="grid gap-6 sm:grid-cols-2">
  257. <div className="grid gap-2">
  258. <Label htmlFor="starts_on">
  259. First day (optional)
  260. </Label>
  261. <Input
  262. id="starts_on"
  263. name="starts_on"
  264. type="date"
  265. defaultValue={offer?.starts_on ?? ''}
  266. />
  267. <InputError message={errors.starts_on} />
  268. </div>
  269. <div className="grid gap-2">
  270. <Label htmlFor="ends_on">
  271. Last day (optional)
  272. </Label>
  273. <Input
  274. id="ends_on"
  275. name="ends_on"
  276. type="date"
  277. defaultValue={offer?.ends_on ?? ''}
  278. />
  279. <InputError message={errors.ends_on} />
  280. </div>
  281. </div>
  282. <div className="flex items-center gap-3">
  283. <input type="hidden" name="active" value="0" />
  284. <Checkbox
  285. id="active"
  286. name="active"
  287. value="1"
  288. defaultChecked={offer?.active ?? true}
  289. />
  290. <Label htmlFor="active">
  291. Active (applies to carts now)
  292. </Label>
  293. </div>
  294. <p className="text-sm text-muted-foreground">
  295. Only one discount applies per order. If a
  296. shopper also types a code, they get whichever
  297. saves more.
  298. </p>
  299. <div className="flex items-center gap-3">
  300. <Button
  301. disabled={processing}
  302. data-test="save-automatic-discount-button"
  303. >
  304. {offer ? 'Save' : 'Create'}
  305. </Button>
  306. <Button variant="ghost" asChild>
  307. <Link
  308. href={DiscountCodeController.index.url(
  309. store.slug,
  310. )}
  311. >
  312. Cancel
  313. </Link>
  314. </Button>
  315. </div>
  316. </>
  317. )}
  318. </Form>
  319. {offer && (
  320. <div className="flex flex-wrap items-center justify-between gap-3 rounded-xl border border-destructive/30 p-4">
  321. <div>
  322. <p className="font-medium">
  323. Delete this automatic discount
  324. </p>
  325. <p className="text-sm text-muted-foreground">
  326. Carts stop getting it. Past orders keep their
  327. discount.
  328. </p>
  329. </div>
  330. <Dialog>
  331. <DialogTrigger asChild>
  332. <Button variant="destructive" size="sm">
  333. Delete
  334. </Button>
  335. </DialogTrigger>
  336. <DialogContent>
  337. <DialogHeader>
  338. <DialogTitle>
  339. Delete {offer.name}?
  340. </DialogTitle>
  341. <DialogDescription>
  342. This cannot be undone. Past orders are
  343. not changed.
  344. </DialogDescription>
  345. </DialogHeader>
  346. <DialogFooter>
  347. <DialogClose asChild>
  348. <Button variant="outline">
  349. Cancel
  350. </Button>
  351. </DialogClose>
  352. <Form
  353. {...AutomaticDiscountController.destroy.form(
  354. {
  355. slug: store.slug,
  356. offer: offer.id,
  357. },
  358. )}
  359. >
  360. {({ processing }) => (
  361. <Button
  362. variant="destructive"
  363. disabled={processing}
  364. >
  365. Delete
  366. </Button>
  367. )}
  368. </Form>
  369. </DialogFooter>
  370. </DialogContent>
  371. </Dialog>
  372. </div>
  373. )}
  374. </div>
  375. </>
  376. );
  377. }
  378. AutomaticDiscountForm.layout = {
  379. breadcrumbs: [
  380. { title: 'Overview', href: dashboard() },
  381. { title: 'Automatic discount', href: '#' },
  382. ],
  383. };