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