"use client"; import { apiInstance } from "@/shared/config/apiConfig"; import { Container, Title } from "@/shared/ui"; import Loader from "@/shared/ui/Loader/Loader"; import { zodResolver } from "@hookform/resolvers/zod"; import { AxiosError } from "axios"; import { useTranslations } from "next-intl"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "react-toastify"; import { z } from "zod"; export default function Contacts() { const t = useTranslations("contacts"); const [error, setError] = useState(""); const [loader, setLoader] = useState(false); const appealFormScheme = z.object({ first_name: z.string(), last_name: z.string(), email: z.string(), phone_number: z.string(), message: z.string(), }); type FormFields = z.infer; const { register, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(appealFormScheme), }); const onSubmit = async (data: FormFields) => { try { setError(""); setLoader(true); const response = await apiInstance.post("/appeal_response/", data); console.log(response); toast.success(t("successMessage")); } catch (error) { if (error instanceof AxiosError) { if ( [500, 501, 502, 503, 504].includes(error.response?.status as number) ) { toast.error(t("serverError")); } } } finally { setLoader(false); } }; return (
<p className="text-grey-text">{t("address")}</p> </div> <div className="max-w-[384px] max-h-[106px] flex flex-col items-center px-7"> <Title text={t("phoneTitle")} className="font-extrabold mb-2" /> <p className="text-grey-text">(0312) 39 40 38</p> </div> <div className="max-w-[384px] max-h-[106px] flex flex-col items-center px-7"> <Title text="Email" className="font-extrabold mb-2" /> <p className="text-grey-text">kyrgyzstan@transparency.org</p> </div> </section> <form onSubmit={handleSubmit(onSubmit)} className="w-[480px] my-0 mx-auto" > <div className="flex items-center flex-col mt-[96px] mb-[96px]"> <h1 className="text-gray-900 text-[36px]">{t("title")}</h1> <div className="flex flex-col items-center gap-6 mt-[64px]"> <div className="flex gap-8 w-full"> <div className="w-[224px] flex flex-col"> <label htmlFor="firstName" className="mb-[6px]"> {t("name")} </label> <input type="text" className="w-full h-[48px] bg-white py-3 px-4 rounded-md shadow-sm border border-gray" placeholder={t("name")} id="firstName" required {...register("first_name", { required: true })} /> </div> <div className="w-[224px] flex flex-col"> <label htmlFor="lastName" className="mb-[6px]"> {t("surname")} </label> <input type="text" className="w-full h-[48px] bg-white py-3 px-4 rounded-md shadow-sm border border-gray" placeholder={t("surname")} id="lastName" required {...register("last_name", { required: true })} /> </div> </div> <div className="w-full"> <label htmlFor="email" className="mb-[6px]"> Email </label> <input type="text" placeholder="user@gmail.com" className="w-full h-[48px] bg-white py-3 px-4 rounded-md shadow-sm border border-gray" id="email" {...register("email", { required: true })} /> </div> <div className="w-full"> <label htmlFor="phoneNumber" className="mb-[6px]"> {t("phone")} </label> <input type="text" className="w-full h-[48px] bg-white py-3 px-4 rounded-md shadow-sm border border-gray" placeholder="+996" id="phoneNumber" required {...register("phone_number", { required: true })} /> </div> <div className="w-full"> <label htmlFor="message" className="mb-[6px]"> {t("message")} </label> <textarea className="h-[120px] resize-none w-full bg-white py-3 px-4 rounded-md shadow-sm border border-gray" id="message" required {...register("message", { required: true })} ></textarea> </div> <div className="w-full"> <input className="border-none" type="checkbox" id="consentCheckbox" name="consent" required /> <label htmlFor="consentCheckbox" className="text-[#667085] ml-[10px]" > {t("checkbox")} </label> </div> {errors.root && ( <p className="text-sm leading-5 text-red-500"> {errors.root.message} </p> )} {error ? ( <p className="text-red-500 leading-5 text-sm">{error}</p> ) : null} <div className="w-full"> <button type="submit" className="bg-blue text-white w-full h-[48px] rounded-md " > {loader ? <Loader /> : t("send")} </button> </div> </div> </div> </form> </Container> </section> ); }