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.

form.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { Form, Head, Link } from '@inertiajs/react';
  2. import { useState } from 'react';
  3. import DiscountCodeController from '@/actions/App/Http/Controllers/DiscountCodeController';
  4. import Heading from '@/components/heading';
  5. import InputError from '@/components/input-error';
  6. import { Button } from '@/components/ui/button';
  7. import { Checkbox } from '@/components/ui/checkbox';
  8. import {
  9. Dialog,
  10. DialogClose,
  11. DialogContent,
  12. DialogDescription,
  13. DialogFooter,
  14. DialogHeader,
  15. DialogTitle,
  16. DialogTrigger,
  17. } from '@/components/ui/dialog';
  18. import { Input } from '@/components/ui/input';
  19. import { Label } from '@/components/ui/label';
  20. import { dashboard } from '@/routes';
  21. import type { DiscountCode, StoreSummary } from '@/types';
  22. /** Add a new discount code, or edit one when `code` is given. */
  23. export default function DiscountForm({
  24. store,
  25. code,
  26. }: {
  27. store: StoreSummary;
  28. code: DiscountCode | null;
  29. }) {
  30. const [type, setType] = useState<'percent' | 'fixed'>(
  31. code?.type ?? 'percent',
  32. );
  33. const action = code
  34. ? DiscountCodeController.update.form({
  35. slug: store.slug,
  36. discount: code.id,
  37. })
  38. : DiscountCodeController.store.form(store.slug);
  39. return (
  40. <>
  41. <Head
  42. title={`${code ? code.code : 'New discount code'} - ${store.name}`}
  43. />
  44. <div className="mx-auto w-full max-w-2xl space-y-8 p-4">
  45. <Heading
  46. title={code ? 'Edit discount code' : 'New discount code'}
  47. description={`For ${store.name}. Shoppers type the code in their cart.`}
  48. />
  49. <Form {...action} className="space-y-6">
  50. {({ processing, errors }) => (
  51. <>
  52. <div className="grid gap-2">
  53. <Label htmlFor="code">Code</Label>
  54. <Input
  55. id="code"
  56. name="code"
  57. required
  58. maxLength={30}
  59. defaultValue={code?.code}
  60. placeholder="RAYA10"
  61. className="font-mono uppercase"
  62. autoCapitalize="characters"
  63. aria-describedby="code-help"
  64. />
  65. <p
  66. id="code-help"
  67. className="text-xs text-muted-foreground"
  68. >
  69. Letters, numbers and dashes. Shoppers can
  70. type it in small or capital letters.
  71. </p>
  72. <InputError message={errors.code} />
  73. </div>
  74. <fieldset className="grid gap-3">
  75. <legend className="mb-1 text-sm font-medium">
  76. Discount
  77. </legend>
  78. <div className="flex flex-wrap gap-4 text-sm">
  79. <label className="flex items-center gap-2">
  80. <input
  81. type="radio"
  82. name="type"
  83. value="percent"
  84. checked={type === 'percent'}
  85. onChange={() => setType('percent')}
  86. />
  87. Percentage off
  88. </label>
  89. <label className="flex items-center gap-2">
  90. <input
  91. type="radio"
  92. name="type"
  93. value="fixed"
  94. checked={type === 'fixed'}
  95. onChange={() => setType('fixed')}
  96. />
  97. Fixed amount off
  98. </label>
  99. </div>
  100. <div className="grid gap-2 sm:max-w-xs">
  101. <Label htmlFor="value">
  102. {type === 'percent'
  103. ? 'Percentage (%)'
  104. : 'Amount (RM)'}
  105. </Label>
  106. <Input
  107. id="value"
  108. name="value"
  109. required
  110. inputMode={
  111. type === 'percent'
  112. ? 'numeric'
  113. : 'decimal'
  114. }
  115. defaultValue={code?.value}
  116. placeholder={
  117. type === 'percent' ? '10' : '5.00'
  118. }
  119. />
  120. <InputError message={errors.value} />
  121. </div>
  122. <InputError message={errors.type} />
  123. </fieldset>
  124. <div className="grid gap-2 sm:max-w-xs">
  125. <Label htmlFor="minimum_subtotal">
  126. Minimum spend on products (RM, optional)
  127. </Label>
  128. <Input
  129. id="minimum_subtotal"
  130. name="minimum_subtotal"
  131. inputMode="decimal"
  132. defaultValue={code?.minimum_subtotal ?? ''}
  133. placeholder="50.00"
  134. />
  135. <InputError message={errors.minimum_subtotal} />
  136. </div>
  137. <div className="grid gap-6 sm:grid-cols-2">
  138. <div className="grid gap-2">
  139. <Label htmlFor="starts_on">
  140. First day (optional)
  141. </Label>
  142. <Input
  143. id="starts_on"
  144. name="starts_on"
  145. type="date"
  146. defaultValue={code?.starts_on ?? ''}
  147. />
  148. <InputError message={errors.starts_on} />
  149. </div>
  150. <div className="grid gap-2">
  151. <Label htmlFor="ends_on">
  152. Last day (optional)
  153. </Label>
  154. <Input
  155. id="ends_on"
  156. name="ends_on"
  157. type="date"
  158. defaultValue={code?.ends_on ?? ''}
  159. />
  160. <InputError message={errors.ends_on} />
  161. </div>
  162. </div>
  163. <div className="grid gap-2 sm:max-w-xs">
  164. <Label htmlFor="usage_limit">
  165. Number of uses (optional)
  166. </Label>
  167. <Input
  168. id="usage_limit"
  169. name="usage_limit"
  170. type="number"
  171. min={1}
  172. step={1}
  173. defaultValue={code?.usage_limit ?? ''}
  174. placeholder="No limit"
  175. aria-describedby="usage-help"
  176. />
  177. <p
  178. id="usage-help"
  179. className="text-xs text-muted-foreground"
  180. >
  181. {code
  182. ? `Used ${code.times_used} times so far. `
  183. : ''}
  184. A use is counted when an order with the code
  185. is paid.
  186. </p>
  187. <InputError message={errors.usage_limit} />
  188. </div>
  189. <div className="flex items-center gap-3">
  190. <input type="hidden" name="active" value="0" />
  191. <Checkbox
  192. id="active"
  193. name="active"
  194. value="1"
  195. defaultChecked={code?.active ?? true}
  196. />
  197. <Label htmlFor="active">
  198. Active (shoppers can use it)
  199. </Label>
  200. </div>
  201. <div className="flex items-center gap-3">
  202. <Button
  203. disabled={processing}
  204. data-test="save-discount-button"
  205. >
  206. {code ? 'Save code' : 'Create code'}
  207. </Button>
  208. <Button variant="ghost" asChild>
  209. <Link
  210. href={DiscountCodeController.index.url(
  211. store.slug,
  212. )}
  213. >
  214. Cancel
  215. </Link>
  216. </Button>
  217. </div>
  218. </>
  219. )}
  220. </Form>
  221. {code && (
  222. <div className="flex flex-wrap items-center justify-between gap-3 rounded-xl border border-destructive/30 p-4">
  223. <div>
  224. <p className="font-medium">Delete this code</p>
  225. <p className="text-sm text-muted-foreground">
  226. Shoppers can no longer use it. Past orders keep
  227. their discount.
  228. </p>
  229. </div>
  230. <Dialog>
  231. <DialogTrigger asChild>
  232. <Button variant="destructive" size="sm">
  233. Delete
  234. </Button>
  235. </DialogTrigger>
  236. <DialogContent>
  237. <DialogHeader>
  238. <DialogTitle>
  239. Delete {code.code}?
  240. </DialogTitle>
  241. <DialogDescription>
  242. This cannot be undone. Past orders that
  243. used it are not changed.
  244. </DialogDescription>
  245. </DialogHeader>
  246. <DialogFooter>
  247. <DialogClose asChild>
  248. <Button variant="outline">
  249. Cancel
  250. </Button>
  251. </DialogClose>
  252. <Form
  253. {...DiscountCodeController.destroy.form(
  254. {
  255. slug: store.slug,
  256. discount: code.id,
  257. },
  258. )}
  259. >
  260. {({ processing }) => (
  261. <Button
  262. variant="destructive"
  263. disabled={processing}
  264. >
  265. Delete code
  266. </Button>
  267. )}
  268. </Form>
  269. </DialogFooter>
  270. </DialogContent>
  271. </Dialog>
  272. </div>
  273. )}
  274. </div>
  275. </>
  276. );
  277. }
  278. DiscountForm.layout = {
  279. breadcrumbs: [
  280. { title: 'Dashboard', href: dashboard() },
  281. { title: 'Discount code', href: '#' },
  282. ],
  283. };