Browse Source

fix: PostVoting total votes refresh

develop
Ezerous 4 years ago
parent
commit
4c9cddffa5
  1. 54
      packages/concordia-app/src/components/PostList/PostVoting/index.jsx
  2. 6
      packages/concordia-contracts/contracts/PostVoting.sol

54
packages/concordia-app/src/components/PostList/PostVoting/index.jsx

@ -18,6 +18,7 @@ const {
[POST_VOTING_CONTRACT]: { [POST_VOTING_CONTRACT]: {
methods: { methods: {
getVote: { cacheCall: getVoteChainData }, getVote: { cacheCall: getVoteChainData },
getTotalVoteCount: { cacheCall: getTotalVoteCountChainData },
getUpvoteCount: { cacheCall: getUpvoteCountChainData }, getUpvoteCount: { cacheCall: getUpvoteCountChainData },
getDownvoteCount: { cacheCall: getDownvoteCountChainData }, getDownvoteCount: { cacheCall: getDownvoteCountChainData },
upvote, downvote, unvote, upvote, downvote, unvote,
@ -34,15 +35,18 @@ const PostVoting = (props) => {
const userAccount = useSelector((state) => state.accounts[0]); const userAccount = useSelector((state) => state.accounts[0]);
// Current votes // Current votes
const getVoteResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getVote); const [getVoteCallHash, setGetVoteCallHash] = useState(null);
const getUpvoteCountResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getUpvoteCount); const [getTotalVoteCountCallHash, setGetTotalVoteCountCallHash] = useState(null);
const getDownvoteCountResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getDownvoteCount); const [getUpvoteCountCallHash, setGetUpvoteCountCallHash] = useState(null);
const [getDownvoteCountCallHash, setGetDownvoteCountCallHash] = useState(null);
const [getVoteCallHash, setGetVoteCallHash] = useState([]); const getVoteResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getVote[getVoteCallHash]);
const [getUpvoteCountCallHash, setGetUpvoteCountCallHash] = useState([]); const getTotalVoteCountResult = useSelector((state) => state.contracts[POST_VOTING_CONTRACT].getTotalVoteCount[getTotalVoteCountCallHash]);
const [getDownvoteCountCallHash, setGetDownvoteCountCallHash] = useState([]); 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 [upvoteCount, setUpvoteCount] = useState(null); const [upvoteCount, setUpvoteCount] = useState(null);
const [downvoteCount, setDownvoteCount] = useState(null); const [downvoteCount, setDownvoteCount] = useState(null);
@ -55,38 +59,45 @@ const PostVoting = (props) => {
// Current votes // Current votes
useEffect(() => { useEffect(() => {
if (drizzleInitialized && !drizzleInitializationFailed && postId !== null) { if (drizzleInitialized && !drizzleInitializationFailed && postId !== null) {
if (upvoteCount === null) setGetUpvoteCountCallHash(getUpvoteCountChainData(postId)); if (getTotalVoteCountCallHash === null) setGetTotalVoteCountCallHash(getTotalVoteCountChainData(postId));
if (getUpvoteCountCallHash === null) setGetUpvoteCountCallHash(getUpvoteCountChainData(postId));
if (downvoteCount === null) setGetDownvoteCountCallHash(getDownvoteCountChainData(postId)); if (getDownvoteCountCallHash === null) setGetDownvoteCountCallHash(getDownvoteCountChainData(postId));
} }
}, [downvoteCount, drizzleInitializationFailed, drizzleInitialized, postId, upvoteCount, userAccount]); }, [drizzleInitializationFailed, drizzleInitialized, getDownvoteCountCallHash,
getTotalVoteCountCallHash, getUpvoteCountCallHash, postId]);
useEffect(() => { useEffect(() => {
const shouldGetOwnVoteFromChain = ownVote === null; const shouldGetOwnVoteFromChain = ownVote === null;
if (drizzleInitialized && !drizzleInitializationFailed && shouldGetOwnVoteFromChain if (drizzleInitialized && !drizzleInitializationFailed && shouldGetOwnVoteFromChain
&& postId !== null && userAccount !== null) { && postId !== null && userAccount !== null && getVoteCallHash === null) {
setGetVoteCallHash(getVoteChainData(postId, userAccount)); setGetVoteCallHash(getVoteChainData(postId, userAccount));
} }
}, [drizzleInitializationFailed, drizzleInitialized, ownVote, postId, userAccount]); }, [drizzleInitializationFailed, drizzleInitialized, getVoteCallHash, ownVote, postId, userAccount]);
useEffect(() => {
if (getVoteResult) {
setOwnVote(getVoteResult.value);
}
}, [getVoteResult]);
useEffect(() => { useEffect(() => {
if (getVoteCallHash && getVoteResult && getVoteResult[getVoteCallHash]) { if (getTotalVoteCountResult) {
setOwnVote(getVoteResult[getVoteCallHash].value); setTotalVoteCount(getTotalVoteCountResult.value);
} }
}, [getVoteCallHash, getVoteResult]); }, [getTotalVoteCountResult]);
useEffect(() => { useEffect(() => {
if (getUpvoteCountCallHash && getUpvoteCountResult && getUpvoteCountResult[getUpvoteCountCallHash]) { if (getUpvoteCountResult) {
setUpvoteCount(getUpvoteCountResult[getUpvoteCountCallHash].value); setUpvoteCount(getUpvoteCountResult.value);
} }
}, [getUpvoteCountCallHash, getUpvoteCountResult]); }, [getUpvoteCountResult]);
useEffect(() => { useEffect(() => {
if (getDownvoteCountCallHash && getDownvoteCountResult && getDownvoteCountResult[getDownvoteCountCallHash]) { if (getDownvoteCountResult) {
setDownvoteCount(getDownvoteCountResult[getDownvoteCountCallHash].value); setDownvoteCount(getDownvoteCountResult.value);
} }
}, [getDownvoteCountCallHash, getDownvoteCountResult]); }, [getDownvoteCountResult]);
// Voting // Voting
useEffect(() => { useEffect(() => {
@ -109,7 +120,6 @@ const PostVoting = (props) => {
}, [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;
const totalVoteCount = (upvoteCount !== null && downvoteCount !== null) ? upvoteCount - downvoteCount : null;
return useMemo(() => ( return useMemo(() => (
<div className="post-voting"> <div className="post-voting">
<Button <Button

6
packages/concordia-contracts/contracts/PostVoting.sol

@ -40,6 +40,12 @@ contract PostVoting {
return (getVoteCount(postID, Option.DOWN)); return (getVoteCount(postID, Option.DOWN));
} }
function getTotalVoteCount(uint postID) public view returns (int) {
int upvoteCount = int(getUpvoteCount(postID));
int downvoteCount = int(getDownvoteCount(postID));
return upvoteCount - downvoteCount;
}
// Gets voters for a specific option (Option.UP/ Option.DOWN) // Gets voters for a specific option (Option.UP/ Option.DOWN)
function getVoters(uint postID, Option option) private view returns (address[] memory) { function getVoters(uint postID, Option option) private view returns (address[] memory) {
require(forum.postExists(postID), forum.POST_DOES_NOT_EXIST()); require(forum.postExists(postID), forum.POST_DOES_NOT_EXIST());

Loading…
Cancel
Save