Skip to content

Commit

Permalink
refactor(comment): improve voting functionality (#1323)
Browse files Browse the repository at this point in the history
  • Loading branch information
nawinsharma authored Oct 3, 2024
1 parent 4a88e5b commit 7ca2c08
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/components/comment/CommentVoteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,43 @@ const CommentVoteForm: React.FC<CommentVoteFormProps> = ({
},
});

const handleVote = (voteType: VoteType) => {
const handleVote = (newVoteType: VoteType) => {
let toastMessage = {
loading: 'Processing vote...',
success: 'Vote updated successfully.',
error: 'Error updating vote.',
};

if (voteType === newVoteType) {
toastMessage = {
loading: 'Removing vote...',
success: `Vote removed successfully.`,
error: 'Error removing vote.',
};
} else if (voteType === null) {
toastMessage = {
loading:
newVoteType === VoteType.UPVOTE ? 'Upvoting...' : 'Downvoting...',
success:
newVoteType === VoteType.UPVOTE
? 'Comment upvoted.'
: 'Comment downvoted.',
error: 'Error casting vote.',
};
} else {
toastMessage = {
loading: 'Changing vote...',
success:
newVoteType === VoteType.UPVOTE
? 'Changed to upvote.'
: 'Changed to downvote.',
error: 'Error changing vote.',
};
}

toast.promise(
execute({ voteType, commentId, currentPath }),
voteType === VoteType.DOWNVOTE
? {
loading: 'Downvoting...',
success: 'Comment has been downvoted.',
error: 'Error',
}
: {
loading: 'Upvoting...',
success: 'Comment has been upvoted.',
error: 'Error',
},
execute({ voteType: newVoteType, commentId, currentPath }),
toastMessage,
);
};

Expand Down Expand Up @@ -99,4 +122,4 @@ const CommentVoteForm: React.FC<CommentVoteFormProps> = ({
);
};

export default CommentVoteForm;
export default CommentVoteForm;

0 comments on commit 7ca2c08

Please sign in to comment.