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