|
| 1 | +import { useState } from "react"; |
| 2 | + |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { Input } from "@/components/ui/input"; |
| 5 | +import { Textarea } from "@/components/ui/textarea"; |
| 6 | +import api from "@/lib/api"; |
| 7 | + |
| 8 | +interface ContactFormData { |
| 9 | + name: string; |
| 10 | + email: string; |
| 11 | + message: string; |
| 12 | +} |
| 13 | + |
| 14 | +type SubmitStatus = "idle" | "submitting" | "success" | "error"; |
| 15 | + |
| 16 | +const initialForm: ContactFormData = { name: "", email: "", message: "" }; |
| 17 | + |
| 18 | +export default function ContactForm() { |
| 19 | + const [form, setForm] = useState<ContactFormData>(initialForm); |
| 20 | + const [status, setStatus] = useState<SubmitStatus>("idle"); |
| 21 | + |
| 22 | + const handleChange = |
| 23 | + (field: keyof ContactFormData) => |
| 24 | + (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 25 | + setForm((prev) => ({ ...prev, [field]: e.target.value })); |
| 26 | + }; |
| 27 | + |
| 28 | + const handleSubmit = async (e: React.FormEvent) => { |
| 29 | + e.preventDefault(); |
| 30 | + setStatus("submitting"); |
| 31 | + try { |
| 32 | + await api.post("/contact/contact/", form); |
| 33 | + setStatus("success"); |
| 34 | + setForm(initialForm); |
| 35 | + } catch { |
| 36 | + setStatus("error"); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <form onSubmit={handleSubmit} className="flex flex-col gap-4"> |
| 42 | + <div className="grid grid-cols-1 gap-4 md:grid-cols-2"> |
| 43 | + <div> |
| 44 | + <label htmlFor="name" className="mb-1 block text-sm font-medium"> |
| 45 | + Name |
| 46 | + </label> |
| 47 | + <Input |
| 48 | + id="name" |
| 49 | + required |
| 50 | + value={form.name} |
| 51 | + onChange={handleChange("name")} |
| 52 | + /> |
| 53 | + </div> |
| 54 | + <div> |
| 55 | + <label htmlFor="email" className="mb-1 block text-sm font-medium"> |
| 56 | + Email |
| 57 | + </label> |
| 58 | + <Input |
| 59 | + id="email" |
| 60 | + type="email" |
| 61 | + required |
| 62 | + value={form.email} |
| 63 | + onChange={handleChange("email")} |
| 64 | + /> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + <div> |
| 68 | + <label htmlFor="message" className="mb-1 block text-sm font-medium"> |
| 69 | + Questions or Comments |
| 70 | + </label> |
| 71 | + <Textarea |
| 72 | + id="message" |
| 73 | + required |
| 74 | + rows={8} |
| 75 | + value={form.message} |
| 76 | + onChange={handleChange("message")} |
| 77 | + /> |
| 78 | + </div> |
| 79 | + {status === "error" && ( |
| 80 | + <p className="text-sm text-red-600"> |
| 81 | + Something went wrong — please try again. |
| 82 | + </p> |
| 83 | + )} |
| 84 | + {status === "success" && ( |
| 85 | + <p className="text-sm text-green-700"> |
| 86 | + Thanks — we'll get back to you soon. |
| 87 | + </p> |
| 88 | + )} |
| 89 | + <Button |
| 90 | + type="submit" |
| 91 | + disabled={status === "submitting"} |
| 92 | + className="self-end bg-[#dd42e4] text-white hover:bg-[#dd42e4]/90" |
| 93 | + > |
| 94 | + {status === "submitting" ? "Sending..." : "Submit"} |
| 95 | + </Button> |
| 96 | + </form> |
| 97 | + ); |
| 98 | +} |
0 commit comments