Fertig
This commit is contained in:
65
app/api/contact/route.ts
Normal file
65
app/api/contact/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
type ContactPayload = {
|
||||
name?: string;
|
||||
phone?: string;
|
||||
email?: string;
|
||||
projectType?: string;
|
||||
materialInterest?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
function isValidEmail(value: string) {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
||||
}
|
||||
|
||||
function isValidPhone(value: string) {
|
||||
return /^[0-9()+.\-\s]{10,}$/.test(value);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const payload = (await request.json()) as ContactPayload;
|
||||
const fieldErrors: Record<string, string> = {};
|
||||
|
||||
const name = payload.name?.trim() ?? "";
|
||||
const phone = payload.phone?.trim() ?? "";
|
||||
const email = payload.email?.trim() ?? "";
|
||||
const message = payload.message?.trim() ?? "";
|
||||
|
||||
if (!name) {
|
||||
fieldErrors.name = "Please enter your full name.";
|
||||
}
|
||||
|
||||
if (!phone) {
|
||||
fieldErrors.phone = "Please enter a phone number.";
|
||||
} else if (!isValidPhone(phone)) {
|
||||
fieldErrors.phone = "Please enter a valid phone number.";
|
||||
}
|
||||
|
||||
if (!email) {
|
||||
fieldErrors.email = "Please enter an email address.";
|
||||
} else if (!isValidEmail(email)) {
|
||||
fieldErrors.email = "Please enter a valid email address.";
|
||||
}
|
||||
|
||||
if (!message) {
|
||||
fieldErrors.message = "Please describe your project or material request.";
|
||||
}
|
||||
|
||||
if (Object.keys(fieldErrors).length > 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Please correct the highlighted fields and try again.",
|
||||
fieldErrors,
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message:
|
||||
"Thanks for reaching out. This form is wired to a placeholder API route and ready for email delivery integration.",
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user