Spaces:
Runtime error
Runtime error
Download frontend/src/components/edit-learner-dialog.tsx from adwhytahumans/CRM_FastAPI_React: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/adwhytahumans/CRM_FastAPI_React/resolve/main/frontend/src/components/edit-learner-dialog.tsx
- Command line
-
hf download hf://spaces/adwhytahumans/CRM_FastAPI_React/frontend/src/components/edit-learner-dialog.tsx
-
curl -L -o edit-learner-dialog.tsx https://huggingface.co/spaces/adwhytahumans/CRM_FastAPI_React/resolve/main/frontend/src/components/edit-learner-dialog.tsx
10.8 kB
| 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<z.infer<typeof formSchema>>({ | |
| 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<typeof formSchema>) { | |
| 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 ( | |
| <Dialog open={open} onOpenChange={onOpenChange}> | |
| <DialogContent className="max-w-2xl"> | |
| <DialogHeader> | |
| <DialogTitle>Edit Learner</DialogTitle> | |
| </DialogHeader> | |
| <Form {...form}> | |
| <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6"> | |
| <div className="grid grid-cols-2 gap-4"> | |
| <FormField | |
| control={form.control} | |
| name="name" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Name</FormLabel> | |
| <FormControl> | |
| <Input {...field} /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="email" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Email</FormLabel> | |
| <FormControl> | |
| <Input {...field} type="email" /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="phone" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Phone (optional)</FormLabel> | |
| <FormControl> | |
| <Input {...field} /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="course" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Course</FormLabel> | |
| <Select | |
| onValueChange={field.onChange} | |
| defaultValue={field.value} | |
| > | |
| <FormControl> | |
| <SelectTrigger> | |
| <SelectValue placeholder="Select a course" /> | |
| </SelectTrigger> | |
| </FormControl> | |
| <SelectContent> | |
| {Object.values(Course).map((course) => ( | |
| <SelectItem key={course} value={course}> | |
| {course} | |
| </SelectItem> | |
| ))} | |
| </SelectContent> | |
| </Select> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="country" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Country</FormLabel> | |
| <FormControl> | |
| <Input {...field} /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="goal" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Goal</FormLabel> | |
| <FormControl> | |
| <Input {...field} /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="budget" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Budget</FormLabel> | |
| <Select | |
| onValueChange={field.onChange} | |
| defaultValue={field.value} | |
| > | |
| <FormControl> | |
| <SelectTrigger> | |
| <SelectValue placeholder="Select budget type" /> | |
| </SelectTrigger> | |
| </FormControl> | |
| <SelectContent> | |
| {Object.values(BudgetType).map((budget) => ( | |
| <SelectItem key={budget} value={budget}> | |
| {budget.charAt(0).toUpperCase() + budget.slice(1)} | |
| </SelectItem> | |
| ))} | |
| </SelectContent> | |
| </Select> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="webinar_join" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Webinar Join Time</FormLabel> | |
| <FormControl> | |
| <Input {...field} type="datetime-local" /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="webinar_leave" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Webinar Leave Time</FormLabel> | |
| <FormControl> | |
| <Input {...field} type="datetime-local" /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="asked_q" | |
| render={({ field }) => ( | |
| <FormItem className="flex flex-row items-start space-x-3 space-y-0"> | |
| <FormControl> | |
| <Checkbox | |
| checked={field.value} | |
| onCheckedChange={field.onChange} | |
| /> | |
| </FormControl> | |
| <div className="space-y-1 leading-none"> | |
| <FormLabel>Asked Questions</FormLabel> | |
| </div> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="referred" | |
| render={({ field }) => ( | |
| <FormItem className="flex flex-row items-start space-x-3 space-y-0"> | |
| <FormControl> | |
| <Checkbox | |
| checked={field.value} | |
| onCheckedChange={field.onChange} | |
| /> | |
| </FormControl> | |
| <div className="space-y-1 leading-none"> | |
| <FormLabel>Referred</FormLabel> | |
| </div> | |
| </FormItem> | |
| )} | |
| /> | |
| <FormField | |
| control={form.control} | |
| name="past_touchpoints" | |
| render={({ field }) => ( | |
| <FormItem> | |
| <FormLabel>Past Touchpoints</FormLabel> | |
| <FormControl> | |
| <Input | |
| {...field} | |
| type="number" | |
| min={0} | |
| onChange={(e) => field.onChange(parseInt(e.target.value))} | |
| /> | |
| </FormControl> | |
| <FormMessage /> | |
| </FormItem> | |
| )} | |
| /> | |
| </div> | |
| <div className="flex justify-end space-x-4"> | |
| <Button | |
| type="button" | |
| variant="outline" | |
| onClick={() => onOpenChange(false)} | |
| > | |
| Cancel | |
| </Button> | |
| <Button type="submit" disabled={isSubmitting}> | |
| {isSubmitting ? "Updating..." : "Update Learner"} | |
| </Button> | |
| </div> | |
| </form> | |
| </Form> | |
| </DialogContent> | |
| </Dialog> | |
| ) | |
| } |