From f28abe568f133ebf0eb9a14208fb3da8d4c82b81 Mon Sep 17 00:00:00 2001 From: Dan Farrelly Date: Fri, 22 Nov 2024 11:30:25 -0500 Subject: [PATCH] Submit all form data --- app/ai/early-access/page.tsx | 2 +- components/NewsletterSignup.tsx | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/ai/early-access/page.tsx b/app/ai/early-access/page.tsx index b9aace605..c81401503 100644 --- a/app/ai/early-access/page.tsx +++ b/app/ai/early-access/page.tsx @@ -29,7 +29,7 @@ export default function Page() { diff --git a/components/NewsletterSignup.tsx b/components/NewsletterSignup.tsx index fb3f927d2..650cdd952 100644 --- a/components/NewsletterSignup.tsx +++ b/components/NewsletterSignup.tsx @@ -1,6 +1,6 @@ "use client"; -import { useRef, useState } from "react"; +import { useRef, useState, type FormEvent } from "react"; import { useSearchParams } from "next/navigation"; export default function NewsletterSignup({ @@ -13,7 +13,7 @@ export default function NewsletterSignup({ showHeader?: boolean; buttonText?: string; tags?: string[]; - tagsFromSearchParams: boolean; + tagsFromSearchParams?: boolean; fields?: { name: string; label: string }[]; }) { const inputRef = useRef(null); @@ -35,15 +35,13 @@ export default function NewsletterSignup({ subscriberTags = tag ? (typeof tag === "string" ? [tag] : tag) : []; } - const subscribeUser = async (e) => { + const onSubmit = async (e: FormEvent) => { e.preventDefault(); + const formData = new FormData(e.currentTarget); + const data = Object.assign({ tags }, formDataToObject(formData)); setLoading(true); const res = await fetch("/api/newsletter/subscribe", { - body: JSON.stringify({ - // @ts-ignore - email: inputRef.current.value, - tags: subscriberTags, - }), + body: JSON.stringify(data), headers: { "Content-Type": "application/json", }, @@ -62,7 +60,7 @@ export default function NewsletterSignup({ const canSubmit = response.result !== true || response.error !== ""; return ( -
+ {showHeader &&

Get Notified

}
@@ -71,15 +69,14 @@ export default function NewsletterSignup({ ); } + +function formDataToObject(formData: FormData): { [key: string]: string } { + const data = {}; + for (let [key, value] of Array.from(formData.entries())) { + data[key] = value; + } + return data; +}