| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- import { Form, Head, Link } from '@inertiajs/react';
- import { useState } from 'react';
- import AutomaticDiscountController from '@/actions/App/Http/Controllers/AutomaticDiscountController';
- 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 { AutomaticDiscount, StoreSummary } from '@/types';
-
- /** Add an automatic discount, or edit one when `offer` is given. */
- export default function AutomaticDiscountForm({
- store,
- offer,
- products,
- }: {
- store: StoreSummary;
- offer: AutomaticDiscount | null;
- products: { id: number; name: string }[];
- }) {
- const [type, setType] = useState<'percent' | 'fixed'>(
- offer?.type ?? 'percent',
- );
- const [appliesTo, setAppliesTo] = useState<'all' | 'chosen'>(
- offer?.product_ids ? 'chosen' : 'all',
- );
- const action = offer
- ? AutomaticDiscountController.update.form({
- slug: store.slug,
- offer: offer.id,
- })
- : AutomaticDiscountController.store.form(store.slug);
-
- return (
- <>
- <Head
- title={`${offer ? offer.name : 'New automatic discount'} - ${store.name}`}
- />
-
- <div className="mx-auto w-full max-w-2xl space-y-8 p-4">
- <Heading
- title={
- offer
- ? 'Edit automatic discount'
- : 'New automatic discount'
- }
- description="No code needed. It applies by itself when a cart qualifies."
- />
-
- <Form {...action} className="space-y-6">
- {({ processing, errors }) => (
- <>
- <div className="grid gap-2">
- <Label htmlFor="name">Name</Label>
- <Input
- id="name"
- name="name"
- required
- maxLength={40}
- defaultValue={offer?.name}
- placeholder="Raya Sale"
- aria-describedby="name-help"
- />
- <p
- id="name-help"
- className="text-xs text-muted-foreground"
- >
- Shoppers see it on product pages, in the
- cart and on their order.
- </p>
- <InputError message={errors.name} />
- </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={offer?.value}
- placeholder={
- type === 'percent' ? '10' : '5.00'
- }
- />
- <InputError message={errors.value} />
- </div>
- <InputError message={errors.type} />
- </fieldset>
-
- <fieldset className="grid gap-3">
- <legend className="mb-1 text-sm font-medium">
- When it applies (leave both empty for a sale
- on every cart)
- </legend>
- <div className="grid gap-6 sm:grid-cols-2">
- <div className="grid gap-2">
- <Label htmlFor="minimum_quantity">
- Buy at least (items)
- </Label>
- <Input
- id="minimum_quantity"
- name="minimum_quantity"
- type="number"
- min={1}
- step={1}
- defaultValue={
- offer?.minimum_quantity ?? ''
- }
- placeholder="2"
- />
- <InputError
- message={errors.minimum_quantity}
- />
- </div>
- <div className="grid gap-2">
- <Label htmlFor="minimum_subtotal">
- Spend at least (RM)
- </Label>
- <Input
- id="minimum_subtotal"
- name="minimum_subtotal"
- inputMode="decimal"
- defaultValue={
- offer?.minimum_subtotal ?? ''
- }
- placeholder="100.00"
- />
- <InputError
- message={errors.minimum_subtotal}
- />
- </div>
- </div>
- </fieldset>
-
- <fieldset className="grid gap-3">
- <legend className="mb-1 text-sm font-medium">
- Products
- </legend>
- <div className="flex flex-wrap gap-4 text-sm">
- <label className="flex items-center gap-2">
- <input
- type="radio"
- name="applies_to"
- value="all"
- checked={appliesTo === 'all'}
- onChange={() => setAppliesTo('all')}
- />
- All products
- </label>
- <label className="flex items-center gap-2">
- <input
- type="radio"
- name="applies_to"
- value="chosen"
- checked={appliesTo === 'chosen'}
- onChange={() =>
- setAppliesTo('chosen')
- }
- />
- Only some products
- </label>
- </div>
- {appliesTo === 'chosen' && (
- <div className="space-y-2 rounded-md border p-3">
- <p className="text-xs text-muted-foreground">
- Only these count towards the
- minimum, and only these get cheaper.
- </p>
- <ul className="grid gap-2 sm:grid-cols-2">
- {products.map((product) => (
- <li
- key={product.id}
- className="flex items-center gap-2 text-sm"
- >
- <Checkbox
- id={`product-${product.id}`}
- name="product_ids[]"
- value={String(
- product.id,
- )}
- defaultChecked={
- offer?.product_ids?.includes(
- product.id,
- ) ?? false
- }
- />
- <Label
- htmlFor={`product-${product.id}`}
- className="font-normal"
- >
- {product.name}
- </Label>
- </li>
- ))}
- </ul>
- </div>
- )}
- <InputError
- message={
- errors.product_ids ??
- Object.entries(errors).find(([key]) =>
- key.startsWith('product_ids.'),
- )?.[1]
- }
- />
- </fieldset>
-
- <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={offer?.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={offer?.ends_on ?? ''}
- />
- <InputError message={errors.ends_on} />
- </div>
- </div>
-
- <div className="flex items-center gap-3">
- <input type="hidden" name="active" value="0" />
- <Checkbox
- id="active"
- name="active"
- value="1"
- defaultChecked={offer?.active ?? true}
- />
- <Label htmlFor="active">
- Active (applies to carts now)
- </Label>
- </div>
-
- <p className="text-sm text-muted-foreground">
- Only one discount applies per order. If a
- shopper also types a code, they get whichever
- saves more.
- </p>
-
- <div className="flex items-center gap-3">
- <Button
- disabled={processing}
- data-test="save-automatic-discount-button"
- >
- {offer ? 'Save' : 'Create'}
- </Button>
- <Button variant="ghost" asChild>
- <Link
- href={DiscountCodeController.index.url(
- store.slug,
- )}
- >
- Cancel
- </Link>
- </Button>
- </div>
- </>
- )}
- </Form>
-
- {offer && (
- <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 automatic discount
- </p>
- <p className="text-sm text-muted-foreground">
- Carts stop getting it. Past orders keep their
- discount.
- </p>
- </div>
- <Dialog>
- <DialogTrigger asChild>
- <Button variant="destructive" size="sm">
- Delete
- </Button>
- </DialogTrigger>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>
- Delete {offer.name}?
- </DialogTitle>
- <DialogDescription>
- This cannot be undone. Past orders are
- not changed.
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <DialogClose asChild>
- <Button variant="outline">
- Cancel
- </Button>
- </DialogClose>
- <Form
- {...AutomaticDiscountController.destroy.form(
- {
- slug: store.slug,
- offer: offer.id,
- },
- )}
- >
- {({ processing }) => (
- <Button
- variant="destructive"
- disabled={processing}
- >
- Delete
- </Button>
- )}
- </Form>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- </div>
- )}
- </div>
- </>
- );
- }
-
- AutomaticDiscountForm.layout = {
- breadcrumbs: [
- { title: 'Dashboard', href: dashboard() },
- { title: 'Automatic discount', href: '#' },
- ],
- };
|