feat: fix vote logic

This commit is contained in:
UranusLin
2024-06-02 22:58:06 +08:00
parent 6063922373
commit 4003653eb0
4 changed files with 4 additions and 63 deletions

View File

@@ -16,6 +16,7 @@ import {
import { Avatar } from '@/features/shared'
import { PostStatus } from '@/types/Post'
import { VoteAction } from '@/types/Vote'
import { useEpoch } from '@/features/core'
export default function Post({
id = '',
@@ -116,17 +117,6 @@ export default function Post({
: VoteAction.DOWNVOTE,
)
setIsAction(voteType)
if (voteType === VoteAction.UPVOTE) {
setLocalUpCount(localUpCount + 1)
if (isAction === VoteAction.DOWNVOTE) {
setLocalDownCount(localDownCount - 1)
}
} else {
setLocalDownCount(localDownCount + 1)
if (isAction === VoteAction.UPVOTE) {
setLocalUpCount(localUpCount - 1)
}
}
setTimeout(() => setShow(false), 500)
}

View File

@@ -30,7 +30,7 @@ export default function PostList() {
const queryClient = useQueryClient()
const { data, fetchNextPage, hasNextPage } = useInfiniteQuery<
const { data, fetchNextPage, hasNextPage, refetch } = useInfiniteQuery<
PostInfo[],
DefaultError,
InfiniteData<PostInfo[]>,
@@ -118,7 +118,6 @@ export default function PostList() {
navigate(`/posts/${postId}/#comments`)
}
const [postsState, setPostsState] = useState<PostInfo[]>(localPosts)
const { createVote } = useVotes()
const handleVote = async (
@@ -127,25 +126,12 @@ export default function PostList() {
post: PostInfo,
): Promise<boolean> => {
try {
console.log(
'isMine',
post.isMine,
'voteType',
voteType,
'votedNonce',
post.votedNonce,
'votedEpoch',
post.votedEpoch,
)
// if isMine is true, it means the user has already voted on this post, so need cancel the vote first
if (post.isMine) {
let cancelAction: VoteAction
if (post.finalAction === VoteAction.UPVOTE) {
cancelAction = VoteAction.CANCEL_UPVOTE
post.upCount -= 1
} else if (post.finalAction === VoteAction.DOWNVOTE) {
cancelAction = VoteAction.CANCEL_DOWNVOTE
post.downCount -= 1
} else {
throw new Error('Invalid finalAction')
}
@@ -164,26 +150,9 @@ export default function PostList() {
votedNonce: null,
votedEpoch: null,
})
if (voteType === VoteAction.UPVOTE) {
post.upCount += 1
} else if (voteType === VoteAction.DOWNVOTE) {
post.downCount += 1
}
}
setPostsState((prevPosts) =>
prevPosts.map((prevPost) =>
prevPost.id === id
? {
...prevPost,
upCount: post.upCount,
downCount: post.downCount,
isMine: true,
finalAction: voteType,
}
: prevPost,
),
)
refetch() // Refresh the posts data after voting
return true
} catch (err) {
@@ -197,7 +166,7 @@ export default function PostList() {
return (
<div className="px-4">
<ul className="space-y-3 md:space-y-6">
{postsState.map((post) => (
{localPosts.map((post) => (
<li
key={post.id}
className="transition-opacity duration-500"

View File

@@ -35,14 +35,6 @@ export function useVotes() {
votedEpoch: number | null
}) => {
const userState = await getGuaranteedUserState()
console.log(
'voteAction',
voteAction,
'voteNonce',
votedNonce,
'voteEpoch',
votedEpoch,
)
let epochKeyProof
if (votedNonce !== null && votedEpoch !== null) {
epochKeyProof = await userState.genEpochKeyProof({

View File

@@ -29,16 +29,6 @@ const checkVoteIsMine = (votes: RelayRawVote[], userState: any) => {
}
}
}
console.log(
'isMine',
isMine,
'finalAction',
finalAction,
'votedNonce',
votedNonce,
'votedEpoch',
votedEpoch,
)
return { isMine, finalAction, votedNonce, votedEpoch }
}