AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

automatic-form.tsx 19KB

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