diff --git a/src/routes/community/+page.svelte b/src/routes/community/+page.svelte index 5a8b266e..dd16c1b1 100644 --- a/src/routes/community/+page.svelte +++ b/src/routes/community/+page.svelte @@ -310,25 +310,32 @@ function updateVote(posts, postVote) { // Find the post by ID const post = posts?.find(post => post?.id === id); - + if (post) { post.upvote = upvote; post.downvote = downvote; + // Check if expand['alreadyVoted(post)'] exists + if (post?.expand['alreadyVoted(post)'] && post?.expand['alreadyVoted(post)']?.length > 0) { + // Find the vote entry for the current user, if it exists + const userVote = post?.expand['alreadyVoted(post)']?.find(vote => vote.user === data?.user?.id); - // Check if expand['alreadyVoted(post)'] exists - if (!post.expand['alreadyVoted(post)']) { - // Create the structure if it does not exist - post['expand']['alreadyVoted(post)'] = [ - { - type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral', - user: data?.user?.id - } - ]; - + if (userVote) { + // Update the existing vote for the user + userVote.type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral'; } else { - // Update the existing type based on the click flags - post.expand['alreadyVoted(post)'][0].type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral'; + // If no vote entry for the user, add a new one + post.expand['alreadyVoted(post)']?.push({ + type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral', + user: data?.user?.id + }); } + } else { + // Create the structure if it does not exist + post.expand['alreadyVoted(post)'] = [{ + type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral', + user: data?.user?.id + }]; + } } else { console.log("Post not found."); @@ -347,7 +354,7 @@ $: { $: { - if($postVote && Object?.keys($postVote).length !== 0) + if($postVote && Object?.keys($postVote).length !== 0 && data?.user?.id) { //Update in realtime the already downloaded posts list when user votes posts = updateVote(posts, $postVote) diff --git a/src/routes/community/post/[postId]/+page.svelte b/src/routes/community/post/[postId]/+page.svelte index 570cddd7..acf734ab 100644 --- a/src/routes/community/post/[postId]/+page.svelte +++ b/src/routes/community/post/[postId]/+page.svelte @@ -364,37 +364,46 @@ }) -// Function to update the vote when posts are cached + + +// Function to update the vote function updateVote(postVote) { const { id, upvote, downvote, upvoteClicked, downvoteClicked } = postVote; - console.log(postVote) + // Find the post by ID const item = $cachedPosts?.posts?.find(post => post?.id === id); - console.log(item) + if (item) { item.upvote = upvote; item.downvote = downvote; + // Check if expand['alreadyVoted(post)'] exists + if (item?.expand['alreadyVoted(post)'] && item?.expand['alreadyVoted(post)']?.length > 0) { + // Find the vote entry for the current user, if it exists + const userVote = item?.expand['alreadyVoted(post)']?.find(vote => vote.user === data?.user?.id); - // Check if expand['alreadyVoted(item)'] exists - if (!item.expand['alreadyVoted(item)']) { - // Create the structure if it does not exist - item['expand']['alreadyVoted(post)'] = [ - { - type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral', - user: data?.user?.id - } - ]; + if (userVote) { + // Update the existing vote for the user + userVote.type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral'; } else { - // Update the existing type based on the click flags - item.expand['alreadyVoted(post)'][0].type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral'; + // If no vote entry for the user, add a new one + item?.expand['alreadyVoted(post)']?.push({ + type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral', + user: data?.user?.id + }); } + } else { + // Create the structure if it does not exist + item.expand['alreadyVoted(post)'] = [{ + type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral', + user: data?.user?.id + }]; + } } else { console.log("Post not found."); } + console.log(item) } - - function addCommentToParent(comments, newComment) { // Helper function to handle the recursion