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

144
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
const postContainer = document.getElementById("post-container");
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
let currentPage = 1;
postTotalElem.innerHTML = postLimit;
@ -24,33 +28,40 @@ function getRandomColor() {
function getUsername() {
const usernames = [
"xXPu55y5l4y3r69Xx",
"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];
const r = Math.floor(Math.random() * users.length);
return users[r].username;
}
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.";
}
class Post {
constructor() {
this.username = getUsername();
this.content = getContent();
constructor(data) {
this.id = data.id;
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() {
@ -73,21 +84,39 @@ class Post {
contentElem.innerHTML = this.content;
postElem.appendChild(contentElem);
// add a random number of comments to the post
const numComments = Math.random() * 5;
for (let i = 0; i < numComments; i++) {
for (let i = 0; i < this.replies.length; i++) {
const reply = this.replies[i];
const commentElem = document.createElement("div");
commentElem.style.backgroundColor = getRandomColor();
const commentUserElem = document.createElement("h2");
commentUserElem.innerHTML = getUsername();
commentUserElem.innerHTML = reply.username;
commentElem.appendChild(commentUserElem);
const commentContentElem = document.createElement("p");
commentContentElem.innerHTML = `comment ${i}`;
commentContentElem.innerHTML = reply.content;
commentElem.appendChild(commentContentElem);
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;
@ -95,9 +124,16 @@ class Post {
}
function createPost() {
const post = new Post();
postContainer.appendChild(post.getElement());
function getRootPosts() {
let result = [];
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;
const rootPosts = getRootPosts();
console.log(rootPosts);
for (let i = startRange + 1; i <= endRange; i++) {
createPost();
postContainer.appendChild(rootPosts[i].getElement());
}
}
@ -151,10 +190,47 @@ function removeInfiniteScroll() {
window.removeEventListener("scroll", handleInfiniteScroll);
}
addPosts(currentPage);
window.addEventListener("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;
// TODO: define the limit of the content to be loaded on the page
// TODO: detect when the user has reached the end of the content container
// TODO: load more content once the end of the container has been reached
// TODO: if there's no more content to be loaded, stop the infinite scroll
console.log(`loaded ${users.length} users and ${posts.length} posts`);
addPosts(currentPage);
window.addEventListener("scroll", handleInfiniteScroll);
}
function loadDataFromEndpoint(endpoint, callback) {
fetch(endpoint)
.then(response => response.json())
.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