forked from sashaastiadi/www_mycelium_net
feat: add newsletter subscription form with loading and error states in Footer component
This commit is contained in:
48
src/app/api/subscribe/route.ts
Normal file
48
src/app/api/subscribe/route.ts
Normal 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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user