bugfixing
This commit is contained in:
parent
6829493cf6
commit
2d712844ce
@ -310,25 +310,32 @@ function updateVote(posts, postVote) {
|
|||||||
|
|
||||||
// Find the post by ID
|
// Find the post by ID
|
||||||
const post = posts?.find(post => post?.id === id);
|
const post = posts?.find(post => post?.id === id);
|
||||||
|
|
||||||
if (post) {
|
if (post) {
|
||||||
post.upvote = upvote;
|
post.upvote = upvote;
|
||||||
post.downvote = downvote;
|
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 (userVote) {
|
||||||
if (!post.expand['alreadyVoted(post)']) {
|
// Update the existing vote for the user
|
||||||
// Create the structure if it does not exist
|
userVote.type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
||||||
post['expand']['alreadyVoted(post)'] = [
|
|
||||||
{
|
|
||||||
type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral',
|
|
||||||
user: data?.user?.id
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Update the existing type based on the click flags
|
// If no vote entry for the user, add a new one
|
||||||
post.expand['alreadyVoted(post)'][0].type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
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 {
|
} else {
|
||||||
console.log("Post not found.");
|
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
|
//Update in realtime the already downloaded posts list when user votes
|
||||||
posts = updateVote(posts, $postVote)
|
posts = updateVote(posts, $postVote)
|
||||||
|
|||||||
@ -364,37 +364,46 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
// Function to update the vote when posts are cached
|
|
||||||
|
|
||||||
|
// Function to update the vote
|
||||||
function updateVote(postVote) {
|
function updateVote(postVote) {
|
||||||
const { id, upvote, downvote, upvoteClicked, downvoteClicked } = postVote;
|
const { id, upvote, downvote, upvoteClicked, downvoteClicked } = postVote;
|
||||||
console.log(postVote)
|
|
||||||
// Find the post by ID
|
// Find the post by ID
|
||||||
const item = $cachedPosts?.posts?.find(post => post?.id === id);
|
const item = $cachedPosts?.posts?.find(post => post?.id === id);
|
||||||
console.log(item)
|
|
||||||
if (item) {
|
if (item) {
|
||||||
item.upvote = upvote;
|
item.upvote = upvote;
|
||||||
item.downvote = downvote;
|
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 (userVote) {
|
||||||
if (!item.expand['alreadyVoted(item)']) {
|
// Update the existing vote for the user
|
||||||
// Create the structure if it does not exist
|
userVote.type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
||||||
item['expand']['alreadyVoted(post)'] = [
|
|
||||||
{
|
|
||||||
type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral',
|
|
||||||
user: data?.user?.id
|
|
||||||
}
|
|
||||||
];
|
|
||||||
} else {
|
} else {
|
||||||
// Update the existing type based on the click flags
|
// If no vote entry for the user, add a new one
|
||||||
item.expand['alreadyVoted(post)'][0].type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
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 {
|
} else {
|
||||||
console.log("Post not found.");
|
console.log("Post not found.");
|
||||||
}
|
}
|
||||||
|
console.log(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function addCommentToParent(comments, newComment) {
|
function addCommentToParent(comments, newComment) {
|
||||||
// Helper function to handle the recursion
|
// Helper function to handle the recursion
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user