website/js/viewPost.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-04-13 10:33:40 +02:00
if(window.location['pathname'] == "/post.html"){
loadPost();
}
2022-03-25 12:23:41 +01:00
// return the value of the get parameter with the given name
function getParameter(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
var results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
2021-04-13 10:33:40 +02:00
}
async function loadPost() {
let id = getParameter("id");
let header = document.createElement("jl-header");
let footer = document.createElement("jl-footer");
let content = document.createElement("div");
if(id == null) {
content.innerHTML = "<h1>404 - Post not found</h1>";
} else {
2022-03-08 11:14:23 +01:00
var graphql = JSON.stringify({
2022-03-08 11:23:37 +01:00
query: 'query($postID: String!) {blogPost(id: $postID) {content title}}',
variables: {
"postID": id
}
2022-03-08 11:14:23 +01:00
})
var requestOptions = {
method: 'POST',
body: graphql,
2022-04-07 09:24:06 +02:00
headers: { 'Content-Type': 'application/json' }
2022-03-08 11:14:23 +01:00
};
2022-03-15 08:49:36 +01:00
let post = (await (await fetch("/API/graphql.php", requestOptions)).json()).data.blogPost;
2021-04-13 10:33:40 +02:00
content.innerHTML = post["content"];
document.title = post["title"] + " - Jonas Leder";
header.setAttribute("data-title", post["title"]);
}
content.id = "content";
document.body.appendChild(header);
document.body.appendChild(content);
document.body.appendChild(footer);
}