diff --git a/js/script.js b/js/script.js index 761ff96..63cf079 100644 --- a/js/script.js +++ b/js/script.js @@ -4,6 +4,7 @@ require("./imgPreview"); require("./includeHTML"); require("./ntpGraph"); require("./ntpMenu"); +require("./viewPost"); require("./customElements/cookie"); require("./customElements/svgLoader"); diff --git a/js/viewPost.js b/js/viewPost.js new file mode 100644 index 0000000..01241e9 --- /dev/null +++ b/js/viewPost.js @@ -0,0 +1,40 @@ +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 = "

404 - Post not found

"; + } else { + let post = await (await fetch("/API/getPost.php?id=" + id)).json(); + 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); +} \ No newline at end of file diff --git a/public/API/getPost.php b/public/API/getPost.php new file mode 100644 index 0000000..9ac86e8 --- /dev/null +++ b/public/API/getPost.php @@ -0,0 +1,24 @@ +real_escape_string($_GET["id"]); +$result = $conn->query("SELECT * FROM posts WHERE id=$id"); +if ($result->num_rows > 0) { + $row = $result->fetch_assoc(); +} else { + die("Post not found"); +} + +$title = $row["title"]; +$content = $row["content"]; +$date = $row["date"]; +$id = $row["id"]; + +header('Content-Type: application/json'); +echo json_encode([ + "title" => $title, + "content" => $content, + "date" => $date, + "id" => $id +]); \ No newline at end of file diff --git a/public/post.html b/public/post.html index ffc7ad0..b89bdff 100644 --- a/public/post.html +++ b/public/post.html @@ -1,32 +1,15 @@ - + + + + + + -} else{ - $id = $conn->real_escape_string($_GET["id"]); - $result = $conn->query("SELECT * FROM posts WHERE id=$id"); - if ($result->num_rows > 0) { - $row = $result->fetch_assoc(); - } else { - die("Post not found"); - } - - $title = $row["title"]; - $content = $row["content"]; - $date = $row["date"]; - $id = $row["id"]; -} - -getHeader($title, ""); -?> -

- -

- + + + + + \ No newline at end of file