Ghost/core/server/public/members.min.js
Nazar Gargol 258bcc71bf Added minified members.js file handling
refs 91984b54ca

- For request effieciency we should be using a minified file just like we did previously with `ghost-sdk.js`
- Modified 'max-age' caching header to 1 year  for both minified and non-minified files as thay won't affect dev environment and should be beneficial for self-hosting instances that don't use minification
- Along the way corrected an extra 301 redirect because `/public/member.js` path wasn't using a bakslach in the end.
2020-02-26 14:08:10 +08:00

1 line
5.6 KiB
JavaScript

Array.prototype.forEach.call(document.querySelectorAll("form[data-members-form]"),function(i){var c=i.querySelector("[data-members-error]");i.addEventListener("submit",function r(e){i.removeEventListener("submit",r),e.preventDefault(),c&&(c.innerText=""),i.classList.remove("success","invalid","error");for(var t=e.target.querySelector("input[data-members-email]").value,n=void 0,a=[],o=e.target.querySelectorAll("input[data-members-label]")||[],s=0;s<o.length;++s)a.push(o[s].value);i.dataset.membersForm&&(n=i.dataset.membersForm),i.classList.add("loading"),fetch("{{admin-url}}/api/canary/members/send-magic-link/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t,emailType:n,labels:a})}).then(function(e){i.addEventListener("submit",r),i.classList.remove("loading"),e.ok?i.classList.add("success"):(c&&(c.innerText="There was an error sending the email, please try again"),i.classList.add("error"))})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-plan]"),function(i){var c=i.querySelector("[data-members-error]");i.addEventListener("click",function r(e){i.removeEventListener("click",r),e.preventDefault();var t,n,a=i.dataset.membersPlan,o=i.dataset.membersSuccess,s=i.dataset.membersCancel;o&&(t=new URL(o,window.location.href).href),s&&(n=new URL(s,window.location.href).href),c&&(c.innerText=""),i.classList.add("loading"),fetch("{{blog-url}}/members/ssr",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{admin-url}}/api/canary/members/create-stripe-checkout-session/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({plan:a,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),i.addEventListener("click",r),i.classList.remove("loading"),c&&(c.innerText=e.message),i.classList.add("error")})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-edit-billing]"),function(t){var n,a,o=t.querySelector("[data-members-error]"),e=t.dataset.membersSuccess,r=t.dataset.membersCancel;e&&(n=new URL(e,window.location.href).href),r&&(a=new URL(r,window.location.href).href),t.addEventListener("click",function r(e){t.removeEventListener("click",r),e.preventDefault(),o&&(o.innerText=""),t.classList.add("loading"),fetch("{{blog-url}}/members/ssr",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{admin-url}}/api/canary/members/create-stripe-setup-session/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({identity:e,successUrl:n,cancelUrl:a})}).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"),o&&(o.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/ssr",{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 a=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 a&&(a.innerText=""),fetch("{{blog-url}}/members/ssr",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{admin-url}}/api/canary/members/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"),a&&(a.innerText="There was an error cancelling your subscription, please try again."))})})}),Array.prototype.forEach.call(document.querySelectorAll("[data-members-continue-subscription]"),function(n){var a=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 a&&(a.innerText=""),fetch("{{blog-url}}/members/ssr",{credentials:"same-origin"}).then(function(e){return e.ok?e.text():null}).then(function(e){return fetch("{{admin-url}}/api/canary/members/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"),a&&(a.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));