| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import { Form, Head, Link } from '@inertiajs/react';
- import { useState } from 'react';
- import DiscountCodeController from '@/actions/App/Http/Controllers/DiscountCodeController';
- 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 { DiscountCode, StoreSummary } from '@/types';
-
- /** Add a new discount code, or edit one when `code` is given. */
- export default function DiscountForm({
- store,
- code,
- }: {
- store: StoreSummary;
- code: DiscountCode | null;
- }) {
- const [type, setType] = useState<'percent' | 'fixed'>(
- code?.type ?? 'percent',
- );
- const action = code
- ? DiscountCodeController.update.form({
- slug: store.slug,
- discount: code.id,
- })
- : DiscountCodeController.store.form(store.slug);
-
- return (
- <>
- <Head
- title={`${code ? code.code : 'New discount code'} - ${store.name}`}
- />
-
- <div className="mx-auto w-full max-w-2xl space-y-8 p-4">
- <Heading
- title={code ? 'Edit discount code' : 'New discount code'}
- description={`For ${store.name}. Shoppers type the code in their cart.`}
- />
-
- <Form {...action} className="space-y-6">
- {({ processing, errors }) => (
- <>
- <div className="grid gap-2">
- <Label htmlFor="code">Code</Label>
- <Input
- id="code"
- name="code"
- required
- maxLength={30}
- defaultValue={code?.code}
- placeholder="RAYA10"
- className="font-mono uppercase"
- autoCapitalize="characters"
- aria-describedby="code-help"
- />
- <p
- id="code-help"
- className="text-xs text-muted-foreground"
- >
- Letters, numbers and dashes. Shoppers can
- type it in small or capital letters.
- </p>
- <InputError message={errors.code} />
- </div>
-
- <fieldset className="grid gap-3">
- <legend className="mb-1 text-sm font-medium">
- Discount
- </legend>
- <div className="flex flex-wrap gap-4 text-sm">
- <label className="flex items-center gap-2">
- <input
- type="radio"
- name="type"
- value="percent"
- checked={type === 'percent'}
- onChange={() => setType('percent')}
- />
- Percentage off
- </label>
- <label className="flex items-center gap-2">
- <input
- type="radio"
- name="type"
- value="fixed"
- checked={type === 'fixed'}
- onChange={() => setType('fixed')}
- />
- Fixed amount off
- </label>
- </div>
- <div className="grid gap-2 sm:max-w-xs">
- <Label htmlFor="value">
- {type === 'percent'
- ? 'Percentage (%)'
- : 'Amount (RM)'}
- </Label>
- <Input
- id="value"
- name="value"
- required
- inputMode={
- type === 'percent'
- ? 'numeric'
- : 'decimal'
- }
- defaultValue={code?.value}
- placeholder={
- type === 'percent' ? '10' : '5.00'
- }
- />
- <InputError message={errors.value} />
- </div>
- <InputError message={errors.type} />
- </fieldset>
-
- <div className="grid gap-2 sm:max-w-xs">
- <Label htmlFor="minimum_subtotal">
- Minimum spend on products (RM, optional)
- </Label>
- <Input
- id="minimum_subtotal"
- name="minimum_subtotal"
- inputMode="decimal"
- defaultValue={code?.minimum_subtotal ?? ''}
- placeholder="50.00"
- />
- <InputError message={errors.minimum_subtotal} />
- </div>
-
- <div className="grid gap-6 sm:grid-cols-2">
- <div className="grid gap-2">
- <Label htmlFor="starts_on">
- First day (optional)
- </Label>
- <Input
- id="starts_on"
- name="starts_on"
- type="date"
- defaultValue={code?.starts_on ?? ''}
- />
- <InputError message={errors.starts_on} />
- </div>
- <div className="grid gap-2">
- <Label htmlFor="ends_on">
- Last day (optional)
- </Label>
- <Input
- id="ends_on"
- name="ends_on"
- type="date"
- defaultValue={code?.ends_on ?? ''}
- />
- <InputError message={errors.ends_on} />
- </div>
- </div>
-
- <div className="grid gap-2 sm:max-w-xs">
- <Label htmlFor="usage_limit">
- Number of uses (optional)
- </Label>
- <Input
- id="usage_limit"
- name="usage_limit"
- type="number"
- min={1}
- step={1}
- defaultValue={code?.usage_limit ?? ''}
- placeholder="No limit"
- aria-describedby="usage-help"
- />
- <p
- id="usage-help"
- className="text-xs text-muted-foreground"
- >
- {code
- ? `Used ${code.times_used} times so far. `
- : ''}
- A use is counted when an order with the code
- is paid.
- </p>
- <InputError message={errors.usage_limit} />
- </div>
-
- <div className="flex items-center gap-3">
- <input type="hidden" name="active" value="0" />
- <Checkbox
- id="active"
- name="active"
- value="1"
- defaultChecked={code?.active ?? true}
- />
- <Label htmlFor="active">
- Active (shoppers can use it)
- </Label>
- </div>
-
- <div className="flex items-center gap-3">
- <Button
- disabled={processing}
- data-test="save-discount-button"
- >
- {code ? 'Save code' : 'Create code'}
- </Button>
- <Button variant="ghost" asChild>
- <Link
- href={DiscountCodeController.index.url(
- store.slug,
- )}
- >
- Cancel
- </Link>
- </Button>
- </div>
- </>
- )}
- </Form>
-
- {code && (
- <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 code</p>
- <p className="text-sm text-muted-foreground">
- Shoppers can no longer use it. Past orders keep
- their discount.
- </p>
- </div>
- <Dialog>
- <DialogTrigger asChild>
- <Button variant="destructive" size="sm">
- Delete
- </Button>
- </DialogTrigger>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>
- Delete {code.code}?
- </DialogTitle>
- <DialogDescription>
- This cannot be undone. Past orders that
- used it are not changed.
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <DialogClose asChild>
- <Button variant="outline">
- Cancel
- </Button>
- </DialogClose>
- <Form
- {...DiscountCodeController.destroy.form(
- {
- slug: store.slug,
- discount: code.id,
- },
- )}
- >
- {({ processing }) => (
- <Button
- variant="destructive"
- disabled={processing}
- >
- Delete code
- </Button>
- )}
- </Form>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- </div>
- )}
- </div>
- </>
- );
- }
-
- DiscountForm.layout = {
- breadcrumbs: [
- { title: 'Dashboard', href: dashboard() },
- { title: 'Discount code', href: '#' },
- ],
- };
|