|
| 1 | +import { useForm } from 'react-hook-form'; |
| 2 | +import { useRef } from 'react'; |
| 3 | +import MailchimpSubscribe from 'react-mailchimp-subscribe'; |
| 4 | +import ReCAPTCHA from 'react-google-recaptcha'; |
| 5 | +import Container from '@/components/containers/Container'; |
| 6 | +import contactUsFormStyles from '@/styles/Contact.module.scss'; |
| 7 | +import RevealContentContainer from '@/components/containers/RevealContentContainer'; |
| 8 | +import SubmitButton from '@/components/buttons/SubmitButton'; |
| 9 | + |
| 10 | +export const ContactUsFormSubscribe = ({ setMsg }) => { |
| 11 | + return ( |
| 12 | + <MailchimpSubscribe |
| 13 | + url={process.env.NEXT_PUBLIC_MAILCHIMP_URL} |
| 14 | + render={({ subscribe, status, message }) => { |
| 15 | + console.info(`MailChimp (contact form): ${status} - ${message}`); |
| 16 | + return ( |
| 17 | + <> |
| 18 | + <ContactUsForm |
| 19 | + subscribe={formData => subscribe(formData)} |
| 20 | + setResponseMessage={setMsg} |
| 21 | + /> |
| 22 | + {status === 'error' && ( |
| 23 | + <div |
| 24 | + className={contactUsFormStyles.contact__respseonErrorMessage} |
| 25 | + > |
| 26 | + {`Newsletter subscription error: ${message}`} |
| 27 | + </div> |
| 28 | + )} |
| 29 | + </> |
| 30 | + ); |
| 31 | + }} |
| 32 | + /> |
| 33 | + ); |
| 34 | +}; |
| 35 | + |
| 36 | +function ContactUsForm({ subscribe, setResponseMessage }) { |
| 37 | + const contactReCaptchaRef = useRef(); |
| 38 | + |
| 39 | + const { |
| 40 | + register, |
| 41 | + handleSubmit, |
| 42 | + reset, |
| 43 | + formState: { errors, isSubmitting }, |
| 44 | + } = useForm({ |
| 45 | + defaultValues: { |
| 46 | + Name: '', |
| 47 | + Email: '', |
| 48 | + Subject: '', |
| 49 | + Message: '', |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + async function onSubmit(data) { |
| 54 | + setResponseMessage(['Submitting...']); |
| 55 | + |
| 56 | + contactReCaptchaRef.current.reset(); |
| 57 | + const gReCaptchaToken = await contactReCaptchaRef.current.executeAsync(); |
| 58 | + |
| 59 | + const res = await fetch('/api/contact', { |
| 60 | + method: 'POST', |
| 61 | + headers: { |
| 62 | + 'Content-Type': 'application/json', |
| 63 | + }, |
| 64 | + body: JSON.stringify({ |
| 65 | + name: data.Name, |
| 66 | + email: data.Email, |
| 67 | + subject: data.Subject, |
| 68 | + message: data.Message, |
| 69 | + subscribe: data.Subscribe, |
| 70 | + gReCaptchaToken, |
| 71 | + }), |
| 72 | + }); |
| 73 | + |
| 74 | + if (res.ok) { |
| 75 | + setResponseMessage([ |
| 76 | + 'Your message was sent successfully. We will be in touch with you as soon as possible.', |
| 77 | + ]); |
| 78 | + } else { |
| 79 | + const jsonRes = await res.json(); |
| 80 | + setResponseMessage([ |
| 81 | + 'Error Submitting Message', |
| 82 | + `Status Code: ${res.status} - ${jsonRes.message}`, |
| 83 | + 'Please contact support at hello@webdevpath.co', |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + if (data.Subscribe) { |
| 88 | + subscribe({ EMAIL: data.Email }); |
| 89 | + } |
| 90 | + reset(); |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <RevealContentContainer> |
| 95 | + <Container> |
| 96 | + <form |
| 97 | + onSubmit={handleSubmit(onSubmit)} |
| 98 | + className={contactUsFormStyles.contact__form} |
| 99 | + > |
| 100 | + <input |
| 101 | + type='text' |
| 102 | + placeholder='name' |
| 103 | + {...register('Name', { |
| 104 | + required: true, |
| 105 | + minLength: 2, |
| 106 | + maxLength: 80, |
| 107 | + //no white space pattern |
| 108 | + pattern: /[^\s-]/i, |
| 109 | + })} |
| 110 | + className={`${contactUsFormStyles.contact__input} ${contactUsFormStyles.contact__name}`} |
| 111 | + /> |
| 112 | + <p className={contactUsFormStyles.contact__errorMessage}> |
| 113 | + {errors.Name?.type === 'required' |
| 114 | + ? 'Name is required' |
| 115 | + : errors.Name?.type === 'pattern' |
| 116 | + ? 'No whitespace' |
| 117 | + : errors.Name?.type === 'minLength' |
| 118 | + ? 'Must be more than 1 character' |
| 119 | + : undefined} |
| 120 | + </p> |
| 121 | + <input |
| 122 | + type='email' |
| 123 | + placeholder='email' |
| 124 | + {...register('Email', { |
| 125 | + required: true, |
| 126 | + pattern: /^\S+@\S+$/i, |
| 127 | + })} |
| 128 | + className={`${contactUsFormStyles.contact__input} ${contactUsFormStyles.contact__email}`} |
| 129 | + /> |
| 130 | + <p className={contactUsFormStyles.contact__errorMessage}> |
| 131 | + {errors.Email?.type === 'required' && 'Email is required'} |
| 132 | + </p> |
| 133 | + <input |
| 134 | + type='text' |
| 135 | + placeholder='subject' |
| 136 | + {...register('Subject', { |
| 137 | + required: true, |
| 138 | + minLength: 2, |
| 139 | + pattern: /[^\s-]/i, |
| 140 | + })} |
| 141 | + className={`${contactUsFormStyles.contact__input} ${contactUsFormStyles.contact__subject}`} |
| 142 | + /> |
| 143 | + <p className={contactUsFormStyles.contact__errorMessage}> |
| 144 | + {errors.Subject?.type === 'required' |
| 145 | + ? 'Subject is required' |
| 146 | + : errors.Subject?.type === 'pattern' |
| 147 | + ? 'No whitespace' |
| 148 | + : errors.Subject?.type === 'minLength' |
| 149 | + ? 'Must be more than 1 character' |
| 150 | + : undefined} |
| 151 | + </p> |
| 152 | + <textarea |
| 153 | + {...register('Message', { |
| 154 | + required: true, |
| 155 | + minLength: 2, |
| 156 | + pattern: /[^\s-]/i, |
| 157 | + })} |
| 158 | + placeholder='Write your message here' |
| 159 | + className={`${contactUsFormStyles.contact__input} ${contactUsFormStyles.contact__message}`} |
| 160 | + /> |
| 161 | + <p className={contactUsFormStyles.contact__errorMessage}> |
| 162 | + {errors.Message?.type === 'required' |
| 163 | + ? 'Message is required' |
| 164 | + : errors.Message?.type === 'pattern' |
| 165 | + ? 'No whitespace' |
| 166 | + : errors.Message?.type === 'minLength' |
| 167 | + ? 'Must be more than 1 character' |
| 168 | + : undefined} |
| 169 | + </p> |
| 170 | + <div className={contactUsFormStyles.contact__subscribe}> |
| 171 | + <input |
| 172 | + className={contactUsFormStyles.contact__subscribeInput} |
| 173 | + type='checkbox' |
| 174 | + placeholder='Subscribe to our DevNews!' |
| 175 | + {...register('Subscribe', {})} |
| 176 | + /> |
| 177 | + Subscribe to our DevNews! |
| 178 | + </div> |
| 179 | + <SubmitButton label='Submit' disabled={isSubmitting} /> |
| 180 | + |
| 181 | + <ReCAPTCHA |
| 182 | + ref={contactReCaptchaRef} |
| 183 | + size='invisible' |
| 184 | + sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY} |
| 185 | + /> |
| 186 | + </form> |
| 187 | + </Container> |
| 188 | + </RevealContentContainer> |
| 189 | + ); |
| 190 | +} |
0 commit comments