feat: add newsletter subscription form with loading and error states in Footer component

This commit is contained in:
2025-10-15 16:31:22 +02:00
parent 794605117a
commit ae277d33b5
2 changed files with 102 additions and 14 deletions

View File

@@ -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 },
);
}
}