diff --git a/src/lib/components/PostSection.svelte b/src/lib/components/PostSection.svelte index a9a70f37..4250ebc1 100644 --- a/src/lib/components/PostSection.svelte +++ b/src/lib/components/PostSection.svelte @@ -6,7 +6,7 @@ import Downvote from '$lib/components/Downvote.svelte'; import Upvote from '$lib/components/Upvote.svelte'; - import {userRegion, tagList, postIdDeleted} from '$lib/store'; + import {userRegion, postVote, tagList, postIdDeleted} from '$lib/store'; import toast from 'svelte-french-toast'; @@ -144,6 +144,9 @@ const handleUpvote = async (event) => { } else { upvoteCounter[postId]--; } + + $postVote = {'id': postId, 'upvote': upvoteCounter[postId], 'downvote': downvoteCounter[postId], 'upvoteClicked': upvoteButtonClicked[postId], 'downvoteClicked': downvoteButtonClicked[postId]}; + const response = await fetch(fastifyURL+'/upvote', { method: 'POST', headers: { @@ -178,6 +181,7 @@ const handleDownvote = async (event) => { downvoteCounter[postId]--; } + $postVote = {'id': postId, 'upvote': upvoteCounter[postId], 'downvote': downvoteCounter[postId], 'upvoteClicked': upvoteButtonClicked[postId], 'downvoteClicked': downvoteButtonClicked[postId]}; const response = await fetch(fastifyURL+'/downvote', { method: 'POST', diff --git a/src/lib/store.ts b/src/lib/store.ts index 65986cda..b1d7cfc7 100644 --- a/src/lib/store.ts +++ b/src/lib/store.ts @@ -82,6 +82,7 @@ export const switchWatchList = writable((false)); export const cachedPosts = writable(> {}); export const currentPagePosition = writable( (0)); +export const postVote = writable(> {}); export const similarTickerClicked = writable((false)); diff --git a/src/routes/community/+page.svelte b/src/routes/community/+page.svelte index 4d42b895..6ee9b9af 100644 --- a/src/routes/community/+page.svelte +++ b/src/routes/community/+page.svelte @@ -1,7 +1,7 @@ diff --git a/src/routes/community/post/[postId]/+page.svelte b/src/routes/community/post/[postId]/+page.svelte index f837fc2f..849f30f9 100644 --- a/src/routes/community/post/[postId]/+page.svelte +++ b/src/routes/community/post/[postId]/+page.svelte @@ -9,7 +9,7 @@ import { onMount, onDestroy } from 'svelte'; - import {userRegion, screenWidth, scrollToComment, postIdDeleted, setCache, getCache, tagList, numberOfUnreadNotification, commentAdded, commentUpdated, commentIdDeleted } from '$lib/store'; + import {userRegion, cachedPosts, postVote, screenWidth, scrollToComment, postIdDeleted, setCache, getCache, tagList, numberOfUnreadNotification, commentAdded, commentUpdated, commentIdDeleted } from '$lib/store'; import { goto, afterNavigate } from '$app/navigation'; import { base } from '$app/paths' @@ -68,6 +68,9 @@ } else { upvoteCounter[postId]--; } + + $postVote = {'id': postId, 'upvote': upvoteCounter[postId], 'downvote': downvoteCounter[postId], 'upvoteClicked': upvoteButtonClicked[postId], 'downvoteClicked': downvoteButtonClicked[postId]}; + const response = await fetch(fastifyURL+'/upvote', { method: 'POST', headers: { @@ -102,7 +105,8 @@ downvoteCounter[postId]--; } - + $postVote = {'id': postId, 'upvote': upvoteCounter[postId], 'downvote': downvoteCounter[postId], 'upvoteClicked': upvoteButtonClicked[postId], 'downvoteClicked': downvoteButtonClicked[postId]}; + const response = await fetch(fastifyURL+'/downvote', { method: 'POST', headers: { @@ -148,7 +152,7 @@ const handleReport = async () => { - dropdownOpen = !dropdownOpen; + dropdownOpen = !dropdownOpen; toast.success('Post has been reported. Thank you!', { style: 'border-radius: 200px; background: #333; color: #fff;' }); @@ -356,8 +360,41 @@ $commentAdded = ''; $commentIdDeleted = ''; $scrollToComment = ''; + $postVote = {}; }) + +// Function to update the vote when posts are cached +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(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 + } + ]; + } else { + // Update the existing type based on the click flags + item.expand['alreadyVoted(post)'][0].type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral'; + } + + } else { + console.log("Post not found."); + } +} + + function addCommentToParent(comments, newComment) { // Helper function to handle the recursion @@ -466,7 +503,17 @@ } - + $: { + if($postVote && Object?.keys($postVote).length !== 0) + { + //Update in realtime the already downloaded posts list when user votes in cached posts + updateVote($postVote) + //console.log(posts?.at(0)) + $postVote = {}; + } + +} + @@ -480,7 +527,6 @@ - @@ -488,7 +534,6 @@ - @@ -523,7 +568,7 @@