|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { Send, CheckCircle2, Loader2 } from "lucide-react"; |
| 5 | + |
| 6 | +type Step = "idle" | "submitting" | "success" | "error"; |
| 7 | + |
| 8 | +export function FeedbackForm() { |
| 9 | + const [step, setStep] = useState<Step>("idle"); |
| 10 | + const [message, setMessage] = useState(""); |
| 11 | + const [name, setName] = useState(""); |
| 12 | + const [email, setEmail] = useState(""); |
| 13 | + const [errorMsg, setErrorMsg] = useState(""); |
| 14 | + |
| 15 | + const handleSubmit = async (e: React.FormEvent) => { |
| 16 | + e.preventDefault(); |
| 17 | + if (message.trim().length < 3) return; |
| 18 | + |
| 19 | + setStep("submitting"); |
| 20 | + setErrorMsg(""); |
| 21 | + |
| 22 | + try { |
| 23 | + const res = await fetch("/api/feedback", { |
| 24 | + method: "POST", |
| 25 | + headers: { "Content-Type": "application/json" }, |
| 26 | + body: JSON.stringify({ |
| 27 | + message: message.trim(), |
| 28 | + name: name.trim() || undefined, |
| 29 | + email: email.trim() || undefined, |
| 30 | + }), |
| 31 | + }); |
| 32 | + const data = await res.json(); |
| 33 | + if (!res.ok) { |
| 34 | + setStep("error"); |
| 35 | + setErrorMsg(data.error || "Something went wrong. Please try again."); |
| 36 | + return; |
| 37 | + } |
| 38 | + setStep("success"); |
| 39 | + } catch { |
| 40 | + setStep("error"); |
| 41 | + setErrorMsg("Something went wrong. Please try again."); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + if (step === "success") { |
| 46 | + return ( |
| 47 | + <div className="flex items-center justify-center gap-3 rounded-xl border border-teal-100 bg-teal-50/50 py-6"> |
| 48 | + <CheckCircle2 className="w-5 h-5 text-teal-600 shrink-0" /> |
| 49 | + <span className="text-teal-700 font-medium"> |
| 50 | + Thank you — every note helps us make this better. |
| 51 | + </span> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <form onSubmit={handleSubmit} className="space-y-3"> |
| 58 | + <textarea |
| 59 | + required |
| 60 | + rows={5} |
| 61 | + placeholder="What's working, what's not, what you'd love to see…" |
| 62 | + value={message} |
| 63 | + onChange={(e) => setMessage(e.target.value)} |
| 64 | + disabled={step === "submitting"} |
| 65 | + className="w-full rounded-xl border border-gray-200 bg-white px-4 py-3 text-sm text-gray-900 placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-teal-500 focus:border-transparent disabled:opacity-60 resize-none" |
| 66 | + /> |
| 67 | + <div className="grid grid-cols-1 sm:grid-cols-2 gap-3"> |
| 68 | + <input |
| 69 | + type="text" |
| 70 | + placeholder="Your name (optional)" |
| 71 | + value={name} |
| 72 | + onChange={(e) => setName(e.target.value)} |
| 73 | + disabled={step === "submitting"} |
| 74 | + className="w-full rounded-xl border border-gray-200 bg-white px-4 py-3 text-sm text-gray-900 placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-teal-500 focus:border-transparent disabled:opacity-60" |
| 75 | + /> |
| 76 | + <input |
| 77 | + type="email" |
| 78 | + placeholder="Email (optional, for a reply)" |
| 79 | + value={email} |
| 80 | + onChange={(e) => setEmail(e.target.value)} |
| 81 | + disabled={step === "submitting"} |
| 82 | + className="w-full rounded-xl border border-gray-200 bg-white px-4 py-3 text-sm text-gray-900 placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-teal-500 focus:border-transparent disabled:opacity-60" |
| 83 | + /> |
| 84 | + </div> |
| 85 | + <button |
| 86 | + type="submit" |
| 87 | + disabled={step === "submitting" || message.trim().length < 3} |
| 88 | + className="w-full inline-flex items-center justify-center gap-2 rounded-xl bg-teal-600 px-6 py-3 text-sm font-semibold text-white hover:bg-teal-700 transition-colors disabled:opacity-60" |
| 89 | + > |
| 90 | + {step === "submitting" ? ( |
| 91 | + <> |
| 92 | + <Loader2 className="w-4 h-4 animate-spin" /> |
| 93 | + Sending… |
| 94 | + </> |
| 95 | + ) : ( |
| 96 | + <> |
| 97 | + Send feedback |
| 98 | + <Send className="w-4 h-4" /> |
| 99 | + </> |
| 100 | + )} |
| 101 | + </button> |
| 102 | + {step === "error" && ( |
| 103 | + <p className="text-sm text-red-600 text-center">{errorMsg}</p> |
| 104 | + )} |
| 105 | + <p className="text-xs text-gray-400 text-center pt-1"> |
| 106 | + No account needed. Email is optional and only used to follow up. |
| 107 | + </p> |
| 108 | + </form> |
| 109 | + ); |
| 110 | +} |
0 commit comments