({
questionId,
answerId,
upvotes = 0,
downvotes = 0,
votesArr,
slug,
})
| 19 | } |
| 20 | |
| 21 | const VoteForm: React.FC<IVoteFormProps> = ({ |
| 22 | questionId, |
| 23 | answerId, |
| 24 | upvotes = 0, |
| 25 | downvotes = 0, |
| 26 | votesArr, |
| 27 | slug, |
| 28 | }) => { |
| 29 | const currentPath = usePathname(); |
| 30 | const { execute } = useAction(voteHandlerAction, { |
| 31 | onSuccess: () => {}, |
| 32 | onError: (error) => { |
| 33 | toast.error(error); |
| 34 | }, |
| 35 | }); |
| 36 | |
| 37 | const handleVote = (voteType: VoteType) => { |
| 38 | const isRemovingVote = userVoted && userVoteVal.voteType === voteType; |
| 39 | |
| 40 | toast.promise( |
| 41 | execute({ voteType, questionId, answerId, currentPath, slug }), |
| 42 | isRemovingVote |
| 43 | ? { |
| 44 | loading: `Removing ${voteType.toLowerCase()}...`, |
| 45 | success: `Your ${voteType.toLowerCase()} has been removed.`, |
| 46 | error: 'Error', |
| 47 | } |
| 48 | : { |
| 49 | loading: `${voteType === VoteType.UPVOTE ? 'Upvoting' : 'Downvoting'}...`, |
| 50 | success: `Question has been ${voteType === VoteType.UPVOTE ? 'upvoted' : 'downvoted'}.`, |
| 51 | error: 'Error', |
| 52 | } |
| 53 | ); |
| 54 | }; |
| 55 | |
| 56 | const userVoted = Boolean(votesArr.length); |
| 57 | const userVoteVal = votesArr[0]; |
| 58 | |
| 59 | return ( |
| 60 | <div className="flex gap-2"> |
| 61 | <button |
| 62 | className="m-auto" |
| 63 | onClick={(e) => { |
| 64 | e.stopPropagation(); |
| 65 | handleVote(VoteType.UPVOTE); |
| 66 | }} |
| 67 | > |
| 68 | <div |
| 69 | className={`flex min-w-8 items-center gap-1 rounded-full px-4 py-1 text-lg transition-all duration-300 ${ |
| 70 | userVoted && userVoteVal.voteType === VoteType.UPVOTE |
| 71 | ? 'bg-green-500/20 text-green-500' |
| 72 | : 'bg-primary/10 text-primary hover:bg-green-500/20 hover:text-green-500' |
| 73 | }`} |
| 74 | > |
| 75 | <ArrowBigUp |
| 76 | className={`size-5 ${ |
| 77 | userVoted && userVoteVal.voteType === VoteType.UPVOTE |
| 78 | ? 'text-green-500' |
nothing calls this directly
no test coverage detected