|
| 1 | +'use client' |
| 2 | +import { getFormProps, useForm } from '@conform-to/react' |
| 3 | +import { getZodConstraint, parseWithZod } from '@conform-to/zod' |
| 4 | +import { CheckCircle2, CircleAlert } from 'lucide-react' |
| 5 | +import { Input, Textarea } from '@/components/form' |
| 6 | +import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert' |
| 7 | +import { Button } from '@/components/ui/button' |
| 8 | +import { Label } from '@/components/ui/label' |
| 9 | +import { contactFormSchema } from '@/integration/forms/ContactForm/schema' |
| 10 | +import { useActionState } from 'react' |
| 11 | +import { subitContactFormAction } from '@/integration/forms/ContactForm/action' |
| 12 | + |
| 13 | +export const ContactForm = () => { |
| 14 | + const [state, action] = useActionState(subitContactFormAction, undefined) |
| 15 | + const [form, fields] = useForm({ |
| 16 | + id: 'contact-form', |
| 17 | + lastResult: state?.reply, |
| 18 | + constraint: getZodConstraint(contactFormSchema), |
| 19 | + onValidate({ formData }) { |
| 20 | + return parseWithZod(formData, { schema: contactFormSchema }) |
| 21 | + }, |
| 22 | + shouldValidate: 'onBlur', |
| 23 | + }) |
| 24 | + |
| 25 | + return ( |
| 26 | + <div className="flex items-center justify-center"> |
| 27 | + <div className="container mx-auto max-w-xl"> |
| 28 | + {state?.reply.status === 'success' ? ( |
| 29 | + <div className="space-y-4"> |
| 30 | + <Alert> |
| 31 | + <CheckCircle2 className="stroke-green-500" /> |
| 32 | + <AlertTitle |
| 33 | + className="text-lg" |
| 34 | + dangerouslySetInnerHTML={{ |
| 35 | + __html: '<h2>Success!</h2>', |
| 36 | + }} |
| 37 | + ></AlertTitle> |
| 38 | + <AlertDescription |
| 39 | + dangerouslySetInnerHTML={{ |
| 40 | + __html: '<p>Your message has been sent successfully.</p>', |
| 41 | + }} |
| 42 | + ></AlertDescription> |
| 43 | + </Alert> |
| 44 | + </div> |
| 45 | + ) : ( |
| 46 | + <form |
| 47 | + {...getFormProps(form)} |
| 48 | + method="POST" |
| 49 | + action={action} |
| 50 | + className="space-y-4" |
| 51 | + > |
| 52 | + {form.errors && ( |
| 53 | + <div className="space-y-4"> |
| 54 | + <Alert> |
| 55 | + <CircleAlert className="stroke-red-500" /> |
| 56 | + <AlertTitle className="text-lg">Form Error!</AlertTitle> |
| 57 | + <AlertDescription>{form.errors}</AlertDescription> |
| 58 | + </Alert> |
| 59 | + </div> |
| 60 | + )} |
| 61 | + <div className="space-y-2"> |
| 62 | + <Label htmlFor={fields.name.id}>Name</Label> |
| 63 | + <Input |
| 64 | + meta={fields.name} |
| 65 | + type="text" |
| 66 | + placeholder="Enter your name" |
| 67 | + /> |
| 68 | + {fields.name.errors && ( |
| 69 | + <p className="text-sm text-red-500">{fields.name.errors}</p> |
| 70 | + )} |
| 71 | + </div> |
| 72 | + |
| 73 | + <div className="space-y-2"> |
| 74 | + <Label htmlFor={fields.email.id}>Email</Label> |
| 75 | + <Input |
| 76 | + meta={fields.email} |
| 77 | + type="email" |
| 78 | + placeholder="Enter your email" |
| 79 | + /> |
| 80 | + {fields.email.errors && ( |
| 81 | + <p className="text-sm text-red-500">{fields.email.errors}</p> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + |
| 85 | + <div className="space-y-2"> |
| 86 | + <Label htmlFor={fields.message.id}>Message</Label> |
| 87 | + <Textarea |
| 88 | + meta={fields.message} |
| 89 | + placeholder="Enter your message" |
| 90 | + className="min-h-[120px]" |
| 91 | + /> |
| 92 | + {fields.message.errors && ( |
| 93 | + <p className="text-sm text-red-500">{fields.message.errors}</p> |
| 94 | + )} |
| 95 | + </div> |
| 96 | + <Button type="submit" className="w-full"> |
| 97 | + Submit |
| 98 | + </Button> |
| 99 | + </form> |
| 100 | + )} |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + ) |
| 104 | +} |
0 commit comments