|
| 1 | +const bloggit_header = document.getElementById("bloggit-header") |
| 2 | +const bloggit_preloader = document.getElementById("bloggit-preloader") |
| 3 | +const bloggit = document.getElementById("bloggit-container") |
| 4 | + |
| 5 | +let bloggit_data = null |
| 6 | + |
| 7 | +bloggit_header.innerHTML = ` |
| 8 | +<nav class="navbar navbar-expand-lg navbar-light bg-light"> |
| 9 | + <a class="navbar-brand" href="#">Craennie</a> |
| 10 | + <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="na |
| 11 | +vbarNav" aria-expanded="false" aria-label="Toggle navigation"> |
| 12 | + <span class="navbar-toggler-icon"></span> |
| 13 | + </button> |
| 14 | + <div class="collapse navbar-collapse" id="navbarNav"> |
| 15 | + <ul class="navbar-nav ml-auto"> |
| 16 | +
|
| 17 | +
|
| 18 | + <li class="nav-item"> |
| 19 | + <a class="nav-link" href="/home">Home</a> |
| 20 | + </li> |
| 21 | + </ul> |
| 22 | + </div> |
| 23 | +</nav>` |
| 24 | + |
| 25 | + |
| 26 | +bloggit_preloader.innerHTML = ` <div id="preloader" class="spinner-border text-primary" role="status"> |
| 27 | + <span class="sr-only">Loading...</span> |
| 28 | + </div>` |
| 29 | + |
| 30 | + function convert_datetime(param){ |
| 31 | + // const dateComponents = param.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/); |
| 32 | + |
| 33 | + // const year = parseInt(dateComponents[1]); |
| 34 | + // const month = parseInt(dateComponents[2]) - 1; |
| 35 | + // const day = parseInt(dateComponents[3]); |
| 36 | + |
| 37 | + // // Create a JavaScript Date object without considering the timezone |
| 38 | + // const date = new Date(year, month, day); |
| 39 | + |
| 40 | + // const time = new Date(param); |
| 41 | + |
| 42 | + // // Get the hours, minutes, and seconds |
| 43 | + // const hours = time.getHours(); |
| 44 | + // const minutes = time.getMinutes(); |
| 45 | + // const seconds = time.getSeconds(); |
| 46 | + |
| 47 | + // let __time__ = `${hours}:${minutes}:${seconds}`; |
| 48 | + |
| 49 | + // Format the date as "Month Day, Year" |
| 50 | + return param |
| 51 | + return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }).toString() + " ;" + __time__; |
| 52 | + } |
| 53 | + |
| 54 | + function strip_tags(param){ |
| 55 | + let post_body = document.createElement("p") |
| 56 | + post_body.innerHTML = param |
| 57 | + let elements_to_remove = post_body.querySelectorAll("table", "img", "iframe", "audio", "video", "picture", |
| 58 | +"figure", "canvas") |
| 59 | + |
| 60 | + elements_to_remove.forEach(function(element) { |
| 61 | + element.remove(); |
| 62 | + }); |
| 63 | + var words = post_body.innerText.split(/\s+/); |
| 64 | + |
| 65 | + // Select the first 120 words |
| 66 | + return words.slice(0, 30).join(' '); |
| 67 | + } |
| 68 | + |
| 69 | +fetch(`http://127.0.0.1:8000/v2/main`, |
| 70 | + { |
| 71 | + "headers": { |
| 72 | + |
| 73 | + "Authorization": "Token a954b21e51c809b72b5e68d5a12717ef4e2c5d3a" |
| 74 | + |
| 75 | +} |
| 76 | + } |
| 77 | +) |
| 78 | +.then((response) => { |
| 79 | + if (response.status === 200) { // Replace with the expected status code |
| 80 | + return response.json(); // If expecting JSON response |
| 81 | + } |
| 82 | + else if(response.status === 404){ |
| 83 | + return response.json() |
| 84 | + } |
| 85 | + else { |
| 86 | + throw new Error(`Couldn't fetch your data: ${response.data}`); |
| 87 | + } |
| 88 | +}) |
| 89 | +.then((data) => { |
| 90 | + console.log(data) |
| 91 | + bloggit_data = data |
| 92 | +if(data["type"] === "all" && data["detail"] === "okay"){ |
| 93 | +data["posts"].forEach(function(parameter){ |
| 94 | + |
| 95 | + let title = parameter.title |
| 96 | + let body = parameter.body |
| 97 | + let image = parameter.image |
| 98 | + let post_id = parameter.custom_id |
| 99 | + let comments = parameter.comment_count |
| 100 | + |
| 101 | + let post_container = document.createElement('section') |
| 102 | + post_container.className = "blog-post" |
| 103 | + |
| 104 | + if(image){ |
| 105 | + |
| 106 | + post_container.innerHTML += ` |
| 107 | + <div class="post-content"> |
| 108 | + <h2>${title}</h2> |
| 109 | + <p>${strip_tags(body)}</p> |
| 110 | + <date>${convert_datetime(parameter.time)}</date> |
| 111 | + <div>${comments} Comments</div> |
| 112 | + <div class="read-more"> |
| 113 | + <a href="${data["individual_page"]}?id=${post_id}">Read More</a> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + <div class="post-image"> |
| 117 | + <img src="http://127.0.0.1:8000${image}" alt="${title}"> |
| 118 | + </div> |
| 119 | + ` |
| 120 | + bloggit.appendChild(post_container) |
| 121 | + }else{ |
| 122 | + post_container.innerHTML += ` |
| 123 | + <div class="post-content"> |
| 124 | + <h2>${title}</h2> |
| 125 | + <p>${strip_tags(body)}</p> |
| 126 | + <date>${convert_datetime(parameter.time)}</date> |
| 127 | + <div>${comments} Comments</div> |
| 128 | + <div class="read-more"> |
| 129 | + <a href="${data["individual_page"]}?id=${post_id}">Read More</a> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + <div class="post-image"> |
| 133 | +
|
| 134 | + </div> |
| 135 | + ` |
| 136 | + bloggit.appendChild(post_container) |
| 137 | + } |
| 138 | + |
| 139 | +}) |
| 140 | +}else if(data["type"] == "one"){ |
| 141 | + bloggit_container.className = "container mt-4" |
| 142 | + post = data["post"] |
| 143 | + |
| 144 | + bloggit_container.innerHTML+=`<h1> ${post.title}</h1>` |
| 145 | + if(post.image){ |
| 146 | + bloggit_container.innerHTML += `<img src="http://127.0.0.1:8000${post.image }" class="img-fluid" alt="Imag |
| 147 | +e for the post">` |
| 148 | + } |
| 149 | + bloggit_container.innerHTML += ` |
| 150 | + <p>${convert_datetime(post.time)}</p> |
| 151 | + <div class="mt-4"> |
| 152 | + ${post.body} |
| 153 | + </div>` |
| 154 | +} |
| 155 | +}) |
| 156 | + |
| 157 | +.catch((error) =>{ |
| 158 | + console.log(error) |
| 159 | +}) |
| 160 | + |
0 commit comments