'use client' import { useContext } from 'react' // import { useSubmit } from '@formspree/react' import { FormBuilderContext } from './FormBuilderProvider' import { useGoogleReCaptcha } from 'react-google-recaptcha-v3' import { useReactBricksContext, sendFormSubmission, } from 'react-bricks/rsc/client' import { createSubmissionError, FormSubmissionError, } from '../../shared/FormNewsletter/NewsletterUtils' export interface FormBuilderClientProps { formId: string successMessage: string children: any } const FormBuilderClient: React.FC = ({ formId, successMessage, children, }) => { const { register, setError, handleSubmit, errors, isSubmitSuccessful } = useContext(FormBuilderContext) const { executeRecaptcha } = useGoogleReCaptcha() const rbContext = useReactBricksContext() // const onSubmit = useSubmit(formspreeFormId, { // onError(errs) { // const formErrs = errs.getFormErrors() // for (const { code, message } of formErrs) { // setError && // setError(`root.${code}`, { // type: code, // message, // }) // } // const fieldErrs = errs.getAllFieldErrors() // for (const [field, errs] of fieldErrs) { // setError && // setError(field, { // message: errs.map((e) => e.message).join(', '), // }) // } // }, // }) 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 } } console.log('SUBMITTED - ', formId) if (!register || !handleSubmit) { return null } return ( <> {isSubmitSuccessful ? (

{successMessage}

) : (
{children} {errors && errors.root && (
    {Object.values(errors.root).map((err) => { if (typeof err !== 'object') { return (
  • {err}
  • ) } const { type, message } = err return (
  • {message}
  • ) })}
)}
)} ) } export default FormBuilderClient