|
1 | | -import { Dispatch, SetStateAction } from "react"; |
2 | | -import { Puppy } from "../types"; |
3 | | -import { useFormStatus } from "react-dom"; |
4 | | -import { createPuppy } from "../queries"; |
5 | | -import { ErrorBoundary } from "react-error-boundary"; |
| 1 | +import { useForm } from '@inertiajs/react'; |
| 2 | +import { useRef } from 'react'; |
| 3 | +import { useFormStatus } from 'react-dom'; |
6 | 4 |
|
7 | | -export function NewPuppyForm({ |
8 | | - puppies, |
9 | | - setPuppies, |
10 | | -}: { |
11 | | - puppies: Puppy[]; |
12 | | - setPuppies: Dispatch<SetStateAction<Puppy[]>>; |
13 | | -}) { |
14 | | - return ( |
15 | | - <div className="mt-12 flex items-center justify-between bg-white p-8 shadow ring ring-black/5"> |
16 | | - <ErrorBoundary |
17 | | - fallbackRender={({ error }) => ( |
18 | | - <pre>{JSON.stringify(error, null, 2)}</pre> |
19 | | - )} |
20 | | - > |
21 | | - <form |
22 | | - action={async (formData: FormData) => { |
23 | | - const response = await createPuppy(formData); |
24 | | - if (response.data) { |
25 | | - setPuppies([...puppies, response.data]); |
26 | | - } |
27 | | - }} |
28 | | - className="mt-4 flex w-full flex-col items-start gap-4" |
29 | | - > |
30 | | - <div className="grid w-full gap-6 md:grid-cols-3"> |
31 | | - <fieldset className="flex w-full flex-col gap-1"> |
32 | | - <label htmlFor="name">Name</label> |
33 | | - <input |
34 | | - required |
35 | | - className="max-w-96 rounded-sm bg-white px-2 py-1 ring ring-black/20 focus:ring-2 focus:ring-cyan-500 focus:outline-none" |
36 | | - id="name" |
37 | | - type="text" |
38 | | - name="name" |
39 | | - /> |
40 | | - </fieldset> |
41 | | - <fieldset className="flex w-full flex-col gap-1"> |
42 | | - <label htmlFor="trait">Personality trait</label> |
43 | | - <input |
44 | | - required |
45 | | - className="max-w-96 rounded-sm bg-white px-2 py-1 ring ring-black/20 focus:ring-2 focus:ring-cyan-500 focus:outline-none" |
46 | | - id="trait" |
47 | | - type="text" |
48 | | - name="trait" |
49 | | - /> |
50 | | - </fieldset> |
51 | | - <fieldset className="col-span-2 flex w-full flex-col gap-1"> |
52 | | - <label htmlFor="image_url">Profile pic</label> |
53 | | - <input |
54 | | - className="max-w-96 rounded-sm bg-white px-2 py-1 ring ring-black/20 focus:ring-2 focus:ring-cyan-500 focus:outline-none" |
55 | | - id="image_url" |
56 | | - type="file" |
57 | | - name="image_url" |
58 | | - /> |
59 | | - </fieldset> |
60 | | - </div> |
61 | | - <SubmitButton /> |
62 | | - </form> |
63 | | - </ErrorBoundary> |
64 | | - </div> |
65 | | - ); |
| 5 | +export function NewPuppyForm() { |
| 6 | + const { post, setData, data, errors, reset } = useForm({ |
| 7 | + name: '', |
| 8 | + trait: '', |
| 9 | + image: null as File | null, |
| 10 | + }); |
| 11 | + const fileInputRef = useRef<HTMLInputElement>(null); |
| 12 | + |
| 13 | + return ( |
| 14 | + <> |
| 15 | + <div className="mt-12 flex items-center justify-between bg-white p-8 shadow ring ring-black/5"> |
| 16 | + <form |
| 17 | + onSubmit={(e) => { |
| 18 | + e.preventDefault(); |
| 19 | + post(route('puppies.store'), { |
| 20 | + preserveScroll: true, |
| 21 | + onSuccess: () => { |
| 22 | + reset(); |
| 23 | + if (fileInputRef.current) { |
| 24 | + fileInputRef.current.value = ''; |
| 25 | + } |
| 26 | + }, |
| 27 | + }); |
| 28 | + }} |
| 29 | + className="mt-4 flex w-full flex-col items-start gap-4" |
| 30 | + > |
| 31 | + <div className="grid w-full gap-6 md:grid-cols-3"> |
| 32 | + <fieldset className="flex w-full flex-col gap-1"> |
| 33 | + <label htmlFor="name">Name</label> |
| 34 | + <input |
| 35 | + value={data.name} |
| 36 | + className="max-w-96 rounded-sm bg-white px-2 py-1 ring ring-black/20 focus:ring-2 focus:ring-cyan-500 focus:outline-none" |
| 37 | + id="name" |
| 38 | + type="text" |
| 39 | + name="name" |
| 40 | + onChange={(e) => setData('name', e.target.value)} |
| 41 | + /> |
| 42 | + {errors.name && <p className="mt-1 text-xs text-red-500">{errors.name}</p>} |
| 43 | + </fieldset> |
| 44 | + <fieldset className="flex w-full flex-col gap-1"> |
| 45 | + <label htmlFor="trait">Personality trait</label> |
| 46 | + <input |
| 47 | + value={data.trait} |
| 48 | + className="max-w-96 rounded-sm bg-white px-2 py-1 ring ring-black/20 focus:ring-2 focus:ring-cyan-500 focus:outline-none" |
| 49 | + id="trait" |
| 50 | + type="text" |
| 51 | + name="trait" |
| 52 | + onChange={(e) => setData('trait', e.target.value)} |
| 53 | + /> |
| 54 | + {errors.trait && <p className="mt-1 text-xs text-red-500">{errors.trait}</p>} |
| 55 | + </fieldset> |
| 56 | + <fieldset className="col-span-2 flex w-full flex-col gap-1"> |
| 57 | + <label htmlFor="image">Profile pic</label> |
| 58 | + <input |
| 59 | + ref={fileInputRef} |
| 60 | + className="max-w-96 rounded-sm bg-white px-2 py-1 ring ring-black/20 focus:ring-2 focus:ring-cyan-500 focus:outline-none" |
| 61 | + id="image" |
| 62 | + type="file" |
| 63 | + name="image" |
| 64 | + onChange={(e) => setData('image', e.target.files ? e.target.files[0] : null)} |
| 65 | + /> |
| 66 | + {errors.image && <p className="mt-1 text-xs text-red-500">{errors.image}</p>} |
| 67 | + </fieldset> |
| 68 | + </div> |
| 69 | + <SubmitButton /> |
| 70 | + </form> |
| 71 | + </div> |
| 72 | + </> |
| 73 | + ); |
66 | 74 | } |
67 | 75 |
|
68 | 76 | function SubmitButton() { |
69 | | - const status = useFormStatus(); |
70 | | - return ( |
71 | | - <button |
72 | | - className="mt-4 inline-block rounded bg-cyan-300 px-4 py-2 font-medium text-cyan-900 hover:bg-cyan-200 focus:ring-2 focus:ring-cyan-500 focus:outline-none disabled:cursor-not-allowed disabled:bg-slate-200" |
73 | | - type="submit" |
74 | | - disabled={status.pending} |
75 | | - > |
76 | | - {status.pending |
77 | | - ? `Adding ${status?.data?.get("name") || "puppy"}...` |
78 | | - : "Add puppy"} |
79 | | - </button> |
80 | | - ); |
| 77 | + const status = useFormStatus(); |
| 78 | + return ( |
| 79 | + <button |
| 80 | + className="mt-4 inline-block rounded bg-cyan-300 px-4 py-2 font-medium text-cyan-900 hover:bg-cyan-200 focus:ring-2 focus:ring-cyan-500 focus:outline-none disabled:cursor-not-allowed disabled:bg-slate-200" |
| 81 | + type="submit" |
| 82 | + disabled={status.pending} |
| 83 | + > |
| 84 | + {status.pending ? `Adding ${status?.data?.get('name') || 'puppy'}...` : 'Add puppy'} |
| 85 | + </button> |
| 86 | + ); |
81 | 87 | } |
0 commit comments