AbiirSkincare/Hydrogen/web/app/routes/api.contact.tsx

42 lines
1.3 KiB
TypeScript

import type {Route} from '~/lib/reactRouterRouteStubs';
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
export async function loader() {
return new Response(null, {status: 404});
}
export async function action({request}: Route.ActionArgs) {
if (request.method !== 'POST') {
return Response.json({ok: false, error: 'method_not_allowed'}, {status: 405});
}
const form = await request.formData();
const name = String(form.get('name') ?? '').trim();
const email = String(form.get('email') ?? '').trim().toLowerCase();
const subject = String(form.get('subject') ?? '').trim();
const message = String(form.get('message') ?? '').trim();
const phone = String(form.get('phone') ?? '').trim();
if (name.length < 2) {
return Response.json({ok: false, error: 'invalid_name'}, {status: 400});
}
if (!EMAIL_RE.test(email)) {
return Response.json({ok: false, error: 'invalid_email'}, {status: 400});
}
if (message.length < 4) {
return Response.json({ok: false, error: 'invalid_message'}, {status: 400});
}
/* Integrate e.g. SendGrid / Resend / Shopify notification here. */
console.log('[contact]', {
name,
email,
subject: subject || undefined,
phone: phone || undefined,
messageLength: message.length,
});
return Response.json({ok: true});
}