2019-09-06 11:07:46 +03:00
|
|
|
Array.prototype.forEach.call(document.querySelectorAll('form[data-members-form]'), function (form){
|
2019-09-10 09:23:52 +03:00
|
|
|
var errorEl = form.querySelector('[data-members-error]');
|
2019-09-10 10:48:55 +03:00
|
|
|
function submitHandler(event) {
|
|
|
|
form.removeEventListener('submit', submitHandler);
|
2019-09-03 07:39:00 +03:00
|
|
|
event.preventDefault();
|
2019-09-10 09:23:52 +03:00
|
|
|
if (errorEl) {
|
|
|
|
errorEl.innerText = '';
|
|
|
|
}
|
2019-09-03 07:39:00 +03:00
|
|
|
form.classList.remove('success', 'invalid', 'error');
|
2019-09-06 11:07:46 +03:00
|
|
|
var input = event.target.querySelector('input[data-members-email]');
|
|
|
|
var email = input.value;
|
2019-09-03 07:39:00 +03:00
|
|
|
|
|
|
|
if (!email.includes('@')) {
|
|
|
|
form.classList.add('invalid')
|
2019-09-10 10:48:55 +03:00
|
|
|
form.addEventListener('submit', submitHandler);
|
2019-09-03 07:39:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:29:56 +03:00
|
|
|
form.classList.add('loading');
|
2019-09-06 10:14:21 +03:00
|
|
|
fetch('{{admin-url}}/api/canary/members/send-magic-link/', {
|
2019-09-03 07:39:00 +03:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2019-09-06 11:07:46 +03:00
|
|
|
email: email
|
2019-09-03 07:39:00 +03:00
|
|
|
})
|
|
|
|
}).then(function (res) {
|
2019-09-10 10:48:55 +03:00
|
|
|
form.addEventListener('submit', submitHandler);
|
2019-09-09 07:29:56 +03:00
|
|
|
form.classList.remove('loading');
|
2019-09-03 07:39:00 +03:00
|
|
|
if (res.ok) {
|
|
|
|
form.classList.add('success')
|
|
|
|
} else {
|
2019-09-10 09:23:52 +03:00
|
|
|
if (errorEl) {
|
|
|
|
errorEl.innerText = 'There was an error sending the email, please try again';
|
|
|
|
}
|
2019-09-03 07:39:00 +03:00
|
|
|
form.classList.add('error')
|
|
|
|
}
|
|
|
|
});
|
2019-09-10 10:48:55 +03:00
|
|
|
}
|
|
|
|
form.addEventListener('submit', submitHandler);
|
2019-09-06 10:14:21 +03:00
|
|
|
});
|
|
|
|
|
2019-09-10 09:23:52 +03:00
|
|
|
Array.prototype.forEach.call(document.querySelectorAll('[data-members-plan]'), function (el) {
|
|
|
|
var errorEl = el.querySelector('[data-members-error]');
|
2019-09-10 10:48:55 +03:00
|
|
|
function clickHandler(event) {
|
|
|
|
el.removeEventListener('click', clickHandler);
|
2019-09-06 10:14:21 +03:00
|
|
|
event.preventDefault();
|
|
|
|
|
2019-09-10 09:23:52 +03:00
|
|
|
var plan = el.dataset.membersPlan;
|
2019-09-06 10:14:21 +03:00
|
|
|
|
2019-09-10 09:23:52 +03:00
|
|
|
if (errorEl) {
|
|
|
|
errorEl.innerText = '';
|
|
|
|
}
|
2019-09-09 07:54:01 +03:00
|
|
|
el.classList.add('loading');
|
2019-09-06 10:14:21 +03:00
|
|
|
fetch('{{blog-url}}/members/ssr', {
|
|
|
|
credentials: 'same-origin'
|
|
|
|
}).then(function (res) {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error('Could not get identity token');
|
|
|
|
}
|
|
|
|
return res.text();
|
|
|
|
}).then(function (identity) {
|
|
|
|
return fetch('{{admin-url}}/api/canary/members/create-stripe-checkout-session/', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2019-09-06 11:07:46 +03:00
|
|
|
plan: plan,
|
|
|
|
identity: identity
|
2019-09-06 10:14:21 +03:00
|
|
|
})
|
|
|
|
}).then(function (res) {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error('Could not create stripe checkout session');
|
|
|
|
}
|
|
|
|
return res.json();
|
|
|
|
});
|
2019-09-06 11:07:46 +03:00
|
|
|
}).then(function (result) {
|
|
|
|
var stripe = Stripe(result.publicKey);
|
2019-09-06 10:14:21 +03:00
|
|
|
return stripe.redirectToCheckout({
|
2019-09-06 11:07:46 +03:00
|
|
|
sessionId: result.sessionId
|
2019-09-06 10:14:21 +03:00
|
|
|
});
|
2019-09-10 09:23:52 +03:00
|
|
|
}).then(function (result) {
|
|
|
|
if (result.error) {
|
|
|
|
throw new Error(result.error.message);
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
console.error(err);
|
2019-09-10 10:48:55 +03:00
|
|
|
el.addEventListener('click', clickHandler);
|
2019-09-09 07:54:01 +03:00
|
|
|
el.classList.remove('loading');
|
2019-09-10 09:23:52 +03:00
|
|
|
if (errorEl) {
|
|
|
|
errorEl.innerText = err.message;
|
|
|
|
}
|
2019-09-09 07:54:01 +03:00
|
|
|
el.classList.add('error');
|
2019-09-06 10:14:21 +03:00
|
|
|
});
|
2019-09-10 10:48:55 +03:00
|
|
|
}
|
|
|
|
el.addEventListener('click', clickHandler);
|
2019-09-06 10:14:21 +03:00
|
|
|
});
|
2019-09-03 07:39:00 +03:00
|
|
|
|
2019-09-10 10:38:35 +03:00
|
|
|
Array.prototype.forEach.call(document.querySelectorAll('[data-members-signout]'), function (el) {
|
2019-09-10 10:48:55 +03:00
|
|
|
function clickHandler(event) {
|
|
|
|
el.removeEventListener('click', clickHandler);
|
2019-09-10 10:38:35 +03:00
|
|
|
event.preventDefault();
|
|
|
|
el.classList.remove('error');
|
|
|
|
el.classList.add('loading');
|
|
|
|
fetch('{{blog-url}}/members/ssr', {
|
|
|
|
method: 'DELETE'
|
|
|
|
}).then(function (res) {
|
|
|
|
if (res.ok) {
|
|
|
|
window.location.reload();
|
|
|
|
} else {
|
2019-09-10 10:48:55 +03:00
|
|
|
el.addEventListener('click', clickHandler);
|
2019-09-10 10:38:35 +03:00
|
|
|
el.classList.remove('loading');
|
|
|
|
el.classList.add('error');
|
|
|
|
}
|
|
|
|
});
|
2019-09-10 10:48:55 +03:00
|
|
|
}
|
|
|
|
el.addEventListener('click', clickHandler);
|
2019-09-10 10:38:35 +03:00
|
|
|
});
|
|
|
|
|
2019-09-06 11:07:46 +03:00
|
|
|
var magicLinkRegEx = /token=([a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+\.[a-zA-Z0-9_\-]+)/;
|
|
|
|
var match = location.search.match(magicLinkRegEx);
|
|
|
|
var isMagicLink = !!match
|
|
|
|
var token = match && match[1];
|
2019-09-03 07:39:00 +03:00
|
|
|
|
|
|
|
if (isMagicLink) {
|
|
|
|
fetch('{{blog-url}}/members/ssr', {
|
|
|
|
method: 'POST',
|
|
|
|
body: token
|
|
|
|
}).then(function (res) {
|
|
|
|
if (res.ok) {
|
|
|
|
window.location.search = window.location.search.replace(magicLinkRegEx, '');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|