|
@ -1,9 +1,11 @@ |
|
|
import React, { useState } from 'react'; |
|
|
import React, { useEffect, useState } from 'react'; |
|
|
import PropTypes from 'prop-types'; |
|
|
import PropTypes from 'prop-types'; |
|
|
import { Form } from 'semantic-ui-react'; |
|
|
import { useSelector } from 'react-redux'; |
|
|
|
|
|
import { Button, Form, Header } from 'semantic-ui-react'; |
|
|
import { useTranslation } from 'react-i18next'; |
|
|
import { useTranslation } from 'react-i18next'; |
|
|
import { VOTING_CONTRACT } from 'concordia-shared/src/constants/contracts/ContractNames'; |
|
|
import { VOTING_CONTRACT } from 'concordia-shared/src/constants/contracts/ContractNames'; |
|
|
import { drizzle } from '../../../redux/store'; |
|
|
import { drizzle } from '../../../redux/store'; |
|
|
|
|
|
import { TRANSACTION_ERROR, TRANSACTION_SUCCESS } from '../../../constants/TransactionStatus'; |
|
|
|
|
|
|
|
|
const { contracts: { [VOTING_CONTRACT]: { methods: { vote } } } } = drizzle; |
|
|
const { contracts: { [VOTING_CONTRACT]: { methods: { vote } } } } = drizzle; |
|
|
|
|
|
|
|
@ -11,19 +13,57 @@ const PollVote = (props) => { |
|
|
const { |
|
|
const { |
|
|
topicId, account, pollOptions, enableVoteChanges, hasUserVoted, userVoteIndex, |
|
|
topicId, account, pollOptions, enableVoteChanges, hasUserVoted, userVoteIndex, |
|
|
} = props; |
|
|
} = props; |
|
|
|
|
|
const transactionStack = useSelector((state) => state.transactionStack); |
|
|
|
|
|
const transactions = useSelector((state) => state.transactions); |
|
|
|
|
|
const [voteCacheSendStackId, setVoteCacheSendStackId] = useState(''); |
|
|
const [selectedOptionIndex, setSelectedOptionIndex] = useState(userVoteIndex); |
|
|
const [selectedOptionIndex, setSelectedOptionIndex] = useState(userVoteIndex); |
|
|
const [voting, setVoting] = useState(''); |
|
|
const [voting, setVoting] = useState(false); |
|
|
const { t } = useTranslation(); |
|
|
const { t } = useTranslation(); |
|
|
|
|
|
|
|
|
const onOptionSelected = (e, { value }) => { |
|
|
const onOptionSelected = (e, { value }) => { |
|
|
setSelectedOptionIndex(value); |
|
|
setSelectedOptionIndex(value); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
setSelectedOptionIndex(userVoteIndex); |
|
|
|
|
|
}, [userVoteIndex]); |
|
|
|
|
|
|
|
|
const onCastVote = () => { |
|
|
const onCastVote = () => { |
|
|
setVoting(true); |
|
|
setVoting(true); |
|
|
vote.cacheSend(...[topicId, selectedOptionIndex + 1], { from: account }); |
|
|
setVoteCacheSendStackId(vote.cacheSend(...[topicId, selectedOptionIndex + 1], { from: account })); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const onUnvote = (e) => { |
|
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
setVoting(true); |
|
|
|
|
|
setVoteCacheSendStackId(vote.cacheSend(...[topicId, 0], { from: account })); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (voting && transactionStack && transactionStack[voteCacheSendStackId] |
|
|
|
|
|
&& transactions[transactionStack[voteCacheSendStackId]]) { |
|
|
|
|
|
if (transactions[transactionStack[voteCacheSendStackId]].status === TRANSACTION_ERROR |
|
|
|
|
|
|| transactions[transactionStack[voteCacheSendStackId]].status === TRANSACTION_SUCCESS) { |
|
|
|
|
|
setVoting(false); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}, [transactionStack, transactions, voteCacheSendStackId, voting]); |
|
|
|
|
|
|
|
|
|
|
|
if (hasUserVoted && !enableVoteChanges) { |
|
|
|
|
|
return ( |
|
|
|
|
|
<> |
|
|
|
|
|
<Header as="h4"> |
|
|
|
|
|
{t('topic.poll.tab.results.user.vote', { |
|
|
|
|
|
userVote: pollOptions[userVoteIndex], |
|
|
|
|
|
})} |
|
|
|
|
|
</Header> |
|
|
|
|
|
<div> |
|
|
|
|
|
{t('topic.poll.tab.vote.no.changes')} |
|
|
|
|
|
</div> |
|
|
|
|
|
</> |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<Form onSubmit={onCastVote}> |
|
|
<Form onSubmit={onCastVote}> |
|
|
<Form.Group grouped> |
|
|
<Form.Group grouped> |
|
@ -34,17 +74,28 @@ const PollVote = (props) => { |
|
|
label={pollOption} |
|
|
label={pollOption} |
|
|
value={index} |
|
|
value={index} |
|
|
checked={index === selectedOptionIndex} |
|
|
checked={index === selectedOptionIndex} |
|
|
disabled={hasUserVoted && !enableVoteChanges && index !== selectedOptionIndex} |
|
|
disabled={voting} |
|
|
onChange={onOptionSelected} |
|
|
onChange={onOptionSelected} |
|
|
/> |
|
|
/> |
|
|
))} |
|
|
))} |
|
|
</Form.Group> |
|
|
</Form.Group> |
|
|
<Form.Button |
|
|
<Button |
|
|
type="submit" |
|
|
type="submit" |
|
|
disabled={voting || (hasUserVoted && !enableVoteChanges) || (selectedOptionIndex === userVoteIndex)} |
|
|
className="primary-button" |
|
|
|
|
|
disabled={voting || (selectedOptionIndex === userVoteIndex)} |
|
|
> |
|
|
> |
|
|
{t('topic.poll.tab.vote.form.button.submit')} |
|
|
{t('topic.poll.tab.vote.form.button.submit')} |
|
|
</Form.Button> |
|
|
</Button> |
|
|
|
|
|
{hasUserVoted && enableVoteChanges |
|
|
|
|
|
&& ( |
|
|
|
|
|
<Button |
|
|
|
|
|
type="submit" |
|
|
|
|
|
disabled={voting} |
|
|
|
|
|
onClick={onUnvote} |
|
|
|
|
|
> |
|
|
|
|
|
{t('topic.poll.tab.vote.form.button.unvote')} |
|
|
|
|
|
</Button> |
|
|
|
|
|
)} |
|
|
</Form> |
|
|
</Form> |
|
|
); |
|
|
); |
|
|
}; |
|
|
}; |
|
|