"use client"; import api from "@/service/api"; import HCaptcha from "@hcaptcha/react-hcaptcha"; import { useRef, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "react-toastify"; import InputGroup from "../_components/InputGroup"; import Loading from "../_components/Loading"; import TextareaGroup from "../_components/Textarea"; type TContactUsForm = { full_name: string; email: string; phone: string; message: string; }; interface IProps { columnsPerRow?: 1 | 2 | 3 | 4; buttonText?: string; } const ContactUsForm = ({ buttonText = "Submit", columnsPerRow = 1, }: IProps) => { const [isLoading, setIsLoading] = useState(false); const [hcaptchaToken, setHcaptchaToken] = useState(null); const captchaRef = useRef(null); const { register, handleSubmit, formState: { errors }, reset, } = useForm(); const onCaptchaVerify = (token: string) => { setHcaptchaToken(token); }; const onCaptchaExpire = () => { setHcaptchaToken(null); }; const onSubmit = async (data: TContactUsForm) => { if (!hcaptchaToken) { toast.error("Please complete the captcha verification"); return; } setIsLoading(() => true); try { const response = ( await api.post("contacts", { ...data, hcaptcha_token: hcaptchaToken }) ).data; toast.success(response.message); reset(); setHcaptchaToken(null); captchaRef.current?.resetCaptcha(); } catch (error) { console.log(error); toast.error("Something went wrong"); throw error; } finally { setIsLoading(() => false); } }; const gridColsClass = { 1: "lg:grid-cols-1", 2: "lg:grid-cols-2", 3: "lg:grid-cols-3", 4: "lg:grid-cols-4", }[columnsPerRow]; const colSpanClass = { 1: "lg:col-span-1", 2: "lg:col-span-2", 3: "lg:col-span-3", 4: "lg:col-span-4", }[columnsPerRow]; return (
); }; export default ContactUsForm;