Skip to content

Commit e76e567

Browse files
committed
feat:added back-end for subscription box
Inputs signed up emails into google sheet
1 parent be5224f commit e76e567

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

src/app/api/subscribe/route.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { NextResponse } from "next/server";
2+
import { google, sheets_v4 } from "googleapis";
3+
import { JWT } from "google-auth-library";
4+
5+
const SCOPES = ["https://www.googleapis.com/auth/spreadsheets"];
6+
const SHEET_ID = process.env.SHEET_ID;
7+
8+
async function getAuth(): Promise<JWT> {
9+
return new google.auth.JWT({
10+
email: process.env.GOOGLE_CLIENT_EMAIL,
11+
key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, "\n"),
12+
scopes: SCOPES,
13+
});
14+
}
15+
16+
async function appendEmailToSheet(email: string) {
17+
const authClient = await getAuth();
18+
const sheets: sheets_v4.Sheets = google.sheets({
19+
version: "v4",
20+
auth: authClient,
21+
});
22+
23+
await sheets.spreadsheets.values.append({
24+
spreadsheetId: SHEET_ID,
25+
range: "Sheet1!A:A",
26+
valueInputOption: "RAW",
27+
requestBody: {
28+
values: [[email]],
29+
},
30+
});
31+
}
32+
33+
export async function POST(req: Request) {
34+
try {
35+
const { email } = await req.json();
36+
37+
if (!email) {
38+
return NextResponse.json({ error: "Email is required" }, { status: 400 });
39+
}
40+
41+
await appendEmailToSheet(email);
42+
return NextResponse.json({ message: "Email added successfully" });
43+
} catch (error) {
44+
console.error("Error adding email:", error);
45+
return NextResponse.json({ error: "Failed to add email" }, { status: 500 });
46+
}
47+
}

src/components/Footer.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,26 @@ export default function Footer() {
2929
toast.error("Please Enter A Valid Email.");
3030
return;
3131
}
32-
33-
const fakeSubscribe = () =>
34-
new Promise((resolve, reject) => {
35-
setTimeout(() => {
36-
resolve("Subscribed");
37-
}, 1500);
38-
});
39-
40-
toast.promise(fakeSubscribe(), {
41-
loading: "Subscribing...",
42-
success: "You've Successfully Subscribed!",
43-
error: "Something Went Wrong. Please Try Again.",
44-
});
45-
32+
33+
await toast.promise(
34+
fetch("/api/subscribe", {
35+
method: "POST",
36+
headers: { "Content-Type": "application/json" },
37+
body: JSON.stringify({ email }),
38+
}).then((res) => {
39+
if (!res.ok) throw new Error("Network response was not ok.");
40+
return res.json();
41+
}),
42+
{
43+
loading: "Subscribing...",
44+
success: "You've Successfully Subscribed!",
45+
error: "Something went wrong. Try again later.",
46+
}
47+
);
48+
4649
setEmail("");
4750
};
48-
51+
4952
return (
5053
<footer className="w-full overflow-hidden bg-gradient-to-b from-[#F3F5FF] to-[#A599CE] px-12 py-12 font-sans text-white dark:from-[#070114] dark:to-[#1F0234]">
5154
<div className="mx-auto flex max-w-[1440px] flex-col gap-y-4 lg:flex-row lg:justify-between">

0 commit comments

Comments
 (0)