Ghost/core/server/public/members.min.js
2020-04-30 19:00:37 +01:00

1 line
5.6 KiB
JavaScript

Array.prototype.forEach.call(document.querySelectorAll("form[data-members-form]"),function(a){var c=a.querySelector("[data-members-error]");a.addEventListener("submit",function r(e){a.removeEventListener("submit",r),e.preventDefault(),c&&(c.innerText=""),a.classList.remove("success","invalid","error");for(var t=e.target.querySelector("input[data-members-email]").value,n=void 0,o=[],s=e.target.querySelectorAll("input[data-members-label]")||[],i=0;i<s.length;++i)o.push(s[i].value);a.dataset.membersForm&&(n=a.dataset.membersForm),a.classList.add("loading"),fetch("{{blog-url}}/members/api/send-magic-link/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t,emailType:n,labels:o})}).then(function(e){a.addEventListener("submit",r),a.classList.remove("loading"),e.ok?a.classList.add("success"):(c&&(c.innerText="There was an error sending the email, please try again"),a.classList.add("error"))})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-plan]"),function(a){var c=a.querySelector("[data-members-error]");a.addEventListener("click",function r(e){a.removeEventListener("click",r),e.preventDefault();var t,n,o=a.dataset.membersPlan,s=a.dataset.membersSuccess,i=a.dataset.membersCancel;s&&(t=new URL(s,window.location.href).href),i&&(n=new URL(i,window.location.href).href),c&&(c.innerText=""),a.classList.add("loading"),fetch("{{blog-url}}/members/api/session",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{blog-url}}/members/api/create-stripe-checkout-session/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({plan:o,identity:e,successUrl:t,cancelUrl:n})}).then(function(e){if(!e.ok)throw new Error("Could not create stripe checkout session");return e.json()})}).then(function(e){return Stripe(e.publicKey).redirectToCheckout({sessionId:e.sessionId})}).then(function(e){if(e.error)throw new Error(e.error.message)}).catch(function(e){console.error(e),a.addEventListener("click",r),a.classList.remove("loading"),c&&(c.innerText=e.message),a.classList.add("error")})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-edit-billing]"),function(t){var n,o,s=t.querySelector("[data-members-error]"),e=t.dataset.membersSuccess,r=t.dataset.membersCancel;e&&(n=new URL(e,window.location.href).href),r&&(o=new URL(r,window.location.href).href),t.addEventListener("click",function r(e){t.removeEventListener("click",r),e.preventDefault(),s&&(s.innerText=""),t.classList.add("loading"),fetch("{{blog-url}}/members/api/session",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{blog-url}}/members/api/create-stripe-update-session/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({identity:e,successUrl:n,cancelUrl:o})}).then(function(e){if(!e.ok)throw new Error("Could not create stripe checkout session");return e.json()})}).then(function(e){return Stripe(e.publicKey).redirectToCheckout({sessionId:e.sessionId})}).then(function(e){if(e.error)throw new Error(e.error.message)}).catch(function(e){console.error(e),t.addEventListener("click",r),t.classList.remove("loading"),s&&(s.innerText=e.message),t.classList.add("error")})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-signout]"),function(t){t.addEventListener("click",function r(e){t.removeEventListener("click",r),e.preventDefault(),t.classList.remove("error"),t.classList.add("loading"),fetch("{{blog-url}}/members/api/session",{method:"DELETE"}).then(function(e){e.ok?window.location.reload():(t.addEventListener("click",r),t.classList.remove("loading"),t.classList.add("error"))})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-cancel-subscription]"),function(n){var o=n.parentElement.querySelector("[data-members-error]");n.addEventListener("click",function r(e){n.removeEventListener("click",r),e.preventDefault(),n.classList.remove("error"),n.classList.add("loading");var t=n.dataset.membersCancelSubscription;return o&&(o.innerText=""),fetch("{{blog-url}}/members/api/session",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{blog-url}}/members/api/subscriptions/"+t+"/",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({identity:e,cancel_at_period_end:!0})})}).then(function(e){e.ok?window.location.reload():(n.addEventListener("click",r),n.classList.remove("loading"),n.classList.add("error"),o&&(o.innerText="There was an error cancelling your subscription, please try again."))})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-continue-subscription]"),function(n){var o=n.parentElement.querySelector("[data-members-error]");n.addEventListener("click",function r(e){n.removeEventListener("click",r),e.preventDefault(),n.classList.remove("error"),n.classList.add("loading");var t=n.dataset.membersContinueSubscription;return o&&(o.innerText=""),fetch("{{blog-url}}/members/api/session",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{blog-url}}/members/api/subscriptions/"+t+"/",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({identity:e,cancel_at_period_end:!1})})}).then(function(e){e.ok?window.location.reload():(n.addEventListener("click",r),n.classList.remove("loading"),n.classList.add("error"),o&&(o.innerText="There was an error continuing your subscription, please try again."))})})});var url=new URL(window.location);url.searchParams.get("token")&&(url.searchParams.delete("token"),window.history.replaceState({},document.title,url.href));