The form is built once to the settled field set, and a valid send is saved before anyone is emailed. Co-authored-by: Cursor <cursoragent@cursor.com>
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import {
|
|
EMPTY_WORKSHOP_FORM_VALUES,
|
|
type WorkshopFieldErrors,
|
|
type WorkshopFormValues,
|
|
} from "../workshopSignup";
|
|
|
|
export const WORKSHOP_SIGNUP_DRAFT_COOKIE = "workshop_signup_draft";
|
|
|
|
export interface WorkshopSignupDraft {
|
|
values: WorkshopFormValues;
|
|
fieldErrors: WorkshopFieldErrors;
|
|
}
|
|
|
|
export function encodeWorkshopSignupDraft(draft: WorkshopSignupDraft): string {
|
|
return Buffer.from(JSON.stringify(draft), "utf8").toString("base64url");
|
|
}
|
|
|
|
export function decodeWorkshopSignupDraft(raw: string | undefined): WorkshopSignupDraft | null {
|
|
if (!raw) return null;
|
|
try {
|
|
const parsed = JSON.parse(Buffer.from(raw, "base64url").toString("utf8")) as Partial<WorkshopSignupDraft>;
|
|
if (!parsed.values || typeof parsed.values !== "object") return null;
|
|
return {
|
|
values: { ...EMPTY_WORKSHOP_FORM_VALUES, ...parsed.values },
|
|
fieldErrors: parsed.fieldErrors ?? {},
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function attachWorkshopSignupDraft(
|
|
response: NextResponse,
|
|
draft: WorkshopSignupDraft,
|
|
): NextResponse {
|
|
response.cookies.set(WORKSHOP_SIGNUP_DRAFT_COOKIE, encodeWorkshopSignupDraft(draft), {
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
secure: process.env.NODE_ENV === "production",
|
|
path: "/workshops",
|
|
maxAge: 60 * 15,
|
|
});
|
|
return response;
|
|
}
|