import { useState } from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Checkbox } from "@/components/ui/checkbox" import { useToast } from "@/components/ui/use-toast" import { updateLearner } from "@/lib/api" import { Course, BudgetType, LearnerIn, LearnerOut } from "@/types/learner" const formSchema = z.object({ name: z.string().min(1, "Name is required"), email: z.string().email("Invalid email address"), phone: z.string().optional(), course: z.nativeEnum(Course), country: z.string().min(1, "Country is required"), goal: z.string().min(1, "Goal is required"), budget: z.nativeEnum(BudgetType), webinar_join: z.string().min(1, "Join time is required"), webinar_leave: z.string().min(1, "Leave time is required"), asked_q: z.boolean(), referred: z.boolean(), past_touchpoints: z.number().min(0), }) interface EditLearnerDialogProps { learner: LearnerOut open: boolean onOpenChange: (open: boolean) => void onSuccess?: () => void } export function EditLearnerDialog({ learner, open, onOpenChange, onSuccess, }: EditLearnerDialogProps) { const [isSubmitting, setIsSubmitting] = useState(false) const { toast } = useToast() const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { name: learner.name, email: learner.email, phone: learner.phone || "", course: learner.course, country: learner.country, goal: learner.goal, budget: learner.budget, webinar_join: learner.webinar_join, webinar_leave: learner.webinar_leave, asked_q: learner.asked_q, referred: learner.referred, past_touchpoints: learner.past_touchpoints, }, }) async function onSubmit(values: z.infer) { setIsSubmitting(true) try { await updateLearner(learner.id, values as LearnerIn) toast({ title: "Success", description: "Learner updated successfully", }) onOpenChange(false) onSuccess?.() } catch (error) { toast({ title: "Error", description: "Failed to update learner", variant: "destructive", }) } finally { setIsSubmitting(false) } } return ( Edit Learner
( Name )} /> ( Email )} /> ( Phone (optional) )} /> ( Course )} /> ( Country )} /> ( Goal )} /> ( Budget )} /> ( Webinar Join Time )} /> ( Webinar Leave Time )} /> (
Asked Questions
)} /> (
Referred
)} /> ( Past Touchpoints field.onChange(parseInt(e.target.value))} /> )} />
) }