Browse Source

fix: poll replication

develop
Ezerous 4 years ago
parent
commit
f02dcbe4ee
  1. 33
      packages/concordia-app/src/components/PollView/index.jsx
  2. 5
      packages/concordia-app/src/views/Topic/TopicCreate/index.jsx
  3. 20
      packages/concordia-app/src/views/Topic/TopicView/index.jsx

33
packages/concordia-app/src/components/PollView/index.jsx

@ -18,11 +18,20 @@ import { POLL_OPTIONS, POLL_QUESTION } from '../../constants/orbit/PollsDatabase
import PollDataInvalid from './PollDataInvalid'; import PollDataInvalid from './PollDataInvalid';
import PollGuestView from './PollGuestView'; import PollGuestView from './PollGuestView';
const { contracts: { [VOTING_CONTRACT]: { methods: { getPoll: { cacheCall: getPollChainData } } } } } = drizzle; const {
contracts: {
[VOTING_CONTRACT]: {
methods: {
getPoll:
{ cacheCall: getPollChainData, clearCacheCall: clearGetPollChainData },
},
},
},
} = drizzle;
const { orbit } = breeze; const { orbit } = breeze;
const PollView = (props) => { const PollView = (props) => {
const { topicId } = props; const { topicId, topicAuthorAddress } = props;
const userAddress = useSelector((state) => state.user.address); const userAddress = useSelector((state) => state.user.address);
const hasSignedUp = useSelector((state) => state.user.hasSignedUp); const hasSignedUp = useSelector((state) => state.user.hasSignedUp);
const getPollResults = useSelector((state) => state.contracts[VOTING_CONTRACT].getPoll); const getPollResults = useSelector((state) => state.contracts[VOTING_CONTRACT].getPoll);
@ -51,24 +60,25 @@ const PollView = (props) => {
type: FETCH_USER_DATABASE, type: FETCH_USER_DATABASE,
orbit, orbit,
dbName: POLLS_DATABASE, dbName: POLLS_DATABASE,
userAddress, userAddress: topicAuthorAddress,
}); });
}, [dispatch, userAddress]); }, [dispatch, topicAuthorAddress]);
useEffect(() => { useEffect(() => {
if (getPollCallHash && getPollResults && getPollResults[getPollCallHash]) { if (getPollCallHash && getPollResults && getPollResults[getPollCallHash]) {
setPollHash(getPollResults[getPollCallHash].value[1]); const pollResults = getPollResults[getPollCallHash];
setPollChangeVoteEnabled(getPollResults[getPollCallHash].value[2]); setPollHash(pollResults.value[1]);
setVoteCounts(getPollResults[getPollCallHash].value[4].map((voteCount) => parseInt(voteCount, 10))); setPollChangeVoteEnabled(pollResults.value[2]);
setVoteCounts(pollResults.value[4].map((voteCount) => parseInt(voteCount, 10)));
const cumulativeSum = getPollResults[getPollCallHash].value[4] const cumulativeSum = pollResults.value[4]
.map((voteCount) => parseInt(voteCount, 10)) .map((voteCount) => parseInt(voteCount, 10))
.reduce((accumulator, voteCount) => (accumulator.length === 0 .reduce((accumulator, voteCount) => (accumulator.length === 0
? [voteCount] ? [voteCount]
: [...accumulator, accumulator[accumulator.length - 1] + voteCount]), []); : [...accumulator, accumulator[accumulator.length - 1] + voteCount]), []);
setVoters(cumulativeSum setVoters(cumulativeSum
.map((subArrayEnd, index) => getPollResults[getPollCallHash].value[5] .map((subArrayEnd, index) => pollResults.value[5]
.slice(index > 0 ? cumulativeSum[index - 1] : 0, .slice(index > 0 ? cumulativeSum[index - 1] : 0,
subArrayEnd))); subArrayEnd)));
@ -160,6 +170,10 @@ const PollView = (props) => {
]); ]);
}, [chainDataLoading, orbitDataLoading, pollGraphTab, pollVoteTab, t]); }, [chainDataLoading, orbitDataLoading, pollGraphTab, pollVoteTab, t]);
useEffect(() => () => {
clearGetPollChainData();
}, []);
return ( return (
<Container id="topic-poll-container" textAlign="left"> <Container id="topic-poll-container" textAlign="left">
{pollHashValid {pollHashValid
@ -183,6 +197,7 @@ const PollView = (props) => {
PollView.propTypes = { PollView.propTypes = {
topicId: PropTypes.number.isRequired, topicId: PropTypes.number.isRequired,
topicAuthorAddress: PropTypes.string,
}; };
export default PollView; export default PollView;

5
packages/concordia-app/src/views/Topic/TopicCreate/index.jsx

@ -54,10 +54,7 @@ const TopicCreate = (props) => {
const goToTopic = useCallback((topicId) => { const goToTopic = useCallback((topicId) => {
if (topicId) history.push(`/topics/${topicId}`); if (topicId) history.push(`/topics/${topicId}`);
else { else history.push(`/topics/${newTopicId}`);
console.error('Error creating poll!');
history.push(`/topics/${newTopicId}`);
}
}, [history, newTopicId]); }, [history, newTopicId]);
useEffect(() => { useEffect(() => {

20
packages/concordia-app/src/views/Topic/TopicView/index.jsx

@ -22,7 +22,13 @@ import targetBlank from '../../../utils/markdownUtils';
const { const {
contracts: { contracts: {
[FORUM_CONTRACT]: { methods: { getTopic: { cacheCall: getTopicChainData } } }, [FORUM_CONTRACT]: { methods: { getTopic: { cacheCall: getTopicChainData } } },
[VOTING_CONTRACT]: { methods: { pollExists: { cacheCall: pollExistsChainData } } }, [VOTING_CONTRACT]: {
methods: {
pollExists: {
cacheCall: pollExistsChainData, clearCacheCall: clearPollExistsChainData,
},
},
},
}, },
} = drizzle; } = drizzle;
const { orbit } = breeze; const { orbit } = breeze;
@ -132,12 +138,22 @@ const TopicView = (props) => {
} }
}, [topicId, topics]); }, [topicId, topics]);
const poll = useMemo(() => hasPoll && <PollView topicId={topicId} />, [hasPoll, topicId]); const poll = useMemo(() => hasPoll
&& (
<PollView
topicId={topicId}
topicAuthorAddress={topicAuthorAddress}
/>
), [hasPoll, topicAuthorAddress, topicId]);
const stopClickPropagation = (event) => { const stopClickPropagation = (event) => {
event.stopPropagation(); event.stopPropagation();
}; };
useEffect(() => () => {
clearPollExistsChainData();
}, []);
return ( return (
<Container id="topic-container"> <Container id="topic-container">
<Segment> <Segment>

Loading…
Cancel
Save