my-portfolio/dist/blog/js/index.js

1 line
30 KiB
JavaScript
Raw Normal View History

const scrollLimit=150;function goToURL(t){let e=t.split("/");if("accepted"===localStorage.getItem("cookiePopup")&&document.querySelector("#cookiePopup").classList.add("hidden"),"/blog"!==t&&"blog"!==t)if("post"!==e[2]){if("category"===e[2])return e[3]?void loadPostsByCategory(e[e.length-1]):void loadAllCategories().catch((t=>console.log(t)));if("search"===e[2]&&e[3])loadSearchResults(e[e.length-1]);else{if("policy"===e[2]){if("privacy"===e[3])return void loadPrivacyPolicy();if("cookie"===e[3])return void loadCookiePolicy()}"unsubscribe"===e[2]&&e[3]?unsubscribe(e[e.length-1]):show404()}}else loadIndividualPost(e[e.length-1]).catch((t=>console.log(t)));else loadHomeContent()}function submitNewsletter(){fetch(`/api/blog/newsletter/${document.querySelector("#email").value}`,{method:"POST",headers:{"Content-Type":"application/json"}}).then((t=>t.json().then((e=>(document.querySelector("#newsletterMessage").classList.remove("hidden"),e.message.includes("exists")?(document.querySelector("#newsletterMessage").classList.add("error"),document.querySelector("#newsletterMessage").classList.remove("success"),void(document.querySelector("#newsletterMessage div").innerHTML='You"ve already signed up you silly goose!')):t.ok?(document.querySelector("#newsletterMessage div").innerHTML=e.message,void document.querySelector("#newsletterMessage").classList.add("success")):(document.querySelector("#newsletterMessage").classList.add("error"),document.querySelector("#newsletterMessage").classList.remove("success"),void(document.querySelector("#newsletterMessage div").innerHTML=e.error)))))))}function unsubscribe(t){fetch(`/api/blog/newsletter/${t}`,{method:"DELETE",headers:{"Content-Type":"application/json"}}).then((t=>t.json().then((t=>{document.querySelector("#main").innerHTML="";let e=document.createElement("section");e.classList.add("unsubscribe"),e.id="unsubscribe";let n=document.createElement("div");n.classList.add("centered"),n.innerHTML=`\n\t\t\t<h1>Unsubscribe</h1>\n\t\t\t<p>${t.message}</p>\n \t<a href="/blog/" class="btn btnPrimary">See all blog posts</a>\n\t\t`,e.appendChild(n),document.querySelector("#main").appendChild(e)}))))}function createFormattedDate(t){return new Date(t).toLocaleDateString("en-GB",{day:"2-digit",month:"short",year:"numeric"})}function createLargePost(t){let e=document.createElement("div");e.classList.add("outerContent");let n=document.createElement("img");n.className="banner",n.src=t.headerImg.replaceAll("%2F","/"),n.alt=t.title,e.appendChild(n);let o=document.createElement("div");o.classList.add("content");let a=document.createElement("div");a.classList.add("postContent");let i="";t.categories.split(", ").forEach((e=>{i+=`<a href="/blog/category/${e}" class="link">${e}</a>`,t.categories.split(", ").length>1&&(i+=", ")})),i.endsWith(", ")&&(i=i.substring(0,i.length-2));let s=createFormattedDate(t.dateModified);return a.innerHTML=`\n <h2>${t.title}</h2>\n <h3>Last updated: ${s} | ${i}</h3>\n <p>${t.abstract}</p>\n <a href="/blog/post/${t.title}" class="btn btnPrimary">See Post</a>\n `,o.appendChild(a),e.appendChild(o),e}function loadHomeContent(){fetch("/api/blog/post").then((t=>t.json().then((t=>{for(let e=0;e<t.length;e++){if(1===t[e].featured){let n=document.createElement("section");n.classList.add("largePost"),n.id="featuredPost";let o=document.createElement("h1");o.innerHTML="featured post",n.appendChild(o);let a=createLargePost(t[e]);n.appendChild(a),document.querySelector("#main").appendChild(n)}if(1===e){let n=document.createElement("section");n.classList.add("largePost"),n.id="latestPost";let o=document.createElement("h1");o.innerHTML="latest post",n.appendChild(o);let a=createLargePost(t[e]);n.appendChild(a),document.querySelector("#main").appendChild(n)}}}))))}async function getLatestAndFeaturedPosts(){return[await fetch("/api/blog/post/latest").then((t=>t.json())),await fetch("/api/blog/post/featured").then((t=>t.json()))]}function csvToArray(t){let e="",n=[""],o=0,a=!0,i=null;for(i of t)'"'===i?(a&