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
+ )
3
15
4
16
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 ( / \. r u $ / ) . test ( value ) ) {
61
+ setErrors ( { ...errors , email : true } )
62
+ } else {
63
+ setErrors ( { ...errors , email : false } )
64
+ }
65
+ }
66
+ setFormvalues ( { ...formValues , [ name ] : value } )
67
+ }
68
+
5
69
return (
6
- < form action = "https://api.formik.com/submit/codebrahma/contact" method = "post" >
70
+ < form onSubmit = { onSubmit } >
7
71
< input
8
72
type = "hidden"
9
73
name = "_next"
@@ -23,7 +87,10 @@ const ContactForm = ({ referrer }) => {
23
87
borderWidth = { 0 }
24
88
borderRadius = { 3 }
25
89
placeholder = "& #128172 ; Tell us about your idea "
90
+ value = { formValues . msg }
91
+ onChange = { onChange }
26
92
/>
93
+ { errors . msg && < Error > Please enter the message in English</ Error > }
27
94
</ Box >
28
95
< Box width = { [ 1 , 1 / 2 ] } mt = { 1 } >
29
96
< InputText
@@ -35,7 +102,17 @@ const ContactForm = ({ referrer }) => {
35
102
borderWidth = { 0 }
36
103
borderRadius = { 3 }
37
104
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 ) }
38
114
/>
115
+ { ! isCaptchaVerified && errors . captcha && < Error > Please verify you are not a robot</ Error > }
39
116
</ Box >
40
117
< Box width = { [ 1 , 1 / 3 ] } mt = { 1 } >
41
118
< OutlinedButton borderRadius = { 3 } >
0 commit comments