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

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

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

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

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

Loading…
Cancel
Save