|
@ -17,10 +17,7 @@ const { |
|
|
contracts: { |
|
|
contracts: { |
|
|
[POST_VOTING_CONTRACT]: { |
|
|
[POST_VOTING_CONTRACT]: { |
|
|
methods: { |
|
|
methods: { |
|
|
getVote: { cacheCall: getVoteChainData }, |
|
|
getVoteInfo: { cacheCall: getVoteInfoChainData }, |
|
|
getTotalVoteCount: { cacheCall: getTotalVoteCountChainData }, |
|
|
|
|
|
getUpvoteCount: { cacheCall: getUpvoteCountChainData }, |
|
|
|
|
|
getDownvoteCount: { cacheCall: getDownvoteCountChainData }, |
|
|
|
|
|
upvote, downvote, unvote, |
|
|
upvote, downvote, unvote, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
@ -29,21 +26,13 @@ const { |
|
|
|
|
|
|
|
|
const PostVoting = (props) => { |
|
|
const PostVoting = (props) => { |
|
|
const { postId, postAuthorAddress } = props; |
|
|
const { postId, postAuthorAddress } = props; |
|
|
const drizzleInitialized = useSelector((state) => state.drizzleStatus.initialized); |
|
|
|
|
|
const drizzleInitializationFailed = useSelector((state) => state.drizzleStatus.failed); |
|
|
|
|
|
const hasSignedUp = useSelector((state) => state.user.hasSignedUp); |
|
|
const hasSignedUp = useSelector((state) => state.user.hasSignedUp); |
|
|
const userAccount = useSelector((state) => state.accounts[0]); |
|
|
const userAccount = useSelector((state) => state.accounts[0]); |
|
|
|
|
|
|
|
|
// Current votes |
|
|
// Current votes |
|
|
const [getVoteCallHash, setGetVoteCallHash] = useState(null); |
|
|
const [getVoteInfoCallHash, setGetVoteInfoCallHash] = useState(null); |
|
|
const [getTotalVoteCountCallHash, setGetTotalVoteCountCallHash] = useState(null); |
|
|
|
|
|
const [getUpvoteCountCallHash, setGetUpvoteCountCallHash] = useState(null); |
|
|
|
|
|
const [getDownvoteCountCallHash, setGetDownvoteCountCallHash] = useState(null); |
|
|
|
|
|
|
|
|
|
|
|
const getVoteResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getVote[getVoteCallHash]); |
|
|
const getVoteInfoResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getVoteInfo[getVoteInfoCallHash]); |
|
|
const getTotalVoteCountResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getTotalVoteCount[getTotalVoteCountCallHash]); |
|
|
|
|
|
const getUpvoteCountResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getUpvoteCount[getUpvoteCountCallHash]); |
|
|
|
|
|
const getDownvoteCountResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getDownvoteCount[getDownvoteCountCallHash]); |
|
|
|
|
|
|
|
|
|
|
|
const [ownVote, setOwnVote] = useState(null); |
|
|
const [ownVote, setOwnVote] = useState(null); |
|
|
const [totalVoteCount, setTotalVoteCount] = useState(null); |
|
|
const [totalVoteCount, setTotalVoteCount] = useState(null); |
|
@ -58,46 +47,19 @@ const PostVoting = (props) => { |
|
|
|
|
|
|
|
|
// Current votes |
|
|
// Current votes |
|
|
useEffect(() => { |
|
|
useEffect(() => { |
|
|
if (drizzleInitialized && !drizzleInitializationFailed && postId !== null) { |
|
|
if (postId !== null && getVoteInfoCallHash === null) { |
|
|
if (getTotalVoteCountCallHash === null) setGetTotalVoteCountCallHash(getTotalVoteCountChainData(postId)); |
|
|
setGetVoteInfoCallHash(getVoteInfoChainData(postId)); |
|
|
if (getUpvoteCountCallHash === null) setGetUpvoteCountCallHash(getUpvoteCountChainData(postId)); |
|
|
|
|
|
if (getDownvoteCountCallHash === null) setGetDownvoteCountCallHash(getDownvoteCountChainData(postId)); |
|
|
|
|
|
} |
|
|
} |
|
|
}, [drizzleInitializationFailed, drizzleInitialized, getDownvoteCountCallHash, |
|
|
}, [getVoteInfoCallHash, postId]); |
|
|
getTotalVoteCountCallHash, getUpvoteCountCallHash, postId]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
useEffect(() => { |
|
|
const shouldGetOwnVoteFromChain = ownVote === null; |
|
|
if (getVoteInfoResult) { |
|
|
|
|
|
setOwnVote(getVoteInfoResult.value[0]); |
|
|
if (drizzleInitialized && !drizzleInitializationFailed && shouldGetOwnVoteFromChain |
|
|
setTotalVoteCount(getVoteInfoResult.value[1]); |
|
|
&& postId !== null && userAccount !== null && getVoteCallHash === null) { |
|
|
setUpvoteCount(getVoteInfoResult.value[2]); |
|
|
setGetVoteCallHash(getVoteChainData(postId, userAccount)); |
|
|
setDownvoteCount(getVoteInfoResult.value[3]); |
|
|
} |
|
|
|
|
|
}, [drizzleInitializationFailed, drizzleInitialized, getVoteCallHash, ownVote, postId, userAccount]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (getVoteResult) { |
|
|
|
|
|
setOwnVote(getVoteResult.value); |
|
|
|
|
|
} |
|
|
|
|
|
}, [getVoteResult]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (getTotalVoteCountResult) { |
|
|
|
|
|
setTotalVoteCount(getTotalVoteCountResult.value); |
|
|
|
|
|
} |
|
|
} |
|
|
}, [getTotalVoteCountResult]); |
|
|
}, [getVoteInfoResult]); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (getUpvoteCountResult) { |
|
|
|
|
|
setUpvoteCount(getUpvoteCountResult.value); |
|
|
|
|
|
} |
|
|
|
|
|
}, [getUpvoteCountResult]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (getDownvoteCountResult) { |
|
|
|
|
|
setDownvoteCount(getDownvoteCountResult.value); |
|
|
|
|
|
} |
|
|
|
|
|
}, [getDownvoteCountResult]); |
|
|
|
|
|
|
|
|
|
|
|
// Voting |
|
|
// Voting |
|
|
useEffect(() => { |
|
|
useEffect(() => { |
|
@ -110,13 +72,15 @@ const PostVoting = (props) => { |
|
|
} |
|
|
} |
|
|
}, [createVoteCacheSendStackId, transactionStack, transactions, voting]); |
|
|
}, [createVoteCacheSendStackId, transactionStack, transactions, voting]); |
|
|
|
|
|
|
|
|
const vote = useCallback((choice) => { |
|
|
const vote = useCallback((e, choice) => { |
|
|
|
|
|
e.currentTarget.blur(); |
|
|
if (voting) return; |
|
|
if (voting) return; |
|
|
|
|
|
|
|
|
setVoting(true); |
|
|
setVoting(true); |
|
|
if ((ownVote === CHOICE_DEFAULT || ownVote === CHOICE_DOWN) && choice === CHOICE_UP) setVoteCacheSendStackId(upvote.cacheSend(...[postId], { from: userAccount })); |
|
|
if ((ownVote === CHOICE_DEFAULT || ownVote === CHOICE_DOWN) && choice === CHOICE_UP) setVoteCacheSendStackId(upvote.cacheSend(...[postId], { from: userAccount })); |
|
|
else if ((ownVote === CHOICE_DEFAULT || ownVote === CHOICE_UP) && choice === CHOICE_DOWN) setVoteCacheSendStackId(downvote.cacheSend(...[postId], { from: userAccount })); |
|
|
else if ((ownVote === CHOICE_DEFAULT || ownVote === CHOICE_UP) && choice === CHOICE_DOWN) setVoteCacheSendStackId(downvote.cacheSend(...[postId], { from: userAccount })); |
|
|
else if ((ownVote === CHOICE_UP && choice === CHOICE_UP) || (ownVote === CHOICE_DOWN && choice === CHOICE_DOWN)) setVoteCacheSendStackId(unvote.cacheSend(...[postId], { from: userAccount })); |
|
|
else if ((ownVote === CHOICE_UP && choice === CHOICE_UP) || (ownVote === CHOICE_DOWN && choice === CHOICE_DOWN)) setVoteCacheSendStackId(unvote.cacheSend(...[postId], { from: userAccount })); |
|
|
|
|
|
else setVoting(false); |
|
|
}, [ownVote, postId, userAccount, voting]); |
|
|
}, [ownVote, postId, userAccount, voting]); |
|
|
|
|
|
|
|
|
const disableVoting = userAccount === null || !hasSignedUp || postAuthorAddress === null || userAccount === postAuthorAddress; |
|
|
const disableVoting = userAccount === null || !hasSignedUp || postAuthorAddress === null || userAccount === postAuthorAddress; |
|
@ -128,12 +92,13 @@ const PostVoting = (props) => { |
|
|
icon="arrow down" |
|
|
icon="arrow down" |
|
|
negative={ownVote === CHOICE_DOWN} |
|
|
negative={ownVote === CHOICE_DOWN} |
|
|
disabled={disableVoting} |
|
|
disabled={disableVoting} |
|
|
onClick={() => vote(CHOICE_DOWN)} |
|
|
onClick={(e) => vote(e, CHOICE_DOWN)} |
|
|
/> |
|
|
/> |
|
|
<Popup |
|
|
<Popup |
|
|
|
|
|
size="tiny" |
|
|
trigger={( |
|
|
trigger={( |
|
|
<span className="unselectable"> |
|
|
<span className="unselectable"> |
|
|
|
|
|
|
|
|
{totalVoteCount || 0} |
|
|
{totalVoteCount || 0} |
|
|
|
|
|
|
|
|
</span> |
|
|
</span> |
|
@ -162,7 +127,7 @@ const PostVoting = (props) => { |
|
|
icon="arrow up" |
|
|
icon="arrow up" |
|
|
positive={ownVote === CHOICE_UP} |
|
|
positive={ownVote === CHOICE_UP} |
|
|
disabled={disableVoting} |
|
|
disabled={disableVoting} |
|
|
onClick={() => vote(CHOICE_UP)} |
|
|
onClick={(e) => vote(e, CHOICE_UP)} |
|
|
/> |
|
|
/> |
|
|
</div> |
|
|
</div> |
|
|
), [disableVoting, downvoteCount, ownVote, totalVoteCount, upvoteCount, vote]); |
|
|
), [disableVoting, downvoteCount, ownVote, totalVoteCount, upvoteCount, vote]); |
|
|