From b26c3767cb2f0462b17f0f852d86d71f8fbbcbd4 Mon Sep 17 00:00:00 2001 From: nachat-ayoub Date: Thu, 20 Jun 2024 18:38:35 +0100 Subject: [PATCH] add Google ReCaptcha --- src/App.tsx | 76 +++++++++++++++++++------------------------------ src/useForm.tsx | 4 +-- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 496c140..391fce7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,19 +1,16 @@ -// import ReCAPTCHA from 'react-google-recaptcha'; +import ReCAPTCHA from 'react-google-recaptcha'; import { useState } from 'react'; import useForm from './useForm'; -// import axios from 'axios'; -type TStatus = 'success' | 'error' | null; +type TStatus = 'success' | 'error' | 'loading' | null; export default function App() { - const [email, setEmail] = useState(''); - const [message, setMessage] = useState(''); - const [sendStatus, setSendStatus] = useState(null); const [error, setError] = useState(null); + const [sendStatus, setSendStatus] = useState(null); + const [message, setMessage] = useState(''); + const [email, setEmail] = useState(''); const { sendEmail } = useForm(); - // const captchaRef = useRef(null); - async function handleSubmit(e: React.FormEvent) { e.preventDefault(); @@ -28,39 +25,11 @@ export default function App() { return; } + setSendStatus('loading'); + sendEmail(e, (error) => { - if (error) { - setSendStatus('error'); - } else { - setSendStatus('success'); - } + setSendStatus(error ? 'error' : 'success'); }); - - // if (captchaRef?.current && typeof captchaRef.current !== 'string') { - // const token = captchaRef.current.getValue(); - // if (!token) return setError('Please complete the captcha.'); - // captchaRef.current.reset(); - - // await axios - // .post('https://dummy-api-phi.vercel.app/api/reCaptcha', { token }) - // .then((res) => { - // if (res.status === 200) { - // sendEmail(e, (error) => { - // if (error) { - // setSendStatus('error'); - // } else { - // setSendStatus('success'); - // } - // }); - // } else { - // setError('Failed to send email. Please try again.'); - // } - // }) - // .catch((error) => { - // console.log(error); - // setError('Failed to send email. Please try again.'); - // }); - // } } return ( @@ -121,7 +90,10 @@ export default function App() { /> -