diff --git a/components/newsletter.tsx b/components/newsletter.tsx new file mode 100644 index 0000000..6dbeaf1 --- /dev/null +++ b/components/newsletter.tsx @@ -0,0 +1,107 @@ +"use client"; + +import { useState } from "react"; +import { Button } from "./ui/button"; + +export function Newsletter() { + const [email, setEmail] = useState(""); + const [status, setStatus] = useState< + "idle" | "loading" | "success" | "error" + >("idle"); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setStatus("loading"); + + try { + // Get app_id from environment or URL + const appId = + process.env.NEXT_PUBLIC_APP_ID || + new URLSearchParams(window.location.search).get("app_id") || + ""; + + // Create form data + const formData = new FormData(); + formData.append("app_id", appId); + formData.append("email", email); + formData.append("form_type", "newsletter"); // Help identify form type in Kleap + formData.append("_honeypot", ""); // Honeypot field + + // Submit to Kleap's form API + const response = await fetch("https://form.kleap.co", { + method: "POST", + body: formData, + }); + + if (response.ok) { + setStatus("success"); + setEmail(""); + // Reset status after 3 seconds + setTimeout(() => setStatus("idle"), 3000); + } else { + throw new Error("Failed to submit"); + } + } catch { + setStatus("error"); + // Reset status after 3 seconds + setTimeout(() => setStatus("idle"), 3000); + } + }; + + return ( +
+

+ Subscribe to our newsletter +

+

+ Get the latest updates and exclusive content delivered to your inbox. +

+ +
+ setEmail(e.target.value)} + placeholder="Enter your email" + required + className="flex-1 px-4 py-2 rounded-md border border-gray-300 dark:border-gray-700 bg-white dark:bg-neutral-900 text-black dark:text-white placeholder:text-gray-400 focus:ring-2 focus:ring-neutral-400 focus:outline-none" + /> + + {/* Honeypot field - hidden from users */} + + + +
+ + {status === "success" && ( +

+ Thanks for subscribing! +

+ )} + + {status === "error" && ( +

+ Something went wrong. Please try again. +

+ )} +
+ ); +}