forked from coderplex-org/coderplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.js
128 lines (122 loc) · 3.78 KB
/
subscribe.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
127
128
import React, { Component } from 'react';
import { Form, Message } from 'semantic-ui-react';
import { baseEventsURL, subscribeURL } from '../utils/urls';
class Subscribe extends Component {
state = {
subscribersEmail: '',
submittingEmail: false,
emailSubmittingError: '',
subscriberEmailPosted: false,
};
handleChange = event => {
this.setState({
subscribersEmail: event.target.value,
emailSubmittingError: '',
});
};
handleSubmit = () => {
this.setState({ emailSubmittingError: '' });
const emailRegx = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const email = this.state.subscribersEmail;
if (!email) {
this.setState({
emailSubmittingError: 'Please enter a email',
});
return;
}
if (!emailRegx.test(email)) {
this.setState({
emailSubmittingError: 'Please enter a valid email',
});
return;
}
this.postSubscriberEmail(email);
};
async postSubscriberEmail(subscribersEmail) {
await this.setState({ submittingEmail: true });
const postSubscriberEmailRequest = await fetch(
`${baseEventsURL}${subscribeURL}`,
{
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: subscribersEmail }),
},
);
if (postSubscriberEmailRequest.status === 200) {
this.setState({
subscriberEmailPosted: true,
submittingEmail: false,
emailSubmittingError: '',
});
} else {
this.setState({
submittingEmail: false,
emailSubmittingError: 'Submission Failed Try Again.',
});
}
}
render() {
const hasError = this.state.emailSubmittingError !== '';
return (
<div>
<section className="update">
<div className="container update_container">
<h3 className="taglines">
We are constanly updating our platform.<br />If you would like to
stay informed about our updates, drop you email.
</h3>
<div className="update_content">
{this.state.subscriberEmailPosted ? (
<h2>Thank you, we will keep you posted</h2>
) : (
<Form onSubmit={this.handleSubmit} error={hasError}>
<Form.Group>
<Form.Input
placeholder="Enter email address"
name="email"
value={this.state.subscribersEmail}
onChange={this.handleChange}
disabled={this.state.submittingEmail}
/>
<Form.Button
loading={this.state.submittingEmail}
color="pink"
content="Subscribe"
/>
</Form.Group>
<Message
error
header="Action Forbidden"
content={this.state.emailSubmittingError}
/>
</Form>
)}
</div>
</div>
</section>
<style jsx>{`
.taglines {
padding-bottom: 20px;
}
.update_container {
background-color: #f6f6f6 !important;
}
.update_content {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: center;
align-items: center;
}
.container {
background-color: #ffffff;
text-align: center;
padding: 60px;
}
`}</style>
</div>
);
}
}
export default Subscribe;