import { NextPageWithExtras } from "next"; import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { useForm, useFieldArray } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Button } from "@/components/ui/button"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { Form } from "@/components/ui/form"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { PlusCircle } from "lucide-react"; import { useSon } from "@/hooks/useSon"; import { useCustomToast } from "@/hooks/useCustomToast"; import { SonDetailsForm } from "@/components/SonDetailsForm"; import { ActionForm } from "@/components/ActionForm"; import { editableSonSchema } from "@/lib/schemas"; import { EditableSon } from "@/lib/types"; import { useSonContext } from "@/contexts/SonContext"; import { useListContext } from "@/contexts/ListContext"; import { useTemplateContext } from "@/contexts/TemplateContext"; function isValidTrigger(trigger: string): trigger is EditableSon["trigger"] { return [ "member_created", "member_deleted", "member_updated", "post_published", "post_scheduled", ].includes(trigger); } const SonDetailPage: NextPageWithExtras = () => { const router = useRouter(); const { id } = router.query; const { son, loading: sonLoading, error: sonError } = useSon(id as string); const { updateSon, deleteSon } = useSonContext(); const { showToast } = useCustomToast(); const { lists, loading: listsLoading, error: listsError } = useListContext(); const { templates, loading: templatesLoading, error: templatesError, } = useTemplateContext(); const form = useForm({ resolver: zodResolver(editableSonSchema), defaultValues: { name: "", trigger: "member_created", delay: 0, actions: [], }, }); const { fields, append, remove } = useFieldArray({ control: form.control, name: "actions", }); useEffect(() => { if (son) { const { name, trigger, delay, actions } = son; if (isValidTrigger(trigger)) { form.reset({ name, trigger, delay, actions: actions.map((action) => ({ type: action.type, parameters: action.parameters, })), }); } else { console.error(`Invalid trigger value: ${trigger}`); form.reset({ name, trigger: "member_created", delay, actions: actions.map((action) => ({ type: action.type, parameters: action.parameters, })), }); } } }, [son, form]); if (sonLoading || listsLoading || templatesLoading) { return (
Loading...
); } if (sonError || listsError || templatesError) { return (
Error:{" "} {sonError?.message || listsError?.message || templatesError?.message}
); } if (!son) return
Son not found
; const onSubmit = async (data: EditableSon) => { try { await updateSon(son.id, data); showToast("Success", "Son updated successfully"); router.push("/sons"); } catch (error) { showToast("Error", "Failed to update Son", "destructive"); console.error("Failed to update Son:", error); } }; const handleDelete = async () => { try { await deleteSon(son.id); showToast("Success", "Son deleted successfully"); router.push("/sons"); } catch (error) { showToast("Error", "Failed to delete Son", "destructive"); console.error("Failed to delete Son:", error); } }; return (

Edit Son: {son.name}

Created at: {new Date(son.created_at).toLocaleString()}

Last updated: {new Date(son.updated_at).toLocaleString()}

Actions {fields.map((field, index) => ( ))} Are you sure? This action cannot be undone. This will permanently delete the Son. Cancel Delete
); }; SonDetailPage.auth = true; export default SonDetailPage;