"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import Link from "next/link" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Card, CardContent } from "@/components/ui/card" import { ArrowLeft } from "lucide-react" import { User } from "@/types" import { useNotifications } from "@/providers/notification-provider" import { LEAD_SOURCES, LEAD_STATUSES } from "@/lib/constants" const leadFormSchema = z.object({ companyName: z.string().min(1, "Company name is required"), contactName: z.string().min(1, "Contact name is required"), email: z.string().email("Invalid email address"), phone: z.string().optional(), source: z.string().optional(), description: z.string().optional(), status: z.string(), assignedUserId: z.string().optional(), }) type LeadFormValues = z.infer export default function CreateLeadPage() { const router = useRouter() const { addNotification } = useNotifications() const [saving, setSaving] = useState(false) const [users, setUsers] = useState([]) useEffect(() => { fetch("/api/users") .then((r) => r.json()) .then((data) => setUsers(data.users || [])) .catch(() => {}) }, []) const form = useForm({ resolver: zodResolver(leadFormSchema), defaultValues: { companyName: "", contactName: "", email: "", phone: "", source: "", description: "", status: "open", assignedUserId: "none", }, }) async function onSubmit(values: LeadFormValues) { setSaving(true) try { const payload = { ...values, assignedUserId: values.assignedUserId === "none" ? null : (values.assignedUserId ?? null), } const res = await fetch("/api/leads", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }) if (!res.ok) throw new Error("Failed to create lead") const data = await res.json() addNotification("lead_created", "New Lead Created", `${values.companyName} — ${values.contactName}`, `/leads/${data.id}`) router.push("/leads") } catch (e) { console.error("Create lead error:", e) } finally { setSaving(false) } } return (
Back to leads

Create New Lead

Fill in the details to add a new lead.

( Company Name )} /> ( Contact Name )} /> ( Email )} /> ( Phone )} /> ( Source )} /> ( Status )} />
( Description