Ghost/core/server/public/members.min.js
Fabien 'egg' O'Carroll ce14d8113a
🐛 Fixed members form missing data-members-name (#12250)
refs #12249

This was incorrectly assuming the presence of the data-members-name
element in the document. By guarding against it and defaulting to
undefined, we fallback to the existing behaviour when the element is not
present.
2020-10-01 18:05:23 +01:00

1 line
5.7 KiB
JavaScript

Array.prototype.forEach.call(document.querySelectorAll("form[data-members-form]"),function(d){var u=d.querySelector("[data-members-error]");d.addEventListener("submit",function r(e){d.removeEventListener("submit",r),e.preventDefault(),u&&(u.innerText=""),d.classList.remove("success","invalid","error");for(var t=e.target.querySelector("input[data-members-email]"),n=e.target.querySelector("input[data-members-name]"),o=t.value,s=n&&n.value||void 0,i=void 0,a=[],c=e.target.querySelectorAll("input[data-members-label]")||[],l=0;l<c.length;++l)a.push(c[l].value);d.dataset.membersForm&&(i=d.dataset.membersForm),d.classList.add("loading"),fetch("{{blog-url}}/members/api/send-magic-link/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:o,emailType:i,labels:a,name:s})}).then(function(e){d.addEventListener("submit",r),d.classList.remove("loading"),e.ok?d.classList.add("success"):(u&&(u.innerText="There was an error sending the email, please try again"),d.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));