diff --git a/app/api/waitlist/route.ts b/app/api/waitlist/route.ts index 2dc2cae..54b46d4 100644 --- a/app/api/waitlist/route.ts +++ b/app/api/waitlist/route.ts @@ -1,4 +1,5 @@ import { NextResponse, NextRequest } from "next/server"; +import { MongoClient } from "mongodb"; import client from "@/lib/db"; export async function POST(req: NextRequest, res: NextResponse) { @@ -7,7 +8,7 @@ export async function POST(req: NextRequest, res: NextResponse) { } try { - const dbClient = await client.promise; + const dbClient = (await client.promise) as MongoClient; const db = dbClient.db("waitlist_db"); // Replace with your actual db name const email = (await req.json()) as any; const emailText = email.email; diff --git a/app/page.tsx b/app/page.tsx index 6d62127..6aa81af 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -13,7 +13,7 @@ export default function Home() { const [alertType, setAlertType] = useState("default"); const [alertMessage, setAlertMessage] = useState(""); const [submitted, setSubmitted] = useState(false); - async function handleSubmit(e) { + async function handleSubmit(e: any) { e.preventDefault(); // prevent the default form submission behavior const res = await fetch("/api/waitlist", { method: "POST", // specify the HTTP method @@ -81,11 +81,7 @@ export default function Home() { )} {alertMessage && ( - setAlertMessage("")} - > + {alertType === "success" ? ( ) : ( diff --git a/global.d.ts b/global.d.ts new file mode 100644 index 0000000..2835d24 --- /dev/null +++ b/global.d.ts @@ -0,0 +1,8 @@ +import { MongoClient } from "mongodb"; + +declare global { + var mongo: { + conn: MongoClient | null; + promise: Promise | null; + }; +} diff --git a/lib/db.js b/lib/db.js deleted file mode 100644 index 195e4d4..0000000 --- a/lib/db.js +++ /dev/null @@ -1,35 +0,0 @@ -// lib/db.js -import { MongoClient } from "mongodb"; - -const uri = process.env.MONGODB_URI; // Connection string from .env.local -const options = { - useUnifiedTopology: true, - useNewUrlParser: true, -}; - -let client; -let clientPromise; - -if (!process.env.MONGODB_URI) { - throw new Error("Please add your MongoDB URI to .env.local"); -} - -if (process.env.NODE_ENV === "development") { - // In development mode, use a global variable to keep the database connection - // open across hot reloads - if (!global.mongo) { - global.mongo = { conn: null, promise: null }; - } - client = global.mongo; -} else { - // In production mode, create a new connection for every request - client = {}; -} - -if (!client.promise) { - client.promise = MongoClient.connect(uri, options).then((mongoClient) => { - return mongoClient; - }); -} - -export default client; diff --git a/lib/db.ts b/lib/db.ts new file mode 100644 index 0000000..091a3c7 --- /dev/null +++ b/lib/db.ts @@ -0,0 +1,21 @@ +import { MongoClient } from "mongodb"; + +const uri = process.env.MONGODB_URI as string; // Connection string from .env.local + +if (!process.env.MONGODB_URI) { + throw new Error("Please add your MongoDB URI to .env.local"); +} + +let client = + process.env.NODE_ENV === "development" + ? global.mongo + : { conn: null, promise: null }; + +if (!client.promise) { + client.promise = MongoClient.connect(uri).then((mongoClient) => { + client.conn = mongoClient; + return mongoClient; + }); +} + +export default client;