@@ -5,6 +5,10 @@ import axios from 'axios';
55class SubscribeButton extends Component {
66 constructor ( props ) {
77 super ( props ) ;
8+
9+ this . state = {
10+ subscribed : false ,
11+ } ;
812 this . _reCaptchaRef = React . createRef ( ) ;
913 }
1014
@@ -14,56 +18,49 @@ class SubscribeButton extends Component {
1418 const CONTACT_LIST_API = 'https://staging.testsigma.com/api/website/contacts' ;
1519 const email = document . getElementById ( 'email' ) . value ;
1620 const emailError = document . getElementById ( 'email_error' ) ;
17- const subscribeSuccess = document . getElementById ( 'subscribe_success' ) ;
1821 let trimmedURL = window . location . href ;
1922
2023 if ( trimmedURL . length > 250 )
2124 trimmedURL = window . location . href . substr ( 0 , 240 ) + "..." ;
2225
2326 const regex = / ^ ( [ a - z A - Z 0 - 9 _ \. \- \+ ] ) + \@ ( ( [ a - z A - Z 0 - 9 \- ] ) + \. ) + ( [ a - z A - Z 0 - 9 ] { 2 , 4 } ) + $ / ;
24-
25- this . _reCaptchaRef . current . executeAsync ( ) . then ( value => {
26-
27- const formData = {
28- 'CATEGORY' : 'Product Updates' ,
29- 'LSOURCE' : 'Tutorials' ,
30- 'UPDATES' : '' ,
31- 'URL' : trimmedURL ,
32- 'add_tags_immediately' : false ,
33- 'email' : email ,
34- 'g-recaptcha-response' : value ,
35- 'list_id' : 'bec00ef235' ,
36- 'tags' : [ 'Product Updates' ]
37- } ;
38-
39- emailError . style . display = "none" ;
40- subscribeSuccess . style . display = "none" ;
41-
42- if ( ! regex . test ( email ) ) {
43- emailError . style . display = "block" ;
44- return false ;
45- } else {
46- axios ( {
47- method : 'POST' ,
48- url : CONTACT_LIST_API ,
49- data : formData ,
50- headers : { "Content-Type" : "application/json; charset=UTF-8" } ,
51- } )
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+ } )
5253 . then ( function ( response ) {
5354 // handle success
54- document . getElementById ( 'email' ) . value = "" ;
55- subscribeSuccess . style . display = "block" ;
56- setTimeout ( ( ) => {
57- subscribeSuccess . style . display = "none" ;
58- } , 3000 ) ;
59- // console.log("Success: ", response);
60- } )
55+ this . setState ( { subscribed : true } ) ;
56+ } . bind ( this ) )
6157 . catch ( function ( response ) {
6258 // handle error
6359 console . error ( "Failed: " , response ) ;
6460 } ) ;
65- }
66- } ) ;
61+ }
62+ } ) ;
63+ }
6764 }
6865
6966 render ( ) {
@@ -75,7 +72,7 @@ class SubscribeButton extends Component {
7572 Stay up to date with product updates & news
7673 </ h4 >
7774 </ div >
78- < div className = " flex_item">
75+ < div className = { this . state . subscribed ? "hide" : " flex_item"} >
7976 < form >
8077 < svg width = "20" height = "15" viewBox = "0 0 15 11" fill = "none" xmlns = "http://www.w3.org/2000/svg" >
8178 < path
@@ -85,16 +82,20 @@ class SubscribeButton extends Component {
8582 < input type = "text" id = "email" name = "email" placeholder = "Your Email Id" />
8683 </ form >
8784 < p id = "email_error" className = "email-error" > Please fill out this field.</ p >
88- < p id = "subscribe_success" className = "subscribe-success" > You're now subscribed to our monthly product updates.</ p >
8985 </ div >
90- < div className = " flex_item btn_width">
86+ < div className = { this . state . subscribed ? "hide" : " flex_item btn_width"} >
9187 < ReCAPTCHA
9288 size = "invisible"
9389 ref = { this . _reCaptchaRef }
9490 sitekey = '6LcL08cZAAAAAAn21PBn75R1qLquLlbx1ZB0ZIFd'
9591 />
9692 < button className = "btn" onClick = { this . subscribeNewsletters } > Subscribe</ button >
9793 </ 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 >
9899 </ div >
99100 </ >
100101 ) ;
0 commit comments