From 4472a23ebd918750688d61b9e85cec02a16557d8 Mon Sep 17 00:00:00 2001 From: oriofir <97814056+oriofir@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:14:12 -0700 Subject: [PATCH] added recaptcha to contact form --- .../sections/contact/form/index.tsx | 65 +++++++++++++++++-- .../sections/partners/contact/index.tsx | 53 +++++++-------- src/pages/_app.tsx | 39 ++++------- 3 files changed, 100 insertions(+), 57 deletions(-) diff --git a/src/components/sections/contact/form/index.tsx b/src/components/sections/contact/form/index.tsx index 397dfb6..0d4bf02 100644 --- a/src/components/sections/contact/form/index.tsx +++ b/src/components/sections/contact/form/index.tsx @@ -1,8 +1,10 @@ -import React, { useState } from 'react' +import React, { useCallback, useState } from 'react' // eslint-disable-next-line import/no-named-as-default +import ReCAPTCHA from 'react-google-recaptcha' +import { useGoogleReCaptcha } from 'react-google-recaptcha-v3' import Select from 'react-select' -import { Container } from '~/components/layout/container' +import * as container from '~/components/layout/container' import Section from '~/components/layout/section' import { Button } from '~/components/primitives/button' import Heading from '~/components/primitives/heading' @@ -40,8 +42,50 @@ const CustomForm = ({ data }: FormProps) => { const [errorMsg, setErrorMsg] = useState('') const [isFormSent, setIsFormSent] = useState(false) const [isSending, setIsSending] = useState(false) + const [notification, setNotification] = useState('') // const reRef = useRef() + const { executeRecaptcha } = useGoogleReCaptcha() + + function submitEnquiryForm(gReCaptchaToken: string) { + fetch('/api/inquiry', { + method: 'POST', + headers: { + Accept: 'application/json, text/plain, */*', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + gRecaptchaToken: gReCaptchaToken + }) + }) + .then((res) => res.json()) + .then((res) => { + // console.log(res, 'response from backend') + if (res?.status === 'success') { + setNotification(res?.message) + } else { + setNotification(res?.message) + } + }) + } + + const handleSubmitForm = useCallback( + (e: any) => { + e.preventDefault() + if (!executeRecaptcha) { + // eslint-disable-next-line no-console + console.log('Execute recaptcha not yet available') + return + } + executeRecaptcha('enquiryFormSubmit').then((gReCaptchaToken) => { + // eslint-disable-next-line no-console + console.log(gReCaptchaToken, 'response Google reCaptcha server') + submitEnquiryForm(gReCaptchaToken) + }) + }, + [executeRecaptcha] + ) + const handleSubmit = async (e: any) => { e.preventDefault() @@ -61,8 +105,6 @@ const CustomForm = ({ data }: FormProps) => { inquiry: selectedOption.value } - // const token = await reRef.current.executeAsync() - await fetch(`${process.env.NEXT_PUBLIC_API}/api/contact`, { method: 'POST', mode: 'no-cors', @@ -127,9 +169,20 @@ const CustomForm = ({ data }: FormProps) => { onChange={(e) => setText(e.target.value)} > + + + {notification &&

{notification}

} ) @@ -138,7 +191,7 @@ const CustomForm = ({ data }: FormProps) => { const Form = ({ data }: DataProps) => { return (
- +
{data?.formHeading} @@ -147,7 +200,7 @@ const Form = ({ data }: DataProps) => {
- +
) } diff --git a/src/components/sections/partners/contact/index.tsx b/src/components/sections/partners/contact/index.tsx index 28b6988..09b54df 100644 --- a/src/components/sections/partners/contact/index.tsx +++ b/src/components/sections/partners/contact/index.tsx @@ -1,6 +1,7 @@ import { useCallback, useState } from 'react' // eslint-disable-next-line import/no-named-as-default import ReCAPTCHA from 'react-google-recaptcha' +// eslint-disable-next-line import/no-unresolved import { useGoogleReCaptcha } from 'react-google-recaptcha-v3' import Select from 'react-select' @@ -57,12 +58,35 @@ const CustomForm = ({ data }: FormProps) => { const [errorMsg, setErrorMsg] = useState('') const [isFormSent, setIsFormSent] = useState(false) const [isSending, setIsSending] = useState(false) + const [notification, setNotification] = useState('') // const reRef = useRef() const { executeRecaptcha } = useGoogleReCaptcha() + function submitEnquiryForm(gReCaptchaToken: string) { + fetch('/api/inquiry', { + method: 'POST', + headers: { + Accept: 'application/json, text/plain, */*', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + gRecaptchaToken: gReCaptchaToken + }) + }) + .then((res) => res.json()) + .then((res) => { + // console.log(res, 'response from backend') + if (res?.status === 'success') { + setNotification(res?.message) + } else { + setNotification(res?.message) + } + }) + } + const handleSubmitForm = useCallback( - (e) => { + (e: any) => { e.preventDefault() if (!executeRecaptcha) { // eslint-disable-next-line no-console @@ -78,28 +102,6 @@ const CustomForm = ({ data }: FormProps) => { [executeRecaptcha] ) - const submitEnquiryForm = (gReCaptchaToken) => { - fetch('/api/enquiry', { - method: 'POST', - headers: { - Accept: 'application/json, text/plain, */*', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - gRecaptchaToken: gReCaptchaToken - }) - }) - .then((res) => res.json()) - .then((res) => { - console.log(res, 'response from backend') - if (res?.status === 'success') { - setNotification(res?.message) - } else { - setNotification(res?.message) - } - }) - } - async function handleSubmit(e: any) { e.preventDefault() @@ -244,10 +246,12 @@ const CustomForm = ({ data }: FormProps) => { + {notification &&

{notification}

} ) @@ -280,6 +284,3 @@ const Contact = ({ data }: DataProps) => { } export default Contact -function submitEnquiryForm(gReCaptchaToken: string) { - throw new Error('Function not implemented.') -} diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 80b0334..07deea1 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -5,7 +5,6 @@ import { AppProps } from 'next/app' import { RealViewportProvider } from 'next-real-viewport' import { ThemeProvider } from 'next-themes' import * as React from 'react' -import { GoogleReCaptchaProvider } from 'react-google-recaptcha-v3' import { QueryClient, QueryClientProvider } from 'react-query' // import { Footer } from '~/components/common/footer' @@ -60,30 +59,20 @@ const App = ({ Component, pageProps, ...rest }: AppProps) => { (({ Component, pageProps }) => ) return ( - - - - - - - - - {/*
*/} - {getLayout({ Component, pageProps, ...rest })} - {/*