92 lines
4.1 KiB
TypeScript
92 lines
4.1 KiB
TypeScript
'use server';
|
|
|
|
import { z } from 'zod';
|
|
import { Resend } from 'resend';
|
|
import { verifyRecaptcha } from '@/lib/recaptcha';
|
|
|
|
const resend = new Resend(process.env.RESEND_API_KEY);
|
|
|
|
const orderSchema = z.object({
|
|
name: z.string().min(2),
|
|
phone: z.string().min(9),
|
|
email: z.string().email(),
|
|
address: z.string().optional(),
|
|
notes: z.string().optional(),
|
|
consent: z.string(),
|
|
products: z.string(),
|
|
total: z.string(),
|
|
});
|
|
|
|
export async function submitOrderForm(prevState: unknown, formData: FormData) {
|
|
try {
|
|
if (process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY && process.env.RECAPTCHA_SECRET_KEY) {
|
|
const token = formData.get('recaptchaToken') as string;
|
|
if (!token) {
|
|
return { success: false, message: "Weryfikacja bezpieczeństwa nie powiodła się. Odśwież stronę i spróbuj ponownie." };
|
|
}
|
|
const { success } = await verifyRecaptcha(token);
|
|
if (!success) {
|
|
return { success: false, message: "Weryfikacja bezpieczeństwa nie powiodła się. Odśwież stronę i spróbuj ponownie." };
|
|
}
|
|
}
|
|
|
|
const data = orderSchema.parse({
|
|
name: formData.get('name'),
|
|
phone: formData.get('phone'),
|
|
email: formData.get('email'),
|
|
address: formData.get('address') || '',
|
|
notes: formData.get('notes') || '',
|
|
consent: formData.get('consent'),
|
|
products: formData.get('products'),
|
|
total: formData.get('total'),
|
|
});
|
|
|
|
const products = JSON.parse(data.products);
|
|
const total = parseFloat(data.total);
|
|
|
|
const productRows = products.map((p: { name: string; quantity: number; price: number }) =>
|
|
`<tr><td style="padding:8px;border-bottom:1px solid #f1f5f9;">${p.name}</td><td style="padding:8px;border-bottom:1px solid #f1f5f9;text-align:center;">${p.quantity}</td><td style="padding:8px;border-bottom:1px solid #f1f5f9;text-align:right;">${(p.price * p.quantity).toLocaleString('pl-PL')} zł</td></tr>`
|
|
).join('');
|
|
|
|
await resend.emails.send({
|
|
from: process.env.RESEND_FROM_EMAIL || '[email protected]',
|
|
to: process.env.RESEND_TO_EMAIL || '[email protected]',
|
|
replyTo: data.email,
|
|
subject: `Nowe zamówienie od ${data.name} — ${total.toLocaleString('pl-PL')} zł`,
|
|
html: `
|
|
<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;">
|
|
<div style="background:#0F2A47;padding:20px;text-align:center;">
|
|
<h1 style="color:#fff;margin:0;font-size:20px;">Nowe zamówienie — ThermCool</h1>
|
|
</div>
|
|
<div style="padding:24px;">
|
|
<h2 style="font-size:16px;margin-top:0;">Dane klienta</h2>
|
|
<p><strong>Imię:</strong> ${data.name}</p>
|
|
<p><strong>Telefon:</strong> <a href="tel:${data.phone}">${data.phone}</a></p>
|
|
<p><strong>Email:</strong> ${data.email}</p>
|
|
${data.address ? `<p><strong>Adres:</strong> ${data.address}</p>` : ''}
|
|
${data.notes ? `<p><strong>Uwagi:</strong> ${data.notes}</p>` : ''}
|
|
<h2 style="font-size:16px;">Zamówione produkty</h2>
|
|
<table style="width:100%;border-collapse:collapse;">
|
|
<thead><tr style="background:#f8fafc;">
|
|
<th style="padding:8px;text-align:left;">Produkt</th>
|
|
<th style="padding:8px;text-align:center;">Ilość</th>
|
|
<th style="padding:8px;text-align:right;">Wartość</th>
|
|
</tr></thead>
|
|
<tbody>${productRows}</tbody>
|
|
<tfoot><tr>
|
|
<td colspan="2" style="padding:8px;font-weight:bold;">Łącznie (urządzenia)</td>
|
|
<td style="padding:8px;font-weight:bold;text-align:right;">${total.toLocaleString('pl-PL')} zł</td>
|
|
</tr></tfoot>
|
|
</table>
|
|
<p style="margin-top:16px;font-size:12px;color:#64748b;">+ koszt montażu do ustalenia telefonicznie</p>
|
|
</div>
|
|
</div>
|
|
`,
|
|
});
|
|
|
|
return { success: true, message: 'Dziękujemy! Skontaktujemy się z Tobą w ciągu 24 godzin, aby potwierdzić zamówienie i ustalić termin montażu.' };
|
|
} catch {
|
|
return { success: false, message: 'Wystąpił błąd. Spróbuj ponownie lub zadzwoń do nas bezpośrednio.' };
|
|
}
|
|
}
|