Skip to content

Commit e5ee08d

Browse files
authored
Recaptcha (#72)
* added recaptcha in contact form * added process link to header * contact from validation
1 parent 86154bf commit e5ee08d

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"prop-types": "^15.7.2",
3939
"react": "^16.8.6",
4040
"react-dom": "^16.8.6",
41+
"react-google-recaptcha": "^2.0.1",
4142
"react-helmet": "^5.2.1",
4243
"styled-system": "^5.0.5"
4344
},

src/components/contactForm.js

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,73 @@
1-
import React from "react"
2-
import { Flex, Box, InputText, OutlinedButton } from "bricks"
1+
import React, { useState } from "react";
2+
import ReCAPTCHA from "react-google-recaptcha";
3+
import { Flex, Box, InputText, OutlinedButton, P } from "bricks";
4+
import { navigate } from "gatsby";
5+
import styled from "@emotion/styled";
6+
import { css } from 'bricks';
7+
8+
const Error = styled('p')(
9+
css({
10+
color: 'red',
11+
fontSize: 0,
12+
margin: '2px 0',
13+
})
14+
)
315

416
const ContactForm = ({ referrer }) => {
17+
const [isCaptchaVerified, setCaptchaVerified] = useState(false);
18+
const [formValues, setFormvalues] = useState({
19+
email: '',
20+
msg: '',
21+
})
22+
const [errors, setErrors] = useState({
23+
msg: false,
24+
email: false,
25+
captcha: false,
26+
})
27+
28+
const onSubmit = (e) => {
29+
e.preventDefault();
30+
31+
if (!isCaptchaVerified) {
32+
setErrors({ ...errors, captcha: true})
33+
} else {
34+
setErrors({
35+
msg: false,
36+
email: false,
37+
captcha: false,
38+
});
39+
40+
let xhttp = new XMLHttpRequest();
41+
xhttp.onreadystatechange = function() {
42+
if (this.readyState === 4 && this.status === 200) {
43+
navigate('/thank-you')
44+
}
45+
};
46+
xhttp.open("POST", "https://api.formik.com/submit/codebrahma/contact", true);
47+
xhttp.setRequestHeader("Content-Type", "application/json");
48+
xhttp.send(JSON.stringify(formValues));
49+
}
50+
}
51+
52+
const onChange = ({target: { name, value }}) => {
53+
if (name === 'msg') {
54+
if (new RegExp(/[^\u0000-\u007F]+/).test(value)) {
55+
setErrors({ ...errors, msg: true})
56+
} else {
57+
setErrors({ ...errors, msg: false})
58+
}
59+
} else if (name === 'email') {
60+
if (new RegExp(/\.ru$/).test(value)) {
61+
setErrors({ ...errors, email: true})
62+
} else {
63+
setErrors({ ...errors, email: false})
64+
}
65+
}
66+
setFormvalues({ ...formValues, [name]: value})
67+
}
68+
569
return (
6-
<form action="https://api.formik.com/submit/codebrahma/contact" method="post">
70+
<form onSubmit={onSubmit}>
771
<input
872
type="hidden"
973
name="_next"
@@ -23,7 +87,10 @@ const ContactForm = ({ referrer }) => {
2387
borderWidth={0}
2488
borderRadius={3}
2589
placeholder="&#128172; Tell us about your idea"
90+
value={formValues.msg}
91+
onChange={onChange}
2692
/>
93+
{errors.msg && <Error>Please enter the message in English</Error>}
2794
</Box>
2895
<Box width={[1, 1 / 2]} mt={1}>
2996
<InputText
@@ -35,7 +102,17 @@ const ContactForm = ({ referrer }) => {
35102
borderWidth={0}
36103
borderRadius={3}
37104
placeholder="@ Email address"
105+
value={formValues.email}
106+
onChange={onChange}
107+
/>
108+
{errors.email && <Error>Please provide a valid email</Error>}
109+
</Box>
110+
<Box mt='1'>
111+
<ReCAPTCHA
112+
sitekey="6LeFW-8UAAAAABYFkKUu6fKNwYFL-p6JngDJV4jI"
113+
onChange={() => setCaptchaVerified(!isCaptchaVerified)}
38114
/>
115+
{!isCaptchaVerified && errors.captcha && <Error>Please verify you are not a robot</Error>}
39116
</Box>
40117
<Box width={[1, 1 / 3]} mt={1}>
41118
<OutlinedButton borderRadius={3}>

src/templates/header.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Link } from 'gatsby'
66

77
const links = [
88
{title: 'Work', link: '/work'},
9+
{title: 'Process', link: '/our-process'},
910
{title: 'FAQ', link: '/faq'},
1011
{title: 'Blog', link: '/blog'},
1112
{title: 'Contact', link: '/contact'},

0 commit comments

Comments
 (0)