feat: determine post count from data

This commit is contained in:
ktyl 2024-07-20 12:55:10 +01:00
parent ecade5608e
commit 5f62b08fd1
1 changed files with 9 additions and 10 deletions

19
main.js
View File

@ -6,19 +6,16 @@ const postCountElem = document.getElementById("post-count");
const postTotalElem = document.getElementById("post-total"); const postTotalElem = document.getElementById("post-total");
const loader = document.getElementById("loader"); const loader = document.getElementById("loader");
// we'll need a value for the max numaer of posts to be added to the page
const postLimit = 99;
// then we'll define a variable for how many posts we want to increase the // then we'll define a variable for how many posts we want to increase the
// page by // page by
const postIncrease = 9; const postIncrease = 9;
// how many times can we increase the content until we reach the max limit?
const pageCount = Math.ceil(postLimit / postIncrease);
// and define a value to determine which page we're on // and define a value to determine which page we're on
let currentPage = 1; let currentPage = 1;
// how many times can we increase the content until we reach the max limit?
postTotalElem.innerHTML = postLimit; function getPageCount() {
return Math.ceil(Object.keys(posts).length / postIncrease);
}
function getRandomColor() { function getRandomColor() {
const h = Math.floor(Math.random() * 360); const h = Math.floor(Math.random() * 360);
@ -140,8 +137,8 @@ function addPosts(pageIdx) {
currentPage = pageIdx; currentPage = pageIdx;
const startRange = (pageIdx - 1) * postIncrease; const startRange = (pageIdx - 1) * postIncrease;
const endRange = currentPage == pageCount const endRange = currentPage == getPageCount()
? postLimit ? posts.length
: pageIdx * postIncrease; : pageIdx * postIncrease;
postCountElem.innerHTML = endRange; postCountElem.innerHTML = endRange;
@ -162,7 +159,7 @@ function handleInfiniteScroll() {
addPosts(currentPage + 1); addPosts(currentPage + 1);
} }
if (currentPage === pageCount) { if (currentPage === getPageCount()) {
removeInfiniteScroll(); removeInfiniteScroll();
} }
}, 1000); }, 1000);
@ -305,4 +302,6 @@ loadDataFromEndpoint("https://api.wayfarer.games/singularity/posts.json", json =
const parent = posts[post.replyTo]; const parent = posts[post.replyTo];
parent.addReply(post); parent.addReply(post);
} }
postTotalElem.innerHTML = Object.keys(posts).length;
}); });