mirror of
https://github.com/Techtonic-Fault/homepage.git
synced 2026-01-23 13:29:52 +00:00
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
"use server"
|
|
|
|
import { z } from "zod"
|
|
|
|
// Define a schema for form validation
|
|
const ContactFormSchema = z.object({
|
|
firstName: z.string().min(1, "Il nome è obbligatorio"),
|
|
lastName: z.string().min(1, "Il cognome è obbligatorio"),
|
|
email: z.string().email("Indirizzo email non valido"),
|
|
company: z.string().optional(),
|
|
message: z.string().min(10, "Il messaggio deve contenere almeno 10 caratteri"),
|
|
})
|
|
|
|
type ContactFormData = z.infer<typeof ContactFormSchema>
|
|
|
|
export async function submitContactForm(formData: FormData) {
|
|
// Artificial delay to simulate network request
|
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
|
|
try {
|
|
// Extract and validate form data
|
|
const data = {
|
|
firstName: formData.get("firstName") as string,
|
|
lastName: formData.get("lastName") as string,
|
|
email: formData.get("email") as string,
|
|
company: formData.get("company") as string,
|
|
message: formData.get("message") as string,
|
|
} as ContactFormData
|
|
|
|
// Validate the form data
|
|
const validatedData = ContactFormSchema.parse(data)
|
|
|
|
// In a real application, you would send an email here
|
|
// For example, using a service like SendGrid, Mailgun, etc.
|
|
console.log("Form submission:", validatedData)
|
|
|
|
// For demonstration purposes, let's log what would happen in a real app
|
|
console.log(`
|
|
In a production environment, an email would be sent with:
|
|
To: contact@techtonicfault.com
|
|
From: ${validatedData.email}
|
|
Subject: New contact form submission from ${validatedData.firstName} ${validatedData.lastName}
|
|
Body: ${validatedData.message}
|
|
Additional Info: Company - ${validatedData.company || "Not provided"}
|
|
`)
|
|
|
|
// Return success response
|
|
return {
|
|
success: true,
|
|
message: "Grazie per l'interesse! Ti ricontatteremo a breve.",
|
|
}
|
|
} catch (error) {
|
|
console.error("Form validation error:", error)
|
|
|
|
if (error instanceof z.ZodError) {
|
|
// Return validation errors
|
|
const errorMessages = error.errors.map((err) => `${err.message}`).join(", ")
|
|
return {
|
|
success: false,
|
|
message: `Ricontrolla i dati: ${errorMessages}`,
|
|
}
|
|
}
|
|
|
|
// Return generic error
|
|
return {
|
|
success: false,
|
|
message: "Qualcosa è andato storto. Riprova più tardi.",
|
|
}
|
|
}
|
|
}
|