|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useCallback, useState } from "react" |
| 4 | +import { zodResolver } from "@hookform/resolvers/zod" |
| 5 | +import axios from "axios" |
| 6 | +import { AlertCircle, ArrowRight, Loader2, MailCheck } from "lucide-react" |
| 7 | +import { useGoogleReCaptcha } from "react-google-recaptcha-v3" |
| 8 | +import { useForm } from "react-hook-form" |
| 9 | +import * as z from "zod" |
| 10 | + |
| 11 | +import { Button } from "@/components/ui/button" |
| 12 | +import { |
| 13 | + Form, |
| 14 | + FormControl, |
| 15 | + FormField, |
| 16 | + FormItem, |
| 17 | + FormLabel, |
| 18 | + FormMessage, |
| 19 | +} from "@/components/ui/form" |
| 20 | +import { Input } from "@/components/ui/input" |
| 21 | + |
| 22 | +import { Alert, AlertDescription, AlertTitle } from "./ui/alert" |
| 23 | +import { Textarea } from "./ui/textarea" |
| 24 | + |
| 25 | +const formSchema = z.object({ |
| 26 | + "kurum-adi": z |
| 27 | + .string() |
| 28 | + .min(2, { |
| 29 | + message: "Kurum adı en az 2 karakter olmalıdır.", |
| 30 | + }) |
| 31 | + .max(255, { |
| 32 | + message: "Kurum adı en fazla 255 karakter olmalıdır.", |
| 33 | + }), |
| 34 | + name: z |
| 35 | + .string() |
| 36 | + .min(2, { |
| 37 | + message: "İsim en az 2 karakter olmalıdır.", |
| 38 | + }) |
| 39 | + .max(255, { |
| 40 | + message: "İsim en fazla 255 karakter olmalıdır.", |
| 41 | + }), |
| 42 | + email: z |
| 43 | + .string() |
| 44 | + .email({ |
| 45 | + message: "Geçerli bir e-posta adresi giriniz.", |
| 46 | + }) |
| 47 | + .max(255, { |
| 48 | + message: "E-posta adresi en fazla 255 karakter olmalıdır.", |
| 49 | + }), |
| 50 | + telefon: z |
| 51 | + .string() |
| 52 | + .min(2, { |
| 53 | + message: "Telefon en az 2 karakter olmalıdır.", |
| 54 | + }) |
| 55 | + .max(255, { |
| 56 | + message: "Telefon en fazla 255 karakter olmalıdır.", |
| 57 | + }), |
| 58 | + aciklama: z |
| 59 | + .string() |
| 60 | + .min(2, { |
| 61 | + message: "Açıklama en az 2 karakter olmalıdır.", |
| 62 | + }) |
| 63 | + .max(500, { |
| 64 | + message: "Açıklama en fazla 500 karakter olmalıdır.", |
| 65 | + }), |
| 66 | +}) |
| 67 | + |
| 68 | +export function ContactForm() { |
| 69 | + const { executeRecaptcha } = useGoogleReCaptcha() |
| 70 | + |
| 71 | + const [error, setError] = useState<boolean>(false) |
| 72 | + const [loading, setLoading] = useState<boolean>(false) |
| 73 | + const [message, setMessage] = useState<string>("") |
| 74 | + |
| 75 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 76 | + resolver: zodResolver(formSchema), |
| 77 | + defaultValues: { |
| 78 | + "kurum-adi": "", |
| 79 | + name: "", |
| 80 | + email: "", |
| 81 | + telefon: "", |
| 82 | + aciklama: "", |
| 83 | + }, |
| 84 | + }) |
| 85 | + |
| 86 | + const handleOnSubmit = useCallback( |
| 87 | + (values: z.infer<typeof formSchema>) => { |
| 88 | + setLoading(true) |
| 89 | + if (!executeRecaptcha) { |
| 90 | + setError(true) |
| 91 | + setMessage( |
| 92 | + "Bot kontrolü yapılırken bir hata oluştu. Biraz bekledikten sonra tekrar deneyin." |
| 93 | + ) |
| 94 | + |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + executeRecaptcha("subscribe_newsletter").then((token) => { |
| 99 | + submitForm(values, token) |
| 100 | + }) |
| 101 | + }, |
| 102 | + [executeRecaptcha] |
| 103 | + ) |
| 104 | + |
| 105 | + function submitForm(values: z.infer<typeof formSchema>, token: string) { |
| 106 | + axios |
| 107 | + .post<string>( |
| 108 | + "https://ws.aciklab.org/contact_liman", |
| 109 | + { |
| 110 | + ...values, |
| 111 | + token, |
| 112 | + action: "subscribe_newsletter", |
| 113 | + }, |
| 114 | + { |
| 115 | + headers: { |
| 116 | + "Content-Type": "multipart/form-data", |
| 117 | + }, |
| 118 | + } |
| 119 | + ) |
| 120 | + .then(({ status, data }) => { |
| 121 | + if (status != 200) { |
| 122 | + setError(true) |
| 123 | + } else { |
| 124 | + setError(false) |
| 125 | + } |
| 126 | + |
| 127 | + setMessage(data) |
| 128 | + }) |
| 129 | + .catch(() => { |
| 130 | + setError(true) |
| 131 | + setMessage("Bir hata oluştu. Lütfen tekrar deneyin.") |
| 132 | + }) |
| 133 | + .finally(() => { |
| 134 | + setLoading(false) |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + return ( |
| 139 | + <Form {...form}> |
| 140 | + <form |
| 141 | + onSubmit={form.handleSubmit(handleOnSubmit)} |
| 142 | + className="w-full space-y-8" |
| 143 | + > |
| 144 | + {message && ( |
| 145 | + <Alert variant={!error ? "default" : "destructive"}> |
| 146 | + {!error ? ( |
| 147 | + <MailCheck className="h-4 w-4" /> |
| 148 | + ) : ( |
| 149 | + <AlertCircle className="h-4 w-4" /> |
| 150 | + )} |
| 151 | + <AlertTitle>{!error ? "Bilgilendirme" : "Hata"}</AlertTitle> |
| 152 | + <AlertDescription>{message}</AlertDescription> |
| 153 | + </Alert> |
| 154 | + )} |
| 155 | + |
| 156 | + <div className="grid grid-cols-2 gap-5"> |
| 157 | + <FormField |
| 158 | + control={form.control} |
| 159 | + name="name" |
| 160 | + render={({ field }) => ( |
| 161 | + <FormItem> |
| 162 | + <FormLabel> |
| 163 | + İsim |
| 164 | + <span className="ml-1 font-bold text-red-500">*</span> |
| 165 | + </FormLabel> |
| 166 | + <FormControl> |
| 167 | + <Input placeholder="" {...field} /> |
| 168 | + </FormControl> |
| 169 | + <FormMessage /> |
| 170 | + </FormItem> |
| 171 | + )} |
| 172 | + /> |
| 173 | + |
| 174 | + <FormField |
| 175 | + control={form.control} |
| 176 | + name="email" |
| 177 | + render={({ field }) => ( |
| 178 | + <FormItem> |
| 179 | + <FormLabel> |
| 180 | + E-posta |
| 181 | + <span className="ml-1 font-bold text-red-500">*</span> |
| 182 | + </FormLabel> |
| 183 | + <FormControl> |
| 184 | + <Input placeholder="liman@aciklab.org" {...field} /> |
| 185 | + </FormControl> |
| 186 | + <FormMessage /> |
| 187 | + </FormItem> |
| 188 | + )} |
| 189 | + /> |
| 190 | + </div> |
| 191 | + |
| 192 | + <div className="grid grid-cols-2 gap-5"> |
| 193 | + <FormField |
| 194 | + control={form.control} |
| 195 | + name="kurum-adi" |
| 196 | + render={({ field }) => ( |
| 197 | + <FormItem> |
| 198 | + <FormLabel> |
| 199 | + Kurum Adı |
| 200 | + <span className="ml-1 font-bold text-red-500">*</span> |
| 201 | + </FormLabel> |
| 202 | + <FormControl> |
| 203 | + <Input placeholder="HAVELSAN A.Ş." {...field} /> |
| 204 | + </FormControl> |
| 205 | + <FormMessage /> |
| 206 | + </FormItem> |
| 207 | + )} |
| 208 | + /> |
| 209 | + |
| 210 | + <FormField |
| 211 | + control={form.control} |
| 212 | + name="telefon" |
| 213 | + render={({ field }) => ( |
| 214 | + <FormItem> |
| 215 | + <FormLabel> |
| 216 | + Telefon |
| 217 | + <span className="ml-1 font-bold text-red-500">*</span> |
| 218 | + </FormLabel> |
| 219 | + <FormControl> |
| 220 | + <Input placeholder="+90 500 000 00 00" {...field} /> |
| 221 | + </FormControl> |
| 222 | + <FormMessage /> |
| 223 | + </FormItem> |
| 224 | + )} |
| 225 | + /> |
| 226 | + </div> |
| 227 | + |
| 228 | + <FormField |
| 229 | + control={form.control} |
| 230 | + name="aciklama" |
| 231 | + render={({ field }) => ( |
| 232 | + <FormItem> |
| 233 | + <FormLabel> |
| 234 | + Açıklama<span className="ml-1 font-bold text-red-500">*</span> |
| 235 | + </FormLabel> |
| 236 | + <FormControl> |
| 237 | + <Textarea |
| 238 | + placeholder="İhtiyacınızı ve isteklerinizi belirtebilirsiniz..." |
| 239 | + {...field} |
| 240 | + /> |
| 241 | + </FormControl> |
| 242 | + <FormMessage /> |
| 243 | + </FormItem> |
| 244 | + )} |
| 245 | + /> |
| 246 | + |
| 247 | + <Button type="submit" disabled={loading}> |
| 248 | + Gönder |
| 249 | + {!loading ? ( |
| 250 | + <ArrowRight className="ml-2 h-4 w-4" /> |
| 251 | + ) : ( |
| 252 | + <Loader2 className="ml-2 h-4 w-4 animate-spin" /> |
| 253 | + )} |
| 254 | + </Button> |
| 255 | + </form> |
| 256 | + <small className="mt-3 block w-full text-muted-foreground/70"> |
| 257 | + Bu iletişim formu reCAPTCHA from Google ile korunmaktadır. |
| 258 | + <br /> |
| 259 | + <a href="https://policies.google.com/privacy" className="mr-2"> |
| 260 | + Gizlilik Politikası |
| 261 | + </a> |
| 262 | + <a href="https://policies.google.com/terms">Hizmet Koşulları</a> |
| 263 | + </small> |
| 264 | + </Form> |
| 265 | + ) |
| 266 | +} |
0 commit comments