fix(forms): Naprawa formularza multisplit i dodanie reCAPTCHA v3 do wszystkich formularzy
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { z } from 'zod';
|
||||
import { Resend } from 'resend';
|
||||
import { verifyRecaptcha } from '@/lib/recaptcha';
|
||||
|
||||
const resend = new Resend(process.env.RESEND_API_KEY);
|
||||
|
||||
@@ -18,6 +19,17 @@ const orderSchema = z.object({
|
||||
|
||||
export async function submitOrderForm(prevState: any, 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'),
|
||||
|
||||
+15
-2
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useCartStore } from '@/lib/cart-store';
|
||||
import { useActionState, useEffect } from 'react';
|
||||
import { useActionState, useEffect, startTransition } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { submitOrderForm } from './actions';
|
||||
import { ArrowLeft, CheckCircle } from 'lucide-react';
|
||||
@@ -75,7 +75,20 @@ Wróć do sklepu
|
||||
</div>
|
||||
|
||||
{/* Formularz */}
|
||||
<form action={formAction} className="bg-white rounded-xl border border-border p-6 flex flex-col gap-4">
|
||||
<form onSubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
const formData = new FormData(e.currentTarget);
|
||||
if (process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY) {
|
||||
const token = await (window as any).grecaptcha.execute(
|
||||
process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY,
|
||||
{ action: 'submit' }
|
||||
);
|
||||
formData.append('recaptchaToken', token);
|
||||
}
|
||||
startTransition(() => {
|
||||
formAction(formData);
|
||||
});
|
||||
}} className="bg-white rounded-xl border border-border p-6 flex flex-col gap-4">
|
||||
<h2 className="text-base font-bold text-primary font-heading">Twoje dane</h2>
|
||||
|
||||
{/* Hidden: produkty jako JSON */}
|
||||
|
||||
Reference in New Issue
Block a user