-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact.js
126 lines (117 loc) · 3.82 KB
/
Contact.js
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
/**
* Copyright Vlad Shamgin (c) 2019
* Alphaux Lightning Hints
* Contact.js component
*
* @summary short description for the file
* @author Vlad Shamgin <[email protected]>
*/
import React, { Component } from 'react';
import ReactGA from 'react-ga';
import '../styles/Contact.css';
class Contact extends Component {
constructor(props) {
super(props);
this.state = {
isSubmitDisabled: false,
buttonSubmitVisibility: 'button-submit-visible',
thankYouTextVisibility: 'text-thankyou-hidden'
};
this.subjectFormRef = React.createRef();
this.textFormRef = React.createRef();
this.handleFormSubmit = this.handleFormSubmit.bind(this);
this.handleCancel = this.handleCancel.bind(this);
}
handleFormSubmit(e) {
e.preventDefault();
ReactGA.event({
category: 'Contact',
action: 'Submit attempt'
});
const gatewayUrl = 'https://myapi';/** */
if(
!this.validate(this.subjectFormRef.current.value) ||
!this.validate(this.textFormRef.current.value)) {
return;
}
const formData = JSON.stringify({
subject: this.subjectFormRef.current.value,
message: this.textFormRef.current.value
});
this.setState({
isSubmitDisabled: true
});
fetch(gatewayUrl, {
method: "POST",
mode: 'no-cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: formData
}
).then((res) => {
this.disableControls();
}).then(()=>{
ReactGA.event({
category: 'Contact',
action: 'Submit successful'
});
}).catch(error => this.disableControls());
}
handleCancel(e) {
e.preventDefault();
ReactGA.event({
category: 'Contact',
action: 'Cancelled'
});
this.props.onCancelCallback();
}
disableControls(){
this.setState({
buttonSubmitVisibility: 'button-submit-hidden',
thankYouTextVisibility: 'text-thankyou-visible'
});
}
validate = (str) => {
if(!str || str === ''){
return false;
}
const trimmedString = str.replace(/^\s+|\s+$/g,'');
if(trimmedString === '') {
return false;
}
return true;
}
render() {
return (
<div className="container-content">
<h2>Contact us</h2>
<p>To contact us, submit your hint, suggest a new category please use the form below.</p>
<form onSubmit={this.handleFormSubmit}>
<label>Subject / Topic: </label>
<input
maxLength="512"
type="text"
ref={this.subjectFormRef}
required
>
</input>
<br />
<label>
Suggest your Hint / Make comment, etc:
</label>
<textarea maxLength="2000" ref={this.textFormRef} required />
<span className={this.state.thankYouTextVisibility}>Thank you! You may close the form now</span>
<input
disabled={this.state.isSubmitDisabled}
className={this.state.buttonSubmitVisibility}
type="submit"
value="Submit" />
<button className="btn-active" onClick={this.handleCancel}>Close</button>
</form>
</div>
);
}
}
export default Contact;