import React from "react"; import { useForm, useFieldArray } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Form } from "@/components/ui/form"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { PlusCircle } from "lucide-react"; import { useCustomToast } from "@/hooks/useCustomToast"; import { SonDetailsForm } from "./SonDetailsForm"; import { ActionForm } from "./ActionForm"; // Import or define your schema here import { sonSchema } from "@/lib/schemas"; import { useSonContext } from "@/contexts/SonContext"; import { useListContext } from "@/contexts/ListContext"; import { useTemplateContext } from "@/contexts/TemplateContext"; type SonFormValues = z.infer; export default function SonCreationForm() { const { createSon } = useSonContext(); const { showToast } = useCustomToast(); const router = useRouter(); const { lists, loading: listsLoading, error: listsError } = useListContext(); const { templates, loading: templatesLoading, error: templatesError, } = useTemplateContext(); const form = useForm({ resolver: zodResolver(sonSchema), defaultValues: { name: "", trigger: "member_created", delay: "0s", enabled: true, actions: [ { type: "send_transactional_email", parameters: {}, }, ], }, }); const { fields, append, remove } = useFieldArray({ control: form.control, name: "actions", }); const onSubmit = async (data: SonFormValues) => { try { await createSon(data); showToast("Success", "Son created successfully"); router.push("/sons"); } catch (error) { showToast("Error", "Failed to create Son", "destructive"); console.error("Failed to create Son:", error); } }; if (listsLoading || templatesLoading) { return (
Loading...
); } if (listsError || templatesError) { return (
Error: {listsError?.message || templatesError?.message}
); } return (
Actions {fields.map((field, index) => ( ))} ); }