'use client' import classNames from 'classnames' import React from 'react' import { RichText, Text, types } from 'react-bricks/rsc' import { LayoutProps } from '../../LayoutSideProps' import { textColors } from '../../colors' import Container from '../../shared/components/Container' import Section from '../../shared/components/Section' import { useReactBricksContext, useAdminContext, sendFormSubmission, } from 'react-bricks/rsc/client' import { GoogleReCaptchaProvider, useGoogleReCaptcha, } from 'react-google-recaptcha-v3' import { useForm } from 'react-hook-form' import { createSubmissionError, FormSubmissionError, } from '../../shared/FormNewsletter/NewsletterUtils' export interface NewsletterProps extends LayoutProps { resultOkText: string title: types.TextValue text: types.TextValue text2: types.TextValue buttonText: types.TextValue formId: string } const NewsletterSubscribeForm: React.FC<{ buttonText: types.TextValue formId: string resultOkText: string }> = ({ buttonText, formId, resultOkText }) => { const { executeRecaptcha } = useGoogleReCaptcha() const rbContext = useReactBricksContext() const { isAdmin } = useAdminContext() const { register, handleSubmit, formState: { errors, isSubmitting, isSubmitSuccessful }, setError, } = useForm() const onSubmit = async ({ email, ...data }: any) => { try { if (!executeRecaptcha) { throw createSubmissionError( 'recaptchaUnavailable', 'reCAPTCHA is not available. Please reload the page and try again.' ) } let token: string | undefined try { token = await executeRecaptcha('form_submit') } catch (err) { console.log(err) throw createSubmissionError( 'recaptchaExecution', 'Failed to execute reCAPTCHA. Please try again.', err ) } if (!token) { throw createSubmissionError( 'recaptchaToken', 'Failed to verify reCAPTCHA token. Please try again.' ) } let result: Awaited> try { result = await sendFormSubmission({ appId: rbContext.appId, appEnv: rbContext.environment, token, formId, emailAddress: email, data, fetchOptions: { apiPrefix: rbContext.apiPrefix }, }) } catch (err) { throw createSubmissionError( 'submissionNetwork', 'We were unable to submit your form. Please check your connection and try again.', err ) } if (!result.success) { const message = 'There was a problem sending your request. Please try again.' throw createSubmissionError('submissionFailed', message) } } catch (err) { const fallbackMessage = 'There was a problem sending your request. Please try again.' const submissionError = err instanceof Error ? (err as FormSubmissionError) : createSubmissionError('submission', fallbackMessage, err) if ( !(err instanceof Error) && submissionError.originalError === undefined ) { submissionError.originalError = err } const message = submissionError.message || fallbackMessage const type = submissionError.submissionType || 'submission' if (setError) { setError(`root.${type}` as any, { type, message, }) } throw submissionError } } return isSubmitSuccessful ? (
{resultOkText}
) : (
{errors.root && (
    {Object.values(errors.root).map((err) => { if (typeof err !== 'object') { return (
  • {err}
  • ) } const { type, message } = err return (
  • {message}
  • ) })}
)}
) } const Newsletter: React.FC = ({ backgroundColor, formId, resultOkText = `Thanks,you're all signed up!`, title, text, text2, buttonText, }) => { const reCaptchaKey = process.env.NEXT_PUBLIC_RECAPTCHA_KEY || '' return (
(

{props.children}

)} placeholder="type a title..." /> ( {props.children} )} />
(

{children}

)} />
) } export default Newsletter