|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { Form, Message } from 'semantic-ui-react'; |
| 3 | +import { baseEventsURL, subscribeURL } from '../utils/urls'; |
| 4 | + |
| 5 | +class Subscribe extends Component { |
| 6 | + state = { |
| 7 | + subscribersEmail: '', |
| 8 | + submittingEmail: false, |
| 9 | + emailSubmittingError: '', |
| 10 | + subscriberEmailPosted: false, |
| 11 | + }; |
| 12 | + |
| 13 | + handleChange = event => { |
| 14 | + this.setState({ |
| 15 | + subscribersEmail: event.target.value, |
| 16 | + emailSubmittingError: '', |
| 17 | + }); |
| 18 | + }; |
| 19 | + |
| 20 | + handleSubmit = () => { |
| 21 | + this.setState({ emailSubmittingError: '' }); |
| 22 | + 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,}))$/; |
| 23 | + const email = this.state.subscribersEmail; |
| 24 | + if (!email) { |
| 25 | + this.setState({ |
| 26 | + emailSubmittingError: 'Please enter a email', |
| 27 | + }); |
| 28 | + return; |
| 29 | + } |
| 30 | + if (!emailRegx.test(email)) { |
| 31 | + this.setState({ |
| 32 | + emailSubmittingError: 'Please enter a valid email', |
| 33 | + }); |
| 34 | + return; |
| 35 | + } |
| 36 | + this.postSubscriberEmail(email); |
| 37 | + }; |
| 38 | + |
| 39 | + async postSubscriberEmail(subscribersEmail) { |
| 40 | + await this.setState({ submittingEmail: true }); |
| 41 | + const postSubscriberEmailRequest = await fetch( |
| 42 | + `${baseEventsURL}${subscribeURL}`, |
| 43 | + { |
| 44 | + method: 'post', |
| 45 | + headers: { 'Content-Type': 'application/json' }, |
| 46 | + body: JSON.stringify({ email: subscribersEmail }), |
| 47 | + }, |
| 48 | + ); |
| 49 | + if (postSubscriberEmailRequest.status === 200) { |
| 50 | + this.setState({ |
| 51 | + subscriberEmailPosted: true, |
| 52 | + submittingEmail: false, |
| 53 | + emailSubmittingError: '', |
| 54 | + }); |
| 55 | + } else { |
| 56 | + this.setState({ |
| 57 | + submittingEmail: false, |
| 58 | + emailSubmittingError: 'Submission Failed Try Again.', |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + render() { |
| 63 | + const hasError = this.state.emailSubmittingError !== ''; |
| 64 | + |
| 65 | + return ( |
| 66 | + <div> |
| 67 | + <section className="update"> |
| 68 | + <div className="container update_container"> |
| 69 | + <h3 className="taglines"> |
| 70 | + We are constanly updating our platform.<br />If you would like to |
| 71 | + stay informed about our updates, drop you email. |
| 72 | + </h3> |
| 73 | + <div className="update_content"> |
| 74 | + {this.state.subscriberEmailPosted ? ( |
| 75 | + <h2>Thank you, we will keep you posted</h2> |
| 76 | + ) : ( |
| 77 | + <Form onSubmit={this.handleSubmit} error={hasError}> |
| 78 | + <Form.Group> |
| 79 | + <Form.Input |
| 80 | + placeholder="Enter email address" |
| 81 | + name="email" |
| 82 | + value={this.state.subscribersEmail} |
| 83 | + onChange={this.handleChange} |
| 84 | + disabled={this.state.submittingEmail} |
| 85 | + /> |
| 86 | + <Form.Button |
| 87 | + loading={this.state.submittingEmail} |
| 88 | + color="pink" |
| 89 | + content="Subscribe" |
| 90 | + /> |
| 91 | + </Form.Group> |
| 92 | + <Message |
| 93 | + error |
| 94 | + header="Action Forbidden" |
| 95 | + content={this.state.emailSubmittingError} |
| 96 | + /> |
| 97 | + </Form> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + </section> |
| 102 | + <style jsx>{` |
| 103 | + .taglines { |
| 104 | + padding-bottom: 20px; |
| 105 | + } |
| 106 | + .update_container { |
| 107 | + background-color: #f6f6f6 !important; |
| 108 | + } |
| 109 | + .update_content { |
| 110 | + display: flex; |
| 111 | + flex-direction: row; |
| 112 | + flex-wrap: wrap; |
| 113 | + justify-content: center; |
| 114 | + align-content: center; |
| 115 | + align-items: center; |
| 116 | + } |
| 117 | + .container { |
| 118 | + background-color: #ffffff; |
| 119 | + text-align: center; |
| 120 | + padding: 60px; |
| 121 | + } |
| 122 | + `}</style> |
| 123 | + </div> |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +export default Subscribe; |
0 commit comments