|
| 1 | +import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; |
| 2 | +import { Puppy } from '@/types'; |
| 3 | +import { useForm } from '@inertiajs/react'; |
| 4 | +import clsx from 'clsx'; |
| 5 | +import { EditIcon, LoaderCircle } from 'lucide-react'; |
| 6 | +import { useState } from 'react'; |
| 7 | +import { Button } from './ui/button'; |
| 8 | +import { Input } from './ui/input'; |
| 9 | +import { Label } from './ui/label'; |
| 10 | + |
| 11 | +export function PuppyUpdate({ puppy }: { puppy: Puppy }) { |
| 12 | + const { data, setData, errors, put, processing } = useForm({ |
| 13 | + name: puppy.name, |
| 14 | + trait: puppy.trait, |
| 15 | + image: null as File | null, |
| 16 | + }); |
| 17 | + |
| 18 | + const [open, setOpen] = useState(false); |
| 19 | + |
| 20 | + return ( |
| 21 | + <Dialog open={open} onOpenChange={setOpen}> |
| 22 | + <DialogTrigger asChild> |
| 23 | + <Button className="group/update bg-background/30 hover:bg-background" size="icon" variant="secondary" aria-label="Update puppy"> |
| 24 | + <EditIcon className="size-4" /> |
| 25 | + </Button> |
| 26 | + </DialogTrigger> |
| 27 | + <DialogContent> |
| 28 | + <DialogTitle>Edit {puppy.name}</DialogTitle> |
| 29 | + <DialogDescription>Make changes to your puppy’s information below.</DialogDescription> |
| 30 | + <form |
| 31 | + className="space-y-6" |
| 32 | + onSubmit={(e) => { |
| 33 | + e.preventDefault(); |
| 34 | + // TODO |
| 35 | + put(route('puppies.update', puppy.id)); |
| 36 | + }} |
| 37 | + > |
| 38 | + <Label htmlFor="name">Name</Label> |
| 39 | + <Input |
| 40 | + id="name" |
| 41 | + className="mt-1 block w-full" |
| 42 | + value={data.name} |
| 43 | + onChange={(e) => setData('name', e.target.value)} |
| 44 | + required |
| 45 | + autoComplete="name" |
| 46 | + placeholder="Full name" |
| 47 | + /> |
| 48 | + {errors.name && <p className="mt-1 text-xs text-red-500">{errors.name}</p>} |
| 49 | + |
| 50 | + <Label htmlFor="trait">Personality trait</Label> |
| 51 | + <Input |
| 52 | + id="trait" |
| 53 | + className="mt-1 block w-full" |
| 54 | + value={data.trait} |
| 55 | + onChange={(e) => setData('trait', e.target.value)} |
| 56 | + required |
| 57 | + placeholder="Personality trait" |
| 58 | + /> |
| 59 | + |
| 60 | + {errors.trait && <p className="mt-1 text-xs text-red-500">{errors.trait}</p>} |
| 61 | + |
| 62 | + <Label htmlFor="image">Change image</Label> |
| 63 | + <Input |
| 64 | + id="image" |
| 65 | + type="file" |
| 66 | + className="mt-1 block w-full" |
| 67 | + onChange={(e) => setData('image', e.target.files ? e.target.files[0] : null)} |
| 68 | + placeholder="Profile picture" |
| 69 | + /> |
| 70 | + |
| 71 | + {errors.image && <p className="mt-1 text-xs text-red-500">{errors.image}</p>} |
| 72 | + |
| 73 | + <DialogFooter className="gap-2"> |
| 74 | + <DialogClose asChild> |
| 75 | + <Button variant="secondary">Cancel</Button> |
| 76 | + </DialogClose> |
| 77 | + |
| 78 | + <Button className="relative disabled:opacity-100" disabled={processing} type="submit"> |
| 79 | + {processing && ( |
| 80 | + <div className="absolute inset-0 grid place-items-center"> |
| 81 | + <LoaderCircle className="size-5 animate-spin stroke-primary-foreground" /> |
| 82 | + </div> |
| 83 | + )} |
| 84 | + <span className={clsx(processing && 'invisible')}>Update</span> |
| 85 | + </Button> |
| 86 | + </DialogFooter> |
| 87 | + </form> |
| 88 | + </DialogContent> |
| 89 | + </Dialog> |
| 90 | + ); |
| 91 | +} |
0 commit comments