diff --git a/src/app/api/subscribe/route.ts b/src/app/api/subscribe/route.ts new file mode 100644 index 0000000..4e24515 --- /dev/null +++ b/src/app/api/subscribe/route.ts @@ -0,0 +1,48 @@ +import { NextRequest, NextResponse } from 'next/server'; + +export async function POST(req: NextRequest) { + const { email } = await req.json(); + + if (!email) { + return NextResponse.json({ error: 'Email is required' }, { status: 400 }); + } + + const MAILERLITE_API_KEY = process.env.MAILERLITE_API_KEY; + const MAILERLITE_GROUP_ID = process.env.MAILERLITE_GROUP_ID; + + if (!MAILERLITE_API_KEY || !MAILERLITE_GROUP_ID) { + return NextResponse.json( + { error: 'MailerLite API key or Group ID are not configured' }, + { status: 500 }, + ); + } + + try { + const response = await fetch( + `https://api.mailerlite.com/api/v2/groups/${MAILERLITE_GROUP_ID}/subscribers`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-MailerLite-ApiKey': MAILERLITE_API_KEY, + }, + body: JSON.stringify({ email }), + }, + ); + + if (!response.ok) { + const errorData = await response.json(); + return NextResponse.json( + { error: errorData.error.message || 'Something went wrong' }, + { status: response.status }, + ); + } + + return NextResponse.json({ success: true }, { status: 200 }); + } catch (error) { + return NextResponse.json( + { error: 'An unexpected error occurred' }, + { status: 500 }, + ); + } +} diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx index 0a679da..10dec1e 100644 --- a/src/components/Footer.tsx +++ b/src/components/Footer.tsx @@ -1,5 +1,8 @@ +'use client' + import Image from 'next/image' import Link from 'next/link' +import { useState } from 'react' import { Button } from '@/components/Button' import { Container } from '@/components/Container' @@ -9,6 +12,39 @@ import github from '@/images/github.svg' import logomark from '@/images/logomark.svg' export function Footer() { + const [email, setEmail] = useState(''); + const [loading, setLoading] = useState(false); + const [message, setMessage] = useState(''); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setLoading(true); + setMessage(''); + + try { + const response = await fetch('/api/subscribe', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ email }), + }); + + const data = await response.json(); + + if (!response.ok) { + throw new Error(data.error || 'Something went wrong'); + } + + setMessage('Thanks for subscribing!'); + setEmail(''); + } catch (error: any) { + setMessage(error.message); + } finally { + setLoading(false); + } + }; + return (