bugfixing
This commit is contained in:
parent
6829493cf6
commit
2d712844ce
@ -314,20 +314,27 @@ function updateVote(posts, postVote) {
|
||||
if (post) {
|
||||
post.upvote = upvote;
|
||||
post.downvote = downvote;
|
||||
|
||||
// Check if expand['alreadyVoted(post)'] exists
|
||||
if (!post.expand['alreadyVoted(post)']) {
|
||||
// Create the structure if it does not exist
|
||||
post['expand']['alreadyVoted(post)'] = [
|
||||
{
|
||||
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);
|
||||
|
||||
if (userVote) {
|
||||
// Update the existing vote for the user
|
||||
userVote.type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
||||
} else {
|
||||
// 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 {
|
||||
// Update the existing type based on the click flags
|
||||
post.expand['alreadyVoted(post)'][0].type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
||||
// Create the structure if it does not exist
|
||||
post.expand['alreadyVoted(post)'] = [{
|
||||
type: upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral',
|
||||
user: data?.user?.id
|
||||
}];
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -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)
|
||||
|
||||
@ -364,38 +364,47 @@
|
||||
})
|
||||
|
||||
|
||||
// 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)'] = [
|
||||
{
|
||||
if (userVote) {
|
||||
// Update the existing vote for the user
|
||||
userVote.type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
||||
} else {
|
||||
// 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 {
|
||||
// Update the existing type based on the click flags
|
||||
item.expand['alreadyVoted(post)'][0].type = upvoteClicked ? 'upvote' : downvoteClicked ? 'downvote' : 'neutral';
|
||||
// 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
|
||||
function findAndAddParent(commentsList, newComment) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user