Browse Source

performance: post voting improvements

develop
Ezerous 4 years ago
parent
commit
ca89a879da
  1. 71
      packages/concordia-app/src/components/PostList/PostVoting/index.jsx
  2. 2
      packages/concordia-app/src/layouts/MainLayout/index.jsx
  3. 10
      packages/concordia-contracts/contracts/PostVoting.sol

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

@ -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]);
useEffect(() => {
if (getUpvoteCountResult) {
setUpvoteCount(getUpvoteCountResult.value);
}
}, [getUpvoteCountResult]);
useEffect(() => {
if (getDownvoteCountResult) {
setDownvoteCount(getDownvoteCountResult.value);
} }
}, [getDownvoteCountResult]); }, [getVoteInfoResult]);
// 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,9 +92,10 @@ 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">
&nbsp;&nbsp; &nbsp;&nbsp;
@ -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]);

2
packages/concordia-app/src/layouts/MainLayout/index.jsx

@ -22,9 +22,9 @@ const MainLayout = (props) => {
{children} {children}
</Grid.Column> </Grid.Column>
<Grid.Column width={4}> <Grid.Column width={4}>
<MainLayoutConcordiaStatus />
<MainLayoutEthereumStatus /> <MainLayoutEthereumStatus />
<MainLayoutIPFSStatus /> <MainLayoutIPFSStatus />
<MainLayoutConcordiaStatus />
</Grid.Column> </Grid.Column>
</Grid> </Grid>

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

@ -46,6 +46,16 @@ contract PostVoting {
return upvoteCount - downvoteCount; return upvoteCount - downvoteCount;
} }
function getVoteInfo(uint postID) public view returns (Option, int, uint, uint) {
require(forum.postExists(postID), forum.POST_DOES_NOT_EXIST());
return (
getVote(postID, msg.sender),
getTotalVoteCount(postID),
getUpvoteCount(postID),
getDownvoteCount(postID)
);
}
// 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