"use client"; import { useState, useEffect } from "react"; import { useRouter, useParams } from "next/navigation"; import { toast } from "sonner"; import { emailTemplatesApi, EmailTemplate } from "@/lib/api"; export default function EditEmailTemplatePage() { const router = useRouter(); const params = useParams(); const slug = params.slug as string; const [template, setTemplate] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); useEffect(() => { fetchTemplate(); }, [slug]); const fetchTemplate = async () => { try { const data = await emailTemplatesApi.get(slug); setTemplate(data); } catch (err: any) { toast.error(err.message || "Failed to load template"); } finally { setLoading(false); } }; const handleSave = async () => { if (!template) return; setSaving(true); try { await emailTemplatesApi.update(slug, { subject: template.subject, body_html: template.body_html, variables: template.variables, }); toast.success("Template saved!"); } catch (err: any) { toast.error(err.message || "Failed to save template"); } finally { setSaving(false); } }; if (loading) { return (
); } if (!template) { return (

Template not found

); } return (

Edit Template: {slug}

setTemplate({ ...template, subject: e.target.value })} className="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-primary" />