-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (107 loc) · 4.21 KB
/
Copy pathindex.js
File metadata and controls
116 lines (107 loc) · 4.21 KB
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
/* globals newspack_my_account */
/**
* Internal dependencies.
*/
import './style.scss';
/**
* Specify a function to execute when the DOM is fully loaded.
*
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/dom-ready/
*
* @param {Function} callback A function to execute after the DOM is ready.
* @return {void}
*/
function domReady( callback ) {
if ( typeof document === 'undefined' ) {
return;
}
if (
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
) {
return void callback();
}
// DOMContentLoaded has not fired yet, delay callback until then.
document.addEventListener( 'DOMContentLoaded', callback );
}
domReady( function () {
const cancelButton = document.querySelector( '.subscription_details .button.cancel' );
const { labels, nonce, rest_url, should_rate_limit } = newspack_my_account || {};
// Show a confirmation dialog before cancelling a subscription.
if ( cancelButton ) {
const confirmCancel = event => {
const message =
labels?.cancel_subscription_message ||
'Are you sure you want to cancel this subscription?';
// eslint-disable-next-line no-alert
if ( ! confirm( message ) ) {
event.preventDefault();
}
};
cancelButton.addEventListener( 'click', confirmCancel );
}
// Rate limit the add payment method form.
const addPaymentForm = document.getElementById( 'add_payment_method' );
if ( addPaymentForm && Boolean( should_rate_limit ) ) {
const errorContainer = document.querySelector( '.woocommerce-notices-wrapper' );
const submitButton = addPaymentForm.querySelector( 'input[type="submit"], button[type="submit"]' );
const rateLimit = function( e ) {
if ( addPaymentForm.hasAttribute( 'data-check-rate-limit' ) ) {
errorContainer.textContent = '';
submitButton.setAttribute( 'disabled', '' );
e.preventDefault();
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
// Return if the request is completed.
if ( xhr.readyState !== 4 ) {
return;
}
// Call onSuccess with parsed JSON if the request is successful.
if ( xhr.status >= 200 && xhr.status < 300 ) {
submitButton.removeAttribute( 'disabled' );
const data = JSON.parse( xhr.responseText );
if ( data?.success ) {
addPaymentForm.removeAttribute( 'data-check-rate-limit' );
addPaymentForm.requestSubmit( submitButton );
addPaymentForm.setAttribute( 'data-check-rate-limit', '1' );
}
if ( data?.error ) {
const error = document.createElement( 'div' );
const errorUl = document.createElement( 'ul' );
const errorLi = document.createElement( 'li' );
errorUl.classList.add( 'woocommerce-error' );
errorLi.textContent = data.error;
error.appendChild( errorUl );
errorUl.appendChild( errorLi );
errorContainer.appendChild( error );
errorContainer.scrollIntoView( { behavior: 'smooth' } );
}
}
};
xhr.open( 'GET', rest_url + 'newspack/v1/check-rate' );
xhr.setRequestHeader( 'X-WP-Nonce', nonce );
xhr.send();
}
};
addPaymentForm.setAttribute( 'data-check-rate-limit', '1' );
addPaymentForm.addEventListener( 'submit' , rateLimit, true );
submitButton.addEventListener( 'click', rateLimit, true );
}
// Fire a newsletter_signup event when the user subscribes to a newsletter via My Account.
window.newspackRAS = window.newspackRAS || [];
window.newspackRAS.push( readerActivation => {
const reader = readerActivation.getReader();
const params = new URLSearchParams( window.location.search );
const subscribed = params.get( 'newspack_newsletters_subscription_subscribed' );
if ( subscribed && reader?.email && reader?.authenticated ) {
readerActivation.dispatchActivity( 'newsletter_signup', {
email: reader.email,
lists: subscribed.split( ',' ),
newsletters_subscription_method: 'my-account',
} );
}
params.delete( 'newspack_newsletters_subscription_subscribed' );
const newQueryString = params.toString() ? '?' + params.toString() : '';
window.history.replaceState( {}, '', window.location.pathname + newQueryString );
} );
} );