From 5777437af81deb7d26a0c2dddcf12b640f39877c Mon Sep 17 00:00:00 2001 From: "nabijonovdavronbek619@gmail.com" Date: Fri, 23 Jan 2026 10:06:11 +0500 Subject: [PATCH] contact part design updated --- components/pages/contact/form.tsx | 252 +++++++++++++++++++++++++++++ components/pages/contact/main.tsx | 253 +----------------------------- components/pages/home/banner.tsx | 10 +- 3 files changed, 261 insertions(+), 254 deletions(-) create mode 100644 components/pages/contact/form.tsx diff --git a/components/pages/contact/form.tsx b/components/pages/contact/form.tsx new file mode 100644 index 0000000..17c4aa9 --- /dev/null +++ b/components/pages/contact/form.tsx @@ -0,0 +1,252 @@ +"use client"; + +import { Check } from "lucide-react"; +import { useState } from "react"; + +interface FormData { + firstName: string; + lastName: string; + email: string; + subject: string; + message: string; + agreeToPolicy: boolean; +} + +interface FormErrors { + firstName?: string; + lastName?: string; + email?: string; + subject?: string; + message?: string; + agreeToPolicy?: string; +} + +export default function Form() { + const [formData, setFormData] = useState({ + firstName: "", + lastName: "", + email: "", + subject: "", + message: "", + agreeToPolicy: false, + }); + const [errors, setErrors] = useState({}); + const [isSubmitting, setIsSubmitting] = useState(false); + const [submitStatus, setSubmitStatus] = useState< + "idle" | "success" | "error" + >("idle"); + + const validateForm = (): boolean => { + const newErrors: FormErrors = {}; + + if (!formData.firstName.trim()) { + newErrors.firstName = "First name is required"; + } + if (!formData.lastName.trim()) { + newErrors.lastName = "Last name is required"; + } + if (!formData.email.trim()) { + newErrors.email = "Email is required"; + } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) { + newErrors.email = "Please enter a valid email"; + } + if (!formData.subject.trim()) { + newErrors.subject = "Subject is required"; + } + if (!formData.message.trim()) { + newErrors.message = "Message is required"; + } + if (!formData.agreeToPolicy) { + newErrors.agreeToPolicy = "You must agree to the privacy policy"; + } + + setErrors(newErrors); + return Object.keys(newErrors).length === 0; + }; + + const handleChange = ( + e: React.ChangeEvent, + ) => { + const { name, value } = e.target; + setFormData((prev) => ({ ...prev, [name]: value })); + if (errors[name as keyof FormErrors]) { + setErrors((prev) => ({ ...prev, [name]: undefined })); + } + }; + + const handleCheckboxChange = () => { + setFormData((prev) => ({ ...prev, agreeToPolicy: !prev.agreeToPolicy })); + if (errors.agreeToPolicy) { + setErrors((prev) => ({ ...prev, agreeToPolicy: undefined })); + } + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + + if (!validateForm()) return; + + setIsSubmitting(true); + setSubmitStatus("idle"); + + try { + const response = await fetch("/api/contact", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(formData), + }); + + if (response.ok) { + setSubmitStatus("success"); + setFormData({ + firstName: "", + lastName: "", + email: "", + subject: "", + message: "", + agreeToPolicy: false, + }); + } else { + setSubmitStatus("error"); + } + } catch { + setSubmitStatus("error"); + } finally { + setIsSubmitting(false); + } + }; + + return ( +
+ {/* First Row - Names */} +
+
+ + {errors.firstName && ( +

{errors.firstName}

+ )} +
+
+ + {errors.lastName && ( +

{errors.lastName}

+ )} +
+
+ + {/* Second Row - Email & Subject */} +
+
+ + {errors.email && ( +

{errors.email}

+ )} +
+
+ + {errors.subject && ( +

{errors.subject}

+ )} +
+
+ + {/* Message Textarea */} +
+