159 lines
6.4 KiB
TypeScript
159 lines
6.4 KiB
TypeScript
/**
|
|
* Admin email-sequence create form (Phase 05 #80 follow-up).
|
|
*
|
|
* POST /admin/email-sequences/new — create a sequence and redirect to
|
|
* its detail page. Validates the steps JSON via the engine package
|
|
* before inserting so the row is always renderable by the trigger
|
|
* handler.
|
|
*/
|
|
import { Form, Link, redirect, useActionData, type ActionFunctionArgs, type LoaderFunctionArgs } from "react-router";
|
|
import { t as i18nT } from "i18next";
|
|
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
|
import { db } from "~/utils/db.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
|
|
type ActionResponse = { ok: true } | { ok: false; error: string };
|
|
|
|
const DEFAULT_STEPS = JSON.stringify(
|
|
[
|
|
{
|
|
stepIndex: 0,
|
|
delayHours: 0,
|
|
template: "welcome",
|
|
subject: i18nT("admin.emailSequences.defaults.welcomeSubject"),
|
|
variables: { productName: "HiveGrid" },
|
|
},
|
|
],
|
|
null,
|
|
2
|
|
);
|
|
|
|
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.marketing.leads.view");
|
|
return Response.json({});
|
|
};
|
|
|
|
export const action = async ({ request }: ActionFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.marketing.leads.view");
|
|
const { t } = await getTranslations(request);
|
|
const formData = await request.formData();
|
|
const name = String(formData.get("name") ?? "").trim();
|
|
const description = String(formData.get("description") ?? "").trim();
|
|
const triggerEvent = String(formData.get("triggerEvent") ?? "").trim();
|
|
const stepsRaw = String(formData.get("stepsJson") ?? "[]");
|
|
const conditionsRaw = String(formData.get("conditionsJson") ?? "");
|
|
if (!name) return Response.json({ ok: false, error: t("admin.emailSequences.errors.nameRequired") } satisfies ActionResponse);
|
|
if (!triggerEvent) return Response.json({ ok: false, error: t("admin.emailSequences.errors.triggerEventRequired") } satisfies ActionResponse);
|
|
let stepsJson: unknown;
|
|
try {
|
|
stepsJson = JSON.parse(stepsRaw);
|
|
} catch {
|
|
return Response.json({ ok: false, error: t("admin.emailSequences.errors.stepsJsonInvalid") } satisfies ActionResponse);
|
|
}
|
|
const { parseSequenceSteps } = await import("@hivegrid/email-sequences");
|
|
try {
|
|
parseSequenceSteps(stepsJson);
|
|
} catch (error) {
|
|
return Response.json({
|
|
ok: false,
|
|
error: error instanceof Error ? error.message : t("admin.emailSequences.errors.stepsJsonValidationFailed"),
|
|
} satisfies ActionResponse);
|
|
}
|
|
let conditionsJson: unknown = null;
|
|
if (conditionsRaw.trim().length > 0) {
|
|
try {
|
|
conditionsJson = JSON.parse(conditionsRaw);
|
|
} catch {
|
|
return Response.json({ ok: false, error: t("admin.emailSequences.errors.conditionsJsonInvalid") } satisfies ActionResponse);
|
|
}
|
|
}
|
|
const created = await db.emailSequence.create({
|
|
data: {
|
|
name,
|
|
description: description || null,
|
|
triggerEvent,
|
|
stepsJson: stepsJson as never,
|
|
conditionsJson: conditionsJson as never,
|
|
isEnabled: false,
|
|
},
|
|
});
|
|
return redirect(`${adminPath("email-sequences", { mode: "host-aware" })}/${created.id}`);
|
|
};
|
|
|
|
export default function EmailSequenceNew() {
|
|
const action = useActionData<ActionResponse>();
|
|
return (
|
|
<div className="mx-auto mb-12 max-w-5xl space-y-5 px-4 py-4 sm:px-6 lg:px-8">
|
|
<div className="border-border border-b pb-5">
|
|
<h1 className="text-lg leading-6 font-medium">{i18nT("admin.emailSequences.form.newTitle")}</h1>
|
|
<p className="text-muted-foreground mt-1 text-sm">
|
|
{i18nT("admin.emailSequences.form.instructions")}
|
|
</p>
|
|
</div>
|
|
|
|
{action && !action.ok ? <div className="rounded-md border border-rose-300 bg-rose-50 px-3 py-2 text-sm text-rose-800">{action.error}</div> : null}
|
|
|
|
<Form method="post" className="space-y-4">
|
|
<div>
|
|
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="name">
|
|
{i18nT("admin.emailSequences.form.name")}
|
|
</label>
|
|
<input id="name" name="name" required className="border-border w-full rounded-md border px-3 py-1.5 text-sm" />
|
|
</div>
|
|
<div>
|
|
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="description">
|
|
{i18nT("admin.emailSequences.form.description")}
|
|
</label>
|
|
<input id="description" name="description" className="border-border w-full rounded-md border px-3 py-1.5 text-sm" />
|
|
</div>
|
|
<div>
|
|
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="triggerEvent">
|
|
{i18nT("admin.emailSequences.form.triggerEvent")}
|
|
</label>
|
|
<input
|
|
id="triggerEvent"
|
|
name="triggerEvent"
|
|
required
|
|
placeholder={i18nT("admin.emailSequences.placeholders.triggerEvent")}
|
|
className="border-border w-full rounded-md border px-3 py-1.5 text-sm"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="stepsJson">
|
|
{i18nT("admin.emailSequences.form.stepsJson")}
|
|
</label>
|
|
<textarea
|
|
id="stepsJson"
|
|
name="stepsJson"
|
|
defaultValue={DEFAULT_STEPS}
|
|
rows={14}
|
|
spellCheck={false}
|
|
className="border-border w-full rounded-md border px-3 py-1.5 font-mono text-xs"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="text-foreground mb-1 block text-sm font-medium" htmlFor="conditionsJson">
|
|
{i18nT("admin.emailSequences.form.conditionsJson")}
|
|
</label>
|
|
<textarea
|
|
id="conditionsJson"
|
|
name="conditionsJson"
|
|
rows={6}
|
|
spellCheck={false}
|
|
className="border-border w-full rounded-md border px-3 py-1.5 font-mono text-xs"
|
|
/>
|
|
</div>
|
|
<div className="flex items-center justify-between">
|
|
<Link to={adminPath("email-sequences", { mode: "host-aware" })} className="text-primary text-sm hover:underline">
|
|
{i18nT("admin.emailSequences.actions.backToList")}
|
|
</Link>
|
|
<button type="submit" className="bg-primary text-primary-foreground rounded-md px-3 py-1.5 text-sm">
|
|
{i18nT("admin.emailSequences.actions.createDisabled")}
|
|
</button>
|
|
</div>
|
|
</Form>
|
|
</div>
|
|
);
|
|
}
|