feat: add post and user data

This commit is contained in:
ktyl 2024-07-14 01:46:48 +01:00
parent 8ed12bf7bc
commit 7a2455e8da
3 changed files with 112 additions and 34 deletions

140
main.js
View File

@ -1,3 +1,6 @@
var users = [];
var posts = {};
// first, let's get all the elements we'll need from our DOM // first, let's get all the elements we'll need from our DOM
const postContainer = document.getElementById("post-container"); const postContainer = document.getElementById("post-container");
const postCountElem = document.getElementById("post-count"); const postCountElem = document.getElementById("post-count");
@ -14,6 +17,7 @@ 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;
postTotalElem.innerHTML = postLimit; postTotalElem.innerHTML = postLimit;
@ -24,33 +28,40 @@ function getRandomColor() {
function getUsername() { function getUsername() {
const usernames = [ const r = Math.floor(Math.random() * users.length);
"xXPu55y5l4y3r69Xx", return users[r].username;
"Keef Farmer",
"Alan",
"mongle boy",
"girlthing",
"doggle poggle",
"catlicker",
"350 million for the nhs",
"thoughts were a mistake",
"410,757,864,530 FRIENDS MADE"
];
const r = Math.floor(Math.random() * usernames.length);
return usernames[r];
} }
function getContent() { function getContent(username) {
let user = null;
for (let i = 0; i < users.length; i++) {
if (users[i].username == username) {
user = users[i];
break;
}
}
return "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; return "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
} }
class Post { class Post {
constructor() { constructor(data) {
this.username = getUsername(); this.id = data.id;
this.content = getContent(); this.username = data.associatedUser;
this.content = data.body;
this.replyTo = data.replyTo;
this.replies = []
}
getIsReply() {
return this.replyTo != "";
}
addReply(reply) {
this.replies.push(reply);
} }
getElement() { getElement() {
@ -73,21 +84,39 @@ class Post {
contentElem.innerHTML = this.content; contentElem.innerHTML = this.content;
postElem.appendChild(contentElem); postElem.appendChild(contentElem);
// add a random number of comments to the post for (let i = 0; i < this.replies.length; i++) {
const numComments = Math.random() * 5; const reply = this.replies[i];
for (let i = 0; i < numComments; i++) {
const commentElem = document.createElement("div"); const commentElem = document.createElement("div");
commentElem.style.backgroundColor = getRandomColor(); commentElem.style.backgroundColor = getRandomColor();
const commentUserElem = document.createElement("h2"); const commentUserElem = document.createElement("h2");
commentUserElem.innerHTML = getUsername(); commentUserElem.innerHTML = reply.username;
commentElem.appendChild(commentUserElem); commentElem.appendChild(commentUserElem);
const commentContentElem = document.createElement("p"); const commentContentElem = document.createElement("p");
commentContentElem.innerHTML = `comment ${i}`; commentContentElem.innerHTML = reply.content;
commentElem.appendChild(commentContentElem); commentElem.appendChild(commentContentElem);
postElem.appendChild(commentElem); postElem.appendChild(commentElem);
// TODO: indent 2nd-level replies
for (let j = 0; j < reply.replies.length; j++) {
const replyReply = reply.replies[j];
const replyReplyElem = document.createElement("div");
replyReplyElem.style.backgroundColor = getRandomColor();
const replyReplyUserElem = document.createElement("h3");
replyReplyUserElem.innerHTML = replyReply.username;
replyReplyElem.appendChild(replyReplyUserElem);
const replyReplyContentElem = document.createElement("p");
replyReplyContentElem.innerHTML = replyReply.content;
replyReplyElem.appendChild(replyReplyContentElem);
postElem.appendChild(replyReplyElem);
}
} }
return postElem; return postElem;
@ -95,9 +124,16 @@ class Post {
} }
function createPost() { function getRootPosts() {
const post = new Post(); let result = [];
postContainer.appendChild(post.getElement()); for (var id in posts) {
const post = posts[id];
if (post.getIsReply())
continue;
result.push(post);
}
return result;
} }
@ -111,8 +147,11 @@ function addPosts(pageIdx) {
postCountElem.innerHTML = endRange; postCountElem.innerHTML = endRange;
const rootPosts = getRootPosts();
console.log(rootPosts);
for (let i = startRange + 1; i <= endRange; i++) { for (let i = startRange + 1; i <= endRange; i++) {
createPost(); postContainer.appendChild(rootPosts[i].getElement());
} }
} }
@ -151,10 +190,47 @@ function removeInfiniteScroll() {
window.removeEventListener("scroll", handleInfiniteScroll); window.removeEventListener("scroll", handleInfiniteScroll);
} }
function init() {
if (posts == undefined)
{
console.log("resource loading failed");
return;
}
// need to load all the resources first
if (users.length == 0 || Object.keys(posts).length == 0)
return;
console.log(`loaded ${users.length} users and ${posts.length} posts`);
addPosts(currentPage); addPosts(currentPage);
window.addEventListener("scroll", handleInfiniteScroll); window.addEventListener("scroll", handleInfiniteScroll);
}
// TODO: define the limit of the content to be loaded on the page function loadDataFromEndpoint(endpoint, callback) {
// TODO: detect when the user has reached the end of the content container fetch(endpoint)
// TODO: load more content once the end of the container has been reached .then(response => response.json())
// TODO: if there's no more content to be loaded, stop the infinite scroll .then(json => {
callback(json);
init();
});
}
loadDataFromEndpoint("users.json", json => { users = json.users; });
loadDataFromEndpoint("posts.json", json => {
// first pass to instantiate all the posts
for (let i = 0; i < json.content.length; i++) {
const post = new Post(json.content[i]);
posts[post.id] = post;
}
// second pass to link each reply to the appropriate parent
for (const id in posts) {
const post = posts[id];
if (!post.getIsReply())
continue;
const parent = posts[post.replyTo];
parent.addReply(post);
}
});

1
posts.json Normal file

File diff suppressed because one or more lines are too long

1
users.json Normal file

File diff suppressed because one or more lines are too long