diff --git a/src/api/emailTemplates.api.ts b/src/api/emailTemplates.api.ts index 351ce8a..010beb4 100644 --- a/src/api/emailTemplates.api.ts +++ b/src/api/emailTemplates.api.ts @@ -12,9 +12,16 @@ import type { UpdateEmailTemplateResponse, } from "../types/emailTemplate.types" -/** GET /admin/email-templates — list all email templates. */ -export const getEmailTemplates = () => - http.get("/admin/email-templates") +export interface GetEmailTemplatesParams { + status?: string + limit?: number + offset?: number + query?: string +} + +/** GET /admin/email-templates — list email templates (paginated). */ +export const getEmailTemplates = (params?: GetEmailTemplatesParams) => + http.get("/admin/email-templates", { params }) /** GET /admin/email-templates/slug/:slug — single template by slug. */ export const getEmailTemplateBySlug = (slug: string) => @@ -23,28 +30,55 @@ export const getEmailTemplateBySlug = (slug: string) => ) function normalizeEmailTemplate(row: unknown): EmailTemplate | null { - if (!row || typeof row !== "object" || !("slug" in row)) return null - const t = row as EmailTemplate + if (!row || typeof row !== "object") return null + const o = row as Record + const slug = String(o.slug ?? o.Slug ?? "").trim() + if (!slug) return null + + const id = Number(o.id ?? o.ID ?? o.Id) + const variablesRaw = o.variables ?? o.Variables + const variables = Array.isArray(variablesRaw) + ? variablesRaw.map((v) => String(v).trim()).filter(Boolean) + : [] + return { - ...t, - variables: Array.isArray(t.variables) ? t.variables : [], - status: t.status ?? "ACTIVE", - updated_at: t.updated_at ?? t.created_at ?? "", + id: Number.isFinite(id) && id > 0 ? id : 0, + slug, + name: String(o.name ?? o.Name ?? slug), + subject: String(o.subject ?? o.Subject ?? ""), + body_text: String(o.body_text ?? o.BodyText ?? o.bodyText ?? ""), + body_html: String(o.body_html ?? o.BodyHtml ?? o.bodyHTML ?? ""), + variables, + is_system: Boolean(o.is_system ?? o.IsSystem ?? false), + status: String(o.status ?? o.Status ?? "ACTIVE"), + created_at: String(o.created_at ?? o.CreatedAt ?? ""), + updated_at: String(o.updated_at ?? o.UpdatedAt ?? o.created_at ?? o.CreatedAt ?? ""), } } +function extractTemplateRows(data: unknown): unknown[] { + if (Array.isArray(data)) return data + if (!data || typeof data !== "object") return [] + const record = data as Record + const candidates = [ + record.templates, + record.Templates, + record.email_templates, + record.EmailTemplates, + ] + for (const candidate of candidates) { + if (Array.isArray(candidate)) return candidate + } + return [] +} + export function parseEmailTemplatesResponse( response: Awaited>, ): EmailTemplate[] { const data = response.data?.data - const rows = Array.isArray(data) - ? data - : Array.isArray(data?.templates) - ? data.templates - : [] - return rows + return extractTemplateRows(data) .map(normalizeEmailTemplate) - .filter((row): row is EmailTemplate => row != null) + .filter((row): row is EmailTemplate => row != null && row.id > 0) } /** PUT /admin/email-templates/:id — update subject and bodies. */ diff --git a/src/components/notifications/EmailComposeFields.tsx b/src/components/notifications/EmailComposeFields.tsx index 706259f..2bc5641 100644 --- a/src/components/notifications/EmailComposeFields.tsx +++ b/src/components/notifications/EmailComposeFields.tsx @@ -1,5 +1,6 @@ import { useEffect, useMemo, useState } from "react" -import { Eye, RefreshCw } from "lucide-react" +import { Link } from "react-router-dom" +import { Eye, ExternalLink, RefreshCw } from "lucide-react" import { toast } from "sonner" import { parseEmailTemplatePreviewResponse, @@ -10,12 +11,17 @@ import { Input } from "../ui/input" import { SpinnerIcon } from "../ui/spinner-icon" import { Textarea } from "../ui/textarea" import { Select } from "../ui/select" +import { cn } from "../../lib/utils" import { buildEmailTemplateVariablesForSend, + defaultOutboundEmailTemplateSlug, + isAutomatedEmailTemplateSlug, isCustomMessageTemplate, } from "../../lib/notificationEmailCompose" import type { EmailTemplate, PreviewEmailTemplateResult } from "../../types/emailTemplate.types" +type EmailContentMode = "freeform" | "template" + type EmailComposeFieldsProps = { templates: EmailTemplate[] templatesLoading: boolean @@ -47,20 +53,48 @@ export function EmailComposeFields({ }: EmailComposeFieldsProps) { const [previewLoading, setPreviewLoading] = useState(false) const [preview, setPreview] = useState(null) + const [contentMode, setContentMode] = useState( + templateSlug ? "template" : "freeform", + ) const selectedTemplate = useMemo( () => templates.find((t) => t.slug === templateSlug) ?? null, [templates, templateSlug], ) - const isFreeForm = !templateSlug + const isFreeForm = contentMode === "freeform" const isCustomMessage = isCustomMessageTemplate(templateSlug) const usesTemplateVariables = - Boolean(templateSlug) && !isCustomMessage && (selectedTemplate?.variables.length ?? 0) > 0 + contentMode === "template" && + Boolean(templateSlug) && + !isCustomMessage && + (selectedTemplate?.variables.length ?? 0) > 0 + + useEffect(() => { + setContentMode(templateSlug ? "template" : "freeform") + }, [templateSlug]) useEffect(() => { setPreview(null) - }, [templateSlug, subject, message, htmlBody, templateVariables]) + }, [templateSlug, subject, message, htmlBody, templateVariables, contentMode]) + + const switchToFreeForm = () => { + setContentMode("freeform") + onTemplateSlugChange("") + onTemplateVariablesChange({}) + setPreview(null) + } + + const switchToTemplate = () => { + setContentMode("template") + const slug = templateSlug || defaultOutboundEmailTemplateSlug(templates) + onTemplateSlugChange(slug) + onTemplateVariablesChange({}) + if (slug && !isCustomMessageTemplate(slug)) { + onHtmlBodyChange("") + } + setPreview(null) + } const handleTemplateChange = (slug: string) => { onTemplateSlugChange(slug) @@ -68,6 +102,7 @@ export function EmailComposeFields({ if (slug && !isCustomMessageTemplate(slug)) { onHtmlBodyChange("") } + setPreview(null) } const handlePreview = async () => { @@ -98,133 +133,229 @@ export function EmailComposeFields({ } return ( -
-
- - -

- {templatesLoading - ? "Loading templates…" - : "System templates (OTP, welcome, etc.) are excluded from outbound campaigns."} -

+
+
+
+

+ Email content +

+

+ Send free-form HTML or render a stored email template on the server. +

+
+
- {(isFreeForm || isCustomMessage) && ( -
- - onSubjectChange(e.target.value)} - /> -
- )} - - {(isFreeForm || isCustomMessage) && ( -
- -