import { useEffect, useMemo, useState } from "react"; const API_BASE = import.meta.env.VITE_API_BASE || ""; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { useAuth } from "@/contexts/AuthContext"; import { useToast } from "@/components/ui/use-toast"; export default function LeadForm() { const { user, profile } = useAuth(); const { toast } = useToast(); const [loading, setLoading] = useState(false); const [form, setForm] = useState({ name: "", email: "", company: "", website: "", budget: "", timeline: "", message: "", source: "wix", }); const inferred = useMemo(() => { const full = (profile?.full_name || "").trim(); const email = (user?.email || profile?.email || "").trim(); return { full, email }; }, [user, profile]); useEffect(() => { setForm((f) => ({ ...f, name: f.name || inferred.full, email: f.email || inferred.email, })); }, [inferred.full, inferred.email]); const update = (k: keyof typeof form) => (e: any) => setForm({ ...form, [k]: e.target.value }); const submit = async () => { if (!form.email) { toast({ title: "Email required", description: "Please provide a valid email.", }); return; } setLoading(true); try { const r = await fetch(`${API_BASE}/api/leads`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(form), }); const ok = r.ok; const data = await r.json().catch(() => ({})); if (!ok) throw new Error(data?.error || `Request failed (${r.status})`); toast({ title: "Thanks!", description: "We’ll follow up shortly with next steps.", }); setForm({ ...form, message: "" }); } catch (e: any) { toast({ title: "Submission failed", description: e?.message || "Try again later.", variant: "destructive" as any, }); } finally { setLoading(false); } }; return (
Start a Wix project Tell us a bit about your goals. We’ll get back within 1 business day.