|
1 | | -import React, { Component } from 'react'; |
2 | | -import ReCAPTCHA from "react-google-recaptcha"; |
3 | | -import axios from 'axios'; |
4 | | - |
5 | | -class SubscribeButton extends Component { |
6 | | - constructor(props) { |
7 | | - super(props); |
8 | | - |
9 | | - this.state = { |
10 | | - subscribed: false, |
11 | | - }; |
12 | | - this._reCaptchaRef = React.createRef(); |
13 | | - } |
| 1 | +import React, { useEffect, useRef } from 'react'; |
14 | 2 |
|
| 3 | +const SubscribeButton = () => { |
| 4 | + const formRef = useRef(null); |
15 | 5 |
|
16 | | - subscribeNewsletters = async (e) => { |
17 | | - e.preventDefault(); |
18 | | - const CONTACT_LIST_API = 'https://staging.testsigma.com/api/website/contacts'; |
19 | | - const email = document.getElementById('email').value; |
20 | | - const emailError = document.getElementById('email_error'); |
21 | | - let trimmedURL = window.location.href; |
| 6 | + useEffect(() => { |
| 7 | + const script = document.createElement('script'); |
| 8 | + script.src = '//js.hsforms.net/forms/embed/v2.js'; |
| 9 | + script.charset = 'utf-8'; |
| 10 | + script.type = 'text/javascript'; |
| 11 | + script.async = true; |
| 12 | + document.body.appendChild(script); |
22 | 13 |
|
23 | | - if (trimmedURL.length > 250) |
24 | | - trimmedURL = window.location.href.substr(0, 240)+"..."; |
| 14 | + script.addEventListener('load', () => { |
| 15 | + if (window.hbspt && window.hbspt.forms) { |
| 16 | + window.hbspt.forms.create({ |
| 17 | + region: "na1", |
| 18 | + portalId: "23341221", |
| 19 | + formId: "87576ba1-bb40-433e-9257-4af82e747a6e", |
| 20 | + target: "#hubspot-form-wrapper", |
| 21 | + }); |
| 22 | + } |
| 23 | + }); |
25 | 24 |
|
26 | | - const regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; |
27 | | - |
28 | | - emailError.style.display = "none"; |
29 | | - if (!regex.test(email)) { |
30 | | - emailError.style.display = "block"; |
31 | | - return false; |
32 | | - } else { |
33 | | - this._reCaptchaRef.current.executeAsync().then(value => { |
34 | | - if (value) { |
35 | | - const formData = { |
36 | | - 'CATEGORY': 'Product Updates', |
37 | | - 'LSOURCE': 'Tutorials', |
38 | | - 'UPDATES': '', |
39 | | - 'URL': trimmedURL, |
40 | | - 'add_tags_immediately': false, |
41 | | - 'email': email, |
42 | | - 'g-recaptcha-response': value, |
43 | | - 'list_id' : 'bec00ef235', |
44 | | - 'tags': ['Product Updates'] |
45 | | - }; |
46 | | - |
47 | | - axios({ |
48 | | - method: 'POST', |
49 | | - url: CONTACT_LIST_API, |
50 | | - data: formData, |
51 | | - headers: { "Content-Type": "application/json; charset=UTF-8" }, |
52 | | - }) |
53 | | - .then(function (response) { |
54 | | - // handle success |
55 | | - this.setState({ subscribed: true }); |
56 | | - }.bind(this)) |
57 | | - .catch(function (response) { |
58 | | - // handle error |
59 | | - console.error("Failed: ", response); |
60 | | - }); |
61 | | - } |
62 | | - }); |
63 | | - } |
64 | | - } |
| 25 | + return () => { |
| 26 | + document.body.removeChild(script); |
| 27 | + }; |
| 28 | + }, []); |
65 | 29 |
|
66 | | - render() { |
67 | | - return ( |
68 | | - <> |
69 | | - <div className="footer_greenbox"> |
70 | | - <div className="flex_item"> |
71 | | - <h4 className="green_text"> |
72 | | - Stay up to date with product updates & news |
73 | | - </h4> |
74 | | - </div> |
75 | | - <div className={this.state.subscribed ? "hide" : "flex_item"}> |
76 | | - <form> |
77 | | - <svg width="20" height="15" viewBox="0 0 15 11" fill="none" xmlns="http://www.w3.org/2000/svg"> |
78 | | - <path |
79 | | - d="M12.75 10.4829H1.41667C1.04094 10.4829 0.680609 10.3449 0.414933 10.0991C0.149256 9.85337 0 9.52008 0 9.17255V1.25336C0.0158898 0.915807 0.172132 0.596975 0.436106 0.36343C0.70008 0.129885 1.05138 -0.00031923 1.41667 5.87777e-07H12.75C13.1257 5.87777e-07 13.4861 0.138056 13.7518 0.383797C14.0174 0.629538 14.1667 0.962834 14.1667 1.31036V9.17255C14.1667 9.52008 14.0174 9.85337 13.7518 10.0991C13.4861 10.3449 13.1257 10.4829 12.75 10.4829ZM1.41667 2.53424V9.17255H12.75V2.53424L7.08334 6.02767L1.41667 2.53424ZM1.98334 1.31036L7.08334 4.45524L12.1834 1.31036H1.98334Z" |
80 | | - fill="#CACACA"/> |
81 | | - </svg> |
82 | | - <input type="text" id="email" name="email" placeholder="Your Email Id"/> |
83 | | - </form> |
84 | | - <p id="email_error" className="email-error">Please fill out this field.</p> |
85 | | - </div> |
86 | | - <div className={this.state.subscribed ? "hide" : "flex_item btn_width"}> |
87 | | - <ReCAPTCHA |
88 | | - size="invisible" |
89 | | - ref={this._reCaptchaRef} |
90 | | - sitekey='6LcL08cZAAAAAAn21PBn75R1qLquLlbx1ZB0ZIFd' |
91 | | - /> |
92 | | - <button className="btn" onClick={this.subscribeNewsletters}>Subscribe</button> |
93 | | - </div> |
94 | | - <div className={this.state.subscribed ? "flex_item subscribe-success" : "hide"}> |
95 | | - <h4 className="green_text"> |
96 | | - You're now subscribed to our monthly product updates. |
97 | | - </h4> |
98 | | - </div> |
99 | | - </div> |
100 | | - </> |
101 | | - ); |
102 | | - } |
103 | | -} |
| 30 | + return ( |
| 31 | + <div className="footer_greenbox"> |
| 32 | + <div className="flex_item"> |
| 33 | + <h4 className="green_text"> |
| 34 | + Stay up to date with product updates & news |
| 35 | + </h4> |
| 36 | + </div> |
| 37 | + <div className="flex_item" id="hubspot-form-wrapper" /> |
| 38 | + </div> |
| 39 | + ); |
| 40 | +}; |
104 | 41 |
|
105 | 42 | export default SubscribeButton; |
0 commit comments