"use client"; import type React from "react"; import { useState } from "react"; import { Upload } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { useOpenStore } from "@/lib/hooks"; import { toast } from "sonner"; import { useTranslations } from "next-intl"; interface OpenStorePageProps { locale?: string; translations?: { title: string; firstName: string; lastName: string; email: string; phone: string; uploadPatent: string; submit: string; selectedFile: string; firstNameRequired: string; lastNameRequired: string; emailInvalid: string; phoneInvalid: string; fileRequired: string; fileSizeError: string; fileTypeError: string; }; } interface FormData { firstName: string; lastName: string; email: string; phone: string; file: File | null; } interface FormErrors { firstName?: string; lastName?: string; email?: string; phone?: string; file?: string; } export default function OpenStorePage({}: OpenStorePageProps) { const [formData, setFormData] = useState({ firstName: "", lastName: "", email: "", phone: "+993", file: null, }); const [errors, setErrors] = useState({}); const [fileName, setFileName] = useState(""); const { mutate: submitOpenStore, isPending: loading } = useOpenStore(); const t = useTranslations(); const validateForm = (): boolean => { const newErrors: FormErrors = {}; if (!formData.firstName.trim()) { newErrors.firstName = t("requiredField"); } if (!formData.lastName.trim()) { newErrors.lastName = t("requiredField"); } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(formData.email)) { newErrors.email = t("requiredField"); } const phoneRegex = /^\+?[0-9]{6,15}$/; if (!phoneRegex.test(formData.phone)) { newErrors.phone = t("requiredField"); } if (!formData.file) { newErrors.file = t("fileRequired"); } else { const allowedTypes = ["image/jpeg", "image/jpg", "application/pdf"]; if (!allowedTypes.includes(formData.file.type)) { newErrors.file = t("fileTypeError"); } if (formData.file.size > 25 * 1024 * 1024) { newErrors.file = t("fileSizeError"); } } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleInputChange = (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 handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setFormData((prev) => ({ ...prev, file })); setFileName(file.name); if (errors.file) { setErrors((prev) => ({ ...prev, file: undefined })); } } }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) return; if (formData.file) { submitOpenStore( { firstName: formData.firstName, lastName: formData.lastName, email: formData.email, phone: formData.phone, patentFile: formData.file, }, { onSuccess: () => { toast.success(t("submit_success")); setFormData({ firstName: "", lastName: "", email: "", phone: "+993", file: null, }); setFileName(""); }, onError: (error: any) => { toast.error(error?.message || t("submit_error")); }, } ); } }; return (
{t("title")} Заполните форму для подачи заявления
{/* First Name */}
{errors.firstName && (

{errors.firstName}

)}
{/* Last Name */}
{errors.lastName && (

{errors.lastName}

)}
{/* Email */}
{errors.email && (

{errors.email}

)}
{/* Phone */}
{errors.phone && (

{errors.phone}

)}
{/* File Upload */}
{fileName && (

{t("selectedFile")}: {fileName}

)} {errors.file && (

{errors.file}

)}
{/* Submit Button */}
); }