-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from haseebzaki-07/new_branch
Add subscribe
- Loading branch information
Showing
8 changed files
with
211 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import nodemailer from "nodemailer"; | ||
|
||
type Data = { | ||
message: string; | ||
}; | ||
|
||
const transporter = nodemailer.createTransport({ | ||
host: process.env.EMAIL_HOST || "smtp.gmail.com", | ||
port: Number(process.env.EMAIL_PORT) || 587, | ||
secure: process.env.EMAIL_PORT === "465", | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
}); | ||
|
||
// Helper function to send a confirmation email | ||
const sendConfirmationEmail = async (email: string) => { | ||
try { | ||
await transporter.sendMail({ | ||
from: `"FlashFathom AI" <${process.env.EMAIL_USER}>`, | ||
to: email, | ||
subject: "Thank you for subscribing!", | ||
text: "Thank you for subscribing to our platform!", | ||
html: `<p>Thank you for subscribing to our platform!</p>`, | ||
}); | ||
} catch (error) { | ||
console.error("Error sending email:", error); | ||
throw new Error("Failed to send confirmation email"); | ||
} | ||
}; | ||
|
||
export async function GET() { | ||
return NextResponse.json({ message: "Welcome to the subscribe API!" }); | ||
} | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const { email } = await req.json(); | ||
|
||
if ( | ||
!email || | ||
typeof email !== "string" || | ||
!/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email) | ||
) { | ||
return NextResponse.json( | ||
{ message: "Invalid email address" }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
await sendConfirmationEmail(email); | ||
return NextResponse.json( | ||
{ message: "Subscription confirmation email sent" }, | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error("Error processing request:", error); | ||
|
||
if (error instanceof SyntaxError) { | ||
return NextResponse.json( | ||
{ message: "Invalid JSON format" }, | ||
{ status: 400 } | ||
); | ||
} | ||
return NextResponse.json( | ||
{ message: "Error sending email, please try again later" }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"use client"; | ||
import React, { useState } from "react"; | ||
import axios from "axios"; | ||
import { toast} from "react-toastify"; | ||
import 'react-toastify/dist/ReactToastify.css'; | ||
|
||
const StayUpdated = () => { | ||
const [email, setEmail] = useState<string>(""); | ||
const [status, setStatus] = useState<"idle" | "loading" | "success" | "error">("idle"); | ||
const apiUrl = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000"; | ||
|
||
const handleSubmit = async (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
setStatus("loading"); | ||
|
||
try { | ||
const response = await axios.post(`${apiUrl}/api/subscribe`, { email }); | ||
|
||
setStatus("success"); | ||
|
||
|
||
toast.success(response.data.message || "Thank you for subscribing!", { | ||
position: "top-right", | ||
autoClose: 3000, | ||
}); | ||
|
||
setEmail(""); | ||
} catch (error: any) { | ||
setStatus("error"); | ||
|
||
|
||
toast.error( | ||
error.response?.data?.message || "Failed to subscribe. Please try again.", | ||
{ | ||
position: "top-right", | ||
autoClose: 3000, | ||
} | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="w-full flex flex-col items-center"> | ||
<form onSubmit={handleSubmit} className="flex flex-col sm:flex-row items-center space-y-2 sm:space-y-0 sm:space-x-2"> | ||
<input | ||
type="email" | ||
placeholder="Enter your email" | ||
value={email} | ||
onChange={(e) => setEmail(e.target.value)} | ||
required | ||
className="border border-gray-300 rounded px-4 py-2 w-full sm:w-auto dark:border-gray-600 dark:bg-gray-900 dark:text-gray-100" | ||
/> | ||
<button | ||
type="submit" | ||
disabled={status === "loading"} | ||
className="bg-purple-600 text-white rounded px-4 py-2 hover:bg-purple-700 dark:bg-purple-500 dark:hover:bg-purple-600" | ||
> | ||
{status === "loading" ? "Subscribing..." : "Subscribe"} | ||
</button> | ||
</form> | ||
|
||
|
||
</div> | ||
); | ||
}; | ||
|
||
export default StayUpdated; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters