load comments from graphQL

This commit is contained in:
Jonas Leder 2022-03-08 12:23:05 +01:00
parent f132ba2c55
commit 9b12fe2c94
No known key found for this signature in database
GPG key ID: 8A53DD45A7D7B44B

View file

@ -1,14 +1,21 @@
class commentsDisplay extends HTMLElement {
constructor() {
super();
let path = window.location.pathname;
let pageName = path.split("/").pop();
this.getComments()
}
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let comments = JSON.parse(xhr.responseText);
async getComments() {
var graphql = JSON.stringify({
query: 'query($article: String!) { comments(article: $article) { name comment gravatarURL }}',
variables: {
"article": window.location.pathname
}
})
var requestOptions = {
method: 'POST',
body: graphql,
};
let comments = (await (await fetch("http://localhost:1234/API/graphql.php", requestOptions)).json()).data.comments;
comments.forEach((element) => {
const h3 = document.createElement("h3");
h3.classList.add("commentTitle");
@ -33,16 +40,6 @@ class commentsDisplay extends HTMLElement {
article.appendChild(commentText);
});
} else {
let p = document.createElement("p");
p.innerText = "Leider konnte dieser Inhalt nicht geladen werden, bitte versuche die Seite neu zu laden oder komme später wieder zurück.";
this.appendChild(p);
}
}
}
xhr.open("GET", "/API/projectComments.php?article=" + pageName);
xhr.send();
}
}