function parseDate(date) { var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear() + ' ' + date.toLocaleTimeString(); } function parseComment(data) { const html = `
${data.firstname + ' ' + data.lastname}

${data.content}

` return html } function parsePost(post, likes) { const html = `
${post.author.firstname + ' ' + post.author.lastname} ${parseDate(new Date(post.time))}

${post.content}

${post.likes} Likes
Like Comment
${post.comments.map(parseComment).join('')}
` return html } function getPost(id) { for (let i = 0; i < data.posts.length; i++) { if (data.posts[i].id === id) { return i } } return -1 } function like(span) { const id = parseInt(span.parentElement.parentElement.getAttribute('postid')) const index = data.user.likes.indexOf(id) if (index === -1) { data.user.likes.push(id) data.posts[getPost(id)].likes++ } else { data.user.likes.splice(index, 1) data.posts[getPost(id)].likes-- } load() } function comment(event) { event.preventDefault(); const text = event.target.elements.text.value.trim(); if (text.length < 1) return; const id = parseInt(event.target.parentElement.parentElement.parentElement.getAttribute('postid')) var index = getPost(id); console.log(index) if (index === -1) return; data.posts[index].comments.push({ firstname: data.user.firstname, lastname: data.user.lastname, content: text }) load() } function post(event) { const text = document.getElementById("text").value.trim() if (text.length < 1) return; data.posts.unshift({ id: data.posts[0].id + 1, author: { firstname: data.user.firstname, lastname: data.user.lastname }, time: Date.now(), content: text, likes: 0, comments: [] }) load() } function load() { const html = `
${data.posts.map(p => parsePost(p, data.user.likes)).join('')}
` add(html, 'posts') const popup = ` ` add(popup, 'popup') } var data = { user: { firstname: 'John', lastname: 'Doe', likes: [1] }, posts: [ { id: 1, author: { firstname: 'Joe', lastname: 'Biden', }, time: 1674269687905, content: '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.', likes: 2, comments: [], }, { id: 0, author: { firstname: 'Amazon', lastname: 'Employee', }, time: 0, content: 'I dont like working at amazon >:(', likes: 69, comments: [ { firstname: 'Jeff', lastname: 'Bezos', content: 'You\'re fired.' }, ], } ] } header(true, false) load();