Skip to content
Snippets Groups Projects
viewPost.js 1.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jonas Leder's avatar
    Jonas Leder committed
    if(window.location['pathname'] == "/post.html"){
        loadPost();
    }
    
    
    function getParameter(key) {
    
        // Address of the current window
        let address = window.location.search
    
        // Returns a URLSearchParams object instance
        let parameterList = new URLSearchParams(address)
    
        // Returning the respected value associated
        // with the provided key
        return parameterList.get(key)
    }
    
    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 {
    
            var graphql = JSON.stringify({
            query: 'query {blogPost(id: "' + id + '") {content title}}',
            variables: {}
            })
            var requestOptions = {
            method: 'POST',
            body: graphql,
            };
            let post = (await (await fetch("http://localhost:1234/API/graphql.php", requestOptions)).json()).data.blogPost;
    
    Jonas Leder's avatar
    Jonas Leder committed
            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);
    }