Files
reui-blocks/blocks/contact2.tsx
T

298 lines
11 KiB
TypeScript

"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { GlobeIcon, LoaderIcon, MailIcon, PhoneIcon } from "lucide-react";
import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
Field,
FieldError,
FieldGroup,
FieldLabel,
} from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { cn } from "@/lib/utils";
const contactFormSchema = z.object({
firstName: z.string().min(1, "نام را وارد کنید"),
lastName: z.string().min(1, "نام خانوادگی را وارد کنید"),
email: z
.string()
.min(1, "ایمیل را وارد کنید")
.email("ایمیل واردشده معتبر نیست"),
subject: z.string().min(1, "موضوع را وارد کنید"),
message: z.string().min(1, "پیام را وارد کنید"),
});
type ContactFormData = z.infer<typeof contactFormSchema>;
interface Contact2Props {
title?: string;
description?: string;
phone?: string;
email?: string;
web?: { label: string; url: string };
className?: string;
onSubmit?: (data: ContactFormData) => Promise<void>;
}
const Contact2 = ({
title = "با تیم فروش صحبت کنید",
description = "برای دمو سامانه مدیریت ارتباط با مشتری، انتخاب پلن یا مشاوره استقرار پیام بگذارید. کارشناس فروش معمولاً ظرف یک روز کاری پاسخ می‌دهد.",
phone = "۲۱-۱۲۳۴۵۶۷۸",
email = "contact@example.com",
web = {
label: "راهنمای محصول",
url: "https://www.ShadcnBlocks",
},
className,
onSubmit,
}: Contact2Props) => {
const [isSubmitted, setIsSubmitted] = useState(false);
const [showSuccess, setShowSuccess] = useState(false);
const form = useForm<ContactFormData>({
resolver: zodResolver(contactFormSchema),
mode: "onSubmit",
reValidateMode: "onSubmit",
defaultValues: {
firstName: "",
lastName: "",
email: "",
subject: "",
message: "",
},
});
const handleFormSubmit = async (data: ContactFormData) => {
try {
if (onSubmit) {
await onSubmit(data);
} else {
console.log("Form submitted:", data);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
setIsSubmitted(true);
setShowSuccess(true);
form.reset();
setTimeout(() => setShowSuccess(false), 4500);
setTimeout(() => setIsSubmitted(false), 5000);
} catch {
form.setError("root", {
message: "ارسال درخواست ناموفق بود. دوباره تلاش کنید.",
});
}
};
return (
<section dir="rtl" lang="fa" className={cn("py-32", className)}>
<div className="container">
<div className="mx-auto flex max-w-6xl flex-col gap-16 lg:flex-row lg:gap-24">
<div className="flex flex-1 flex-col gap-10">
<div>
<h1 className="mb-4 text-xl font-medium tracking-tight md:text-2xll">
{title}
</h1>
<p className="text-muted-foreground">{description}</p>
</div>
<div className="flex flex-col gap-6">
<a
href={`tel:${phone}`}
className="group flex items-center gap-3"
>
<PhoneIcon className="size-5 text-muted-foreground" />
<span className="group-hover:underline" dir="rtl">
{phone}
</span>
</a>
<a
href={`mailto:${email}`}
className="group flex items-center gap-3"
>
<MailIcon className="size-5 text-muted-foreground" />
<span className="group-hover:underline" dir="rtl">
{email}
</span>
</a>
<a
href={web.url}
target="_blank"
rel="noopener noreferrer"
className="group flex items-center gap-3"
>
<GlobeIcon className="size-5 text-muted-foreground" />
<span className="group-hover:underline" dir="rtl">
{web.label}
</span>
</a>
</div>
</div>
<div className="flex-1">
<form
onSubmit={form.handleSubmit(handleFormSubmit)}
className="flex flex-col gap-6 rounded-xl bg-muted/50 p-8 md:p-10"
>
<div>
<h2 className="text-xl font-semibold">درخواست یا سؤال خود را بنویسید</h2>
<p className="text-sm text-muted-foreground">
پاسخ در روزهای کاری، معمولاً کمتر از ۲۴ ساعت
</p>
</div>
{isSubmitted && (
<div
className={cn(
"rounded-lg border border-green-500/20 bg-green-500/10 p-4 text-center transition-opacity duration-500",
showSuccess ? "opacity-100" : "opacity-0",
)}
>
<p className="text-sm font-medium text-green-600 dark:text-green-400">
پیام شما ثبت شد. کارشناس فروش به‌زودی پاسخ می‌دهد.
</p>
</div>
)}
<FieldGroup>
<div className="grid gap-4 sm:grid-cols-2">
<Controller
control={form.control}
name="firstName"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={field.name}>
نام <span className="text-destructive">*</span>
</FieldLabel>
<Input
{...field}
id={field.name}
aria-invalid={fieldState.invalid}
placeholder="علی"
className="bg-background"
/>
{fieldState.invalid && (
<FieldError errors={[fieldState.error]} />
)}
</Field>
)}
/>
<Controller
control={form.control}
name="lastName"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={field.name}>
نام خانوادگی{" "}
<span className="text-destructive">*</span>
</FieldLabel>
<Input
{...field}
id={field.name}
aria-invalid={fieldState.invalid}
placeholder="محمدی"
className="bg-background"
/>
{fieldState.invalid && (
<FieldError errors={[fieldState.error]} />
)}
</Field>
)}
/>
</div>
<Controller
control={form.control}
name="email"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={field.name}>
ایمیل <span className="text-destructive">*</span>
</FieldLabel>
<Input
{...field}
id={field.name}
type="email"
aria-invalid={fieldState.invalid}
placeholder="example@email.com"
dir="rtl"
className="bg-background text-start"
/>
{fieldState.invalid && (
<FieldError errors={[fieldState.error]} />
)}
</Field>
)}
/>
<Controller
control={form.control}
name="subject"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={field.name}>
موضوع <span className="text-destructive">*</span>
</FieldLabel>
<Input
{...field}
id={field.name}
aria-invalid={fieldState.invalid}
placeholder="مثلاً درخواست دمو یا مشاوره پلن"
className="bg-background"
/>
{fieldState.invalid && (
<FieldError errors={[fieldState.error]} />
)}
</Field>
)}
/>
<Controller
control={form.control}
name="message"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel htmlFor={field.name}>
پیام <span className="text-destructive">*</span>
</FieldLabel>
<Textarea
{...field}
id={field.name}
aria-invalid={fieldState.invalid}
placeholder="اندازه تیم فروش، ابزار فعلی و هدف‌تان را کوتاه بنویسید."
rows={4}
className="bg-background"
/>
{fieldState.invalid && (
<FieldError errors={[fieldState.error]} />
)}
</Field>
)}
/>
{form.formState.errors.root && (
<p className="text-sm text-destructive">
{form.formState.errors.root.message}
</p>
)}
<Button
size="lg"
className="w-full gap-2"
disabled={form.formState.isSubmitting}
>
{form.formState.isSubmitting ? (
<>
<LoaderIcon className="size-4 animate-spin" />
در حال ارسال...
</>
) : (
"ارسال درخواست"
)}
</Button>
</FieldGroup>
</form>
</div>
</div>
</div>
</section>
);
};
export { Contact2 };