Skip to content
Snippets Groups Projects
Commit 9f017d78 authored by Richard Githuba's avatar Richard Githuba
Browse files

changes to feed.js

parent 3a1940cf
Branches
No related tags found
No related merge requests found
......@@ -41,7 +41,7 @@ function renderPost(post) {
postImage.style.display = 'none';
}
// Render tags
// render the tags
const tagsContainer = postElement.querySelector('.post-tags');
if (post.tags && post.tags.length > 0) {
post.tags.forEach(tag => {
......@@ -51,15 +51,15 @@ function renderPost(post) {
});
}
// Set likes count
// set the count of the likes
const likeCount = postElement.querySelector('.like-count');
likeCount.textContent = post.likesCount || 0;
// Set timestamp
// timestamp
const timestamp = postElement.querySelector('.timestamp');
timestamp.textContent = new Date(post.postTime).toLocaleDateString();
// Add like button functionality
// add a like when presses
const likeButton = postElement.querySelector('.like-button');
likeButton.addEventListener('click', () => handleLike(post.postId, likeCount));
......@@ -70,10 +70,10 @@ function renderPost(post) {
postFeed.appendChild(postElement);
}
// Handle like button click
// add a like if user had not already liked and remove a like if already liked
async function handleLike(postId, likeCountElement) {
try {
// Check if user has already liked the post
// check if user had already liked
const checkResponse = await fetch(`${API_BASE_URL}/${postId}/hasLiked?userId=${MOCK_USER_ID}`);
const hasLiked = await checkResponse.json();
......@@ -84,11 +84,11 @@ async function handleLike(postId, likeCountElement) {
if (!response.ok) throw new Error('Failed to update like');
// Update like count in UI
// update like count appropriately
const currentCount = parseInt(likeCountElement.textContent);
likeCountElement.textContent = hasLiked ? currentCount - 1 : currentCount + 1;
// Toggle like button appearance
// change color to show they have liked already
const button = likeCountElement.closest('.like-button');
button.classList.toggle('liked', !hasLiked);
......@@ -97,10 +97,9 @@ async function handleLike(postId, likeCountElement) {
}
}
// Initialize the feed
document.addEventListener('DOMContentLoaded', () => {
fetchPosts();
});
// Optional: Add auto-refresh functionality
// setInterval(fetchPosts, 60000); // Refresh every minute
\ No newline at end of file
// refresh posts each minute , though cannot show at moment since not online
setInterval(fetchPosts, 60000);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment