Vote, finalize buttons validations

This commit is contained in:
viktor 2017-12-25 18:15:43 +03:00
parent 5b2efe8ac4
commit 57577c1ccd
4 changed files with 112 additions and 29 deletions

View File

@ -101,7 +101,6 @@ export class BallotKeysCard extends React.Component {
const now = moment();
const finish = moment.utc(this.endTime, 'DD/MM/YYYY h:mm:ss A');
let ms = finish.diff(now);
console.log("ms:", ms)
if (ms <= 0)
return this.timeToFinish = moment(0, "h").format("HH") + ":" + moment(0, "m").format("mm") + ":" + moment(0, "s").format("ss");
@ -179,7 +178,19 @@ export class BallotKeysCard extends React.Component {
this.isFinalized = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.getIsFinalized(_id).call();
}
vote = async (e, _id, _type) => {
isValidaVote = async () => {
const { contractsStore } = this.props;
let isValidVote = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.isValidVote(this.props.id, contractsStore.votingKey).call();
return isValidVote;
}
isActive = async () => {
const { contractsStore } = this.props;
let isActive = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.isActive(this.props.id).call();
return isActive;
}
vote = async (e, _type) => {
const { commonStore, contractsStore } = this.props;
const { push } = this.props.routing;
if (!contractsStore.isValidVotingKey) {
@ -187,6 +198,12 @@ export class BallotKeysCard extends React.Component {
return;
}
commonStore.showLoading();
let isValidVote = await this.isValidaVote();
if (!isValidVote) {
commonStore.hideLoading();
swal("Warning!", constants.INVALID_VOTE_MSG, "warning");
return;
}
contractsStore.votingToChangeKeys.vote(this.props.id, _type, contractsStore.votingKey)
.on("receipt", () => {
commonStore.hideLoading();
@ -200,14 +217,24 @@ export class BallotKeysCard extends React.Component {
});
}
finalize = async (e, _id) => {
finalize = async (e) => {
const { commonStore, contractsStore } = this.props;
const { push } = this.props.routing;
if (!contractsStore.isValidVotingKey) {
swal("Warning!", constants.INVALID_VOTING_KEY_MSG, "warning");
return;
}
if (this.isFinalized) {
swal("Warning!", constants.ALREADY_FINALIZED_MSG, "warning");
return;
}
commonStore.showLoading();
let isActive = await this.isActive();
if (isActive) {
commonStore.hideLoading();
swal("Warning!", constants.INVALID_FINALIZE_MSG, "warning");
return;
}
contractsStore.votingToChangeKeys.finalize(this.props.id, contractsStore.votingKey)
.on("receipt", () => {
commonStore.hideLoading();
@ -310,7 +337,7 @@ export class BallotKeysCard extends React.Component {
<div className="vote-scale--fill vote-scale--fill_no" style={{width: `${this.votesAgainstPercents}%`}}></div>
</div>
</div>
<button type="button" onClick={(e) => this.vote(e, this.props.id, 2)} className="ballots-i--vote ballots-i--vote_no">Vote</button>
<button type="button" onClick={(e) => this.vote(e, 2)} className="ballots-i--vote ballots-i--vote_no">Vote</button>
</div>
</div>
<div className="info">
@ -318,7 +345,7 @@ export class BallotKeysCard extends React.Component {
</div>
<hr />
<div className="ballots-footer">
<button type="button" onClick={(e) => this.finalize(e, this.props.id)} className="ballots-footer-finalize">Finalize ballot</button>
<button type="button" onClick={(e) => this.finalize(e)} className="ballots-footer-finalize">Finalize ballot</button>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore</p>
</div>
</div>

View File

@ -42,7 +42,6 @@ export class BallotMinThresholdCard extends React.Component {
const now = moment();
const finish = moment.utc(this.endTime, 'DD/MM/YYYY h:mm:ss A');
let ms = finish.diff(now);
console.log("ms:", ms)
if (ms <= 0)
return this.timeToFinish = moment(0, "h").format("HH") + ":" + moment(0, "m").format("mm") + ":" + moment(0, "s").format("ss");
@ -68,7 +67,7 @@ export class BallotMinThresholdCard extends React.Component {
@action("Get creator")
getCreator = async (_id) => {
const { contractsStore } = this.props;
let votingState = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.votingState(_id).call()
let votingState = await contractsStore.votingToChangeMinThreshold.votingToChangeMinThresholdInstance.methods.votingState(_id).call()
this.getValidatorFullname(votingState.creator);
}
@ -85,23 +84,35 @@ export class BallotMinThresholdCard extends React.Component {
@action("Get total voters")
getTotalVoters = async (_id) => {
const { contractsStore } = this.props;
this.totalVoters = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.getTotalVoters(_id).call();
this.totalVoters = await contractsStore.votingToChangeMinThreshold.votingToChangeMinThresholdInstance.methods.getTotalVoters(_id).call();
console.log(this.totalVoters);
}
@action("Get progress")
getProgress = async (_id) => {
const { contractsStore } = this.props;
this.progress = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.getProgress(_id).call();
this.progress = await contractsStore.votingToChangeMinThreshold.votingToChangeMinThresholdInstance.methods.getProgress(_id).call();
}
@action("Get isFinalized")
getIsFinalized = async(_id) => {
const { contractsStore } = this.props;
this.isFinalized = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.getIsFinalized(_id).call();
this.isFinalized = await contractsStore.votingToChangeMinThreshold.votingToChangeMinThresholdInstance.methods.getIsFinalized(_id).call();
}
vote = async (e, _id, _type) => {
isValidaVote = async () => {
const { contractsStore } = this.props;
let isValidVote = await contractsStore.votingToChangeMinThreshold.votingToChangeMinThresholdInstance.methods.isValidVote(this.props.id, contractsStore.votingKey).call();
return isValidVote;
}
isActive = async () => {
const { contractsStore } = this.props;
let isActive = await contractsStore.votingToChangeMinThreshold.votingToChangeMinThresholdInstance.methods.isActive(this.props.id).call();
return isActive;
}
vote = async (e, _type) => {
const { commonStore, contractsStore } = this.props;
const { push } = this.props.routing;
if (!contractsStore.isValidVotingKey) {
@ -109,7 +120,13 @@ export class BallotMinThresholdCard extends React.Component {
return;
}
commonStore.showLoading();
contractsStore.votingToChangeKeys.vote(this.props.id, _type, contractsStore.votingKey)
let isValidVote = await this.isValidaVote();
if (!isValidVote) {
commonStore.hideLoading();
swal("Warning!", constants.INVALID_VOTE_MSG, "warning");
return;
}
contractsStore.votingToChangeMinThreshold.vote(this.props.id, _type, contractsStore.votingKey)
.on("receipt", () => {
commonStore.hideLoading();
swal("Congratulations!", constants.VOTED_SUCCESS_MSG, "success").then((result) => {
@ -122,15 +139,25 @@ export class BallotMinThresholdCard extends React.Component {
});
}
finalize = async (e, _id) => {
finalize = async (e) => {
const { commonStore, contractsStore } = this.props;
const { push } = this.props.routing;
if (!contractsStore.isValidVotingKey) {
swal("Warning!", constants.INVALID_VOTING_KEY_MSG, "warning");
return;
}
if (this.isFinalized) {
swal("Warning!", constants.ALREADY_FINALIZED_MSG, "warning");
return;
}
commonStore.showLoading();
contractsStore.votingToChangeKeys.finalize(this.props.id, contractsStore.votingKey)
let isActive = await this.isActive();
if (isActive) {
commonStore.hideLoading();
swal("Warning!", constants.INVALID_FINALIZE_MSG, "warning");
return;
}
contractsStore.votingToChangeMinThreshold.finalize(this.props.id, contractsStore.votingKey)
.on("receipt", () => {
commonStore.hideLoading();
swal("Congratulations!", constants.FINALIZED_SUCCESS_MSG, "success").then((result) => {
@ -212,7 +239,7 @@ export class BallotMinThresholdCard extends React.Component {
<div className="vote-scale--fill vote-scale--fill_no" style={{width: `${this.votesAgainstPercents}%`}}></div>
</div>
</div>
<button type="button" onClick={(e) => this.vote(e, this.props.id, 2)} className="ballots-i--vote ballots-i--vote_no">Vote</button>
<button type="button" onClick={(e) => this.vote(e, 2)} className="ballots-i--vote ballots-i--vote_no">Vote</button>
</div>
</div>
<div className="info">
@ -220,7 +247,7 @@ export class BallotMinThresholdCard extends React.Component {
</div>
<hr />
<div className="ballots-footer">
<button type="button" onClick={(e) => this.finalize(e, this.props.id)} className="ballots-footer-finalize">Finalize ballot</button>
<button type="button" onClick={(e) => this.finalize(e)} className="ballots-footer-finalize">Finalize ballot</button>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore</p>
</div>
</div>

View File

@ -43,7 +43,6 @@ export class BallotProxyCard extends React.Component {
const now = moment();
const finish = moment.utc(this.endTime, 'DD/MM/YYYY h:mm:ss A');
let ms = finish.diff(now);
console.log("ms:", ms)
if (ms <= 0)
return this.timeToFinish = moment(0, "h").format("HH") + ":" + moment(0, "m").format("mm") + ":" + moment(0, "s").format("ss");
@ -77,7 +76,7 @@ export class BallotProxyCard extends React.Component {
@action("Get creator")
getCreator = async (_id) => {
const { contractsStore } = this.props;
let votingState = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.votingState(_id).call()
let votingState = await contractsStore.votingToChangeProxy.votingToChangeProxyInstance.methods.votingState(_id).call()
this.getValidatorFullname(votingState.creator);
}
@ -94,23 +93,35 @@ export class BallotProxyCard extends React.Component {
@action("Get total voters")
getTotalVoters = async (_id) => {
const { contractsStore } = this.props;
this.totalVoters = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.getTotalVoters(_id).call();
this.totalVoters = await contractsStore.votingToChangeProxy.votingToChangeProxyInstance.methods.getTotalVoters(_id).call();
console.log(this.totalVoters);
}
@action("Get progress")
getProgress = async (_id) => {
const { contractsStore } = this.props;
this.progress = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.getProgress(_id).call();
this.progress = await contractsStore.votingToChangeProxy.votingToChangeProxyInstance.methods.getProgress(_id).call();
}
@action("Get isFinalized")
getIsFinalized = async (_id) => {
const { contractsStore } = this.props;
this.isFinalized = await contractsStore.votingToChangeKeys.votingToChangeKeysInstance.methods.getIsFinalized(_id).call();
this.isFinalized = await contractsStore.votingToChangeProxy.votingToChangeProxyInstance.methods.getIsFinalized(_id).call();
}
vote = async (e, _id, _type) => {
isValidaVote = async () => {
const { contractsStore } = this.props;
let isValidVote = await contractsStore.votingToChangeProxy.votingToChangeProxyInstance.methods.isValidVote(this.props.id, contractsStore.votingKey).call();
return isValidVote;
}
isActive = async () => {
const { contractsStore } = this.props;
let isActive = await contractsStore.votingToChangeProxy.votingToChangeProxyInstance.methods.isActive(this.props.id).call();
return isActive;
}
vote = async (e, _type) => {
const { commonStore, contractsStore } = this.props;
const { push } = this.props.routing;
if (!contractsStore.isValidVotingKey) {
@ -118,7 +129,13 @@ export class BallotProxyCard extends React.Component {
return;
}
commonStore.showLoading();
contractsStore.votingToChangeKeys.vote(this.props.id, _type, contractsStore.votingKey)
let isValidVote = await this.isValidaVote();
if (!isValidVote) {
commonStore.hideLoading();
swal("Warning!", constants.INVALID_VOTE_MSG, "warning");
return;
}
contractsStore.votingToChangeProxy.vote(this.props.id, _type, contractsStore.votingKey)
.on("receipt", () => {
commonStore.hideLoading();
swal("Congratulations!", constants.VOTED_SUCCESS_MSG, "success").then((result) => {
@ -131,15 +148,25 @@ export class BallotProxyCard extends React.Component {
});
}
finalize = async (e, _id) => {
finalize = async (e) => {
const { commonStore, contractsStore } = this.props;
const { push } = this.props.routing;
if (!contractsStore.isValidVotingKey) {
swal("Warning!", constants.INVALID_VOTING_KEY_MSG, "warning");
return;
}
if (this.isFinalized) {
swal("Warning!", constants.ALREADY_FINALIZED_MSG, "warning");
return;
}
commonStore.showLoading();
contractsStore.votingToChangeKeys.finalize(this.props.id, contractsStore.votingKey)
let isActive = await this.isActive();
if (isActive) {
commonStore.hideLoading();
swal("Warning!", constants.INVALID_FINALIZE_MSG, "warning");
return;
}
contractsStore.votingToChangeProxy.finalize(this.props.id, contractsStore.votingKey)
.on("receipt", () => {
commonStore.hideLoading();
swal("Congratulations!", constants.FINALIZED_SUCCESS_MSG, "success").then((result) => {
@ -170,7 +197,6 @@ export class BallotProxyCard extends React.Component {
render () {
const { commonStore, contractsStore, ballotStore } = this.props;
let ballotClass = (commonStore.filtered && this.isFinalized) ? "ballots-i display-none" : "ballots-i";
let additionalProps = this.isFinalized ? { disabled: true } : {};
return (
<div className="ballots-i">
<div className="ballots-about">
@ -211,7 +237,7 @@ export class BallotProxyCard extends React.Component {
</div>
<div className="ballots-i-scale">
<div className="ballots-i-scale-column">
<button type="button" onClick={(e) => this.vote(e, this.props.id, 1)} className="ballots-i--vote ballots-i--vote_yes">Vote</button>
<button type="button" onClick={(e) => this.vote(e, 1)} className="ballots-i--vote ballots-i--vote_yes">Vote</button>
<div className="vote-scale--container">
<p className="vote-scale--value">Yes</p>
<p className="vote-scale--votes">Votes: {this.votesForNumber}</p>
@ -230,7 +256,7 @@ export class BallotProxyCard extends React.Component {
<div className="vote-scale--fill vote-scale--fill_no" style={{width: `${this.votesAgainstPercents}%`}}></div>
</div>
</div>
<button type="button" onClick={(e) => this.vote(e, this.props.id, 2)} className="ballots-i--vote ballots-i--vote_no">Vote</button>
<button type="button" onClick={(e) => this.vote(e, 2)} className="ballots-i--vote ballots-i--vote_no">Vote</button>
</div>
</div>
<div className="info">
@ -238,7 +264,7 @@ export class BallotProxyCard extends React.Component {
</div>
<hr />
<div className="ballots-footer">
<button type="button" onClick={(e) => this.finalize(e, this.props.id)} className="ballots-footer-finalize">Finalize ballot</button>
<button type="button" onClick={(e) => this.finalize(e)} className="ballots-footer-finalize">Finalize ballot</button>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore</p>
</div>
</div>

View File

@ -2,6 +2,9 @@ let constants = {};
constants.INVALID_VOTING_KEY_MSG = "The key is not valid voting Key! Please make sure you have loaded correct voting key in metamask";
constants.VOTED_SUCCESS_MSG = "You successfully voted";
constants.FINALIZED_SUCCESS_MSG = "You successfully finalized";
constants.ALREADY_FINALIZED_MSG = "This ballot is already finalized";
constants.INVALID_VOTE_MSG = "You can't vote on this ballot";
constants.INVALID_FINALIZE_MSG = "You can't finalize this ballot";
module.exports = {
constants
}