Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[models:law] Fixed misused async behavior when voting. Closes #394. #489

Merged
merged 7 commits into from
Dec 11, 2014
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib/models/law.js
Original file line number Diff line number Diff line change
@@ -301,11 +301,19 @@ LawSchema.methods.vote = function(citizen, value, cb) {
if (this.closingAt && (+new Date(this.closingAt) < +new Date) ) return cb(new Error('Can\'t vote after closing date.'));

var vote = { author: citizen, value: value, caster: citizen };
this.unvote(citizen);
this.votes.addToSet(vote);
// Add citizen as participant
this.addParticipant(citizen);
this.save(cb);
this.unvote(citizen, onunvote.bind(this));

function onunvote(err) {
if (err) {
if ('function' === typeof cb) return cb(err);
else throw err;
}

this.votes.push(vote);
// Add citizen as participant
this.addParticipant(citizen);
if ('function' === typeof cb) this.save(cb);
}
};

/**
@@ -346,7 +354,7 @@ LawSchema.methods.unvote = function(citizen, cb) {
log('Remove vote %j', removed);
});

if (votes.length) this.save(cb);
if ('function' === typeof cb) this.save(cb);
};

/**
1 change: 1 addition & 0 deletions lib/proposal-options/options.jade
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

.proposal-options
.vote-box
div#voting-error.alert.alert-warning.hide #{t('Connection error when voting')}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfresco please use i18n keys in the canonical format. In this case I guess proposal-options.error.voting would do

.meta-data(class= !!citizen.id ? '' : 'hide')
- if(~positives.indexOf(citizen.id))
include vote-positive
41 changes: 34 additions & 7 deletions lib/proposal-options/proposal-options.js
Original file line number Diff line number Diff line change
@@ -49,11 +49,14 @@ function ProposalOptions (proposal, citizen) {
this.citizen = citizen;
this.el = domify(options({ proposal: proposal, citizen: citizen, t: t }));
this.events = events(this.el, this);

this.events.bind('click .vote-box .direct-vote .vote-option', 'vote');
//UNDONE: this.events.bind('click .vote-box .proxy-vote .vote-option', 'proxy');
this.events.bind('click .vote-box .meta-data .change-vote', 'changevote');

this.on('voting', this.onvoting.bind(this));
this.on('vote', this.onvote.bind(this));
this.on('voteerror', this.onvoteerror.bind(this));

this.buttonsBox = o('.vote-box .vote-options', this.el);
this.delegationBox = o('.vote-box .delegation-box', this.el);
@@ -106,29 +109,35 @@ ProposalOptions.prototype.vote = function(ev) {

if (this.citizen.id) {
log('casting vote %s for %s', value, id);
this.emit('voting', value);
this.post(
'/api/law/:id/vote'.replace(':id', id),
{ value: value },
function (err, res) {
if (err || !res.ok) return log('Failed cast %s for %s with error: %j', value, id, err || res.error);
if (err || !res.ok) {
self.emit('voteerror', value);
return log('Failed cast %s for %s with error: %j', value, id, err || res.error);
}

bus.emit('vote', id, value);
self.emit('vote', value);
}
);
);
} else {
classes(o('.proposal-options p.text-mute', this.el)).remove('hide');
}
}

ProposalOptions.prototype.onvote = function(value) {
ProposalOptions.prototype.onvoting = function(value) {
classes(o("#voting-error", this.el)).add('hide');
classes(o('.vote-options', this.el)).add('hide');
var meta = o('.meta-data', this.el);
var item = o('a.meta-item', this.el);
classes(item).remove('hide');
classes(item).add('hide');
var el;

this.unvote();

switch (value) {
case 'negative':
this.proposal.downvotes.push(this.citizen.id);
@@ -143,11 +152,29 @@ ProposalOptions.prototype.onvote = function(value) {
el = render.dom(neutral);
break;
}

var alert = o('.alert', meta);
if (alert) {
meta.removeChild(alert);
}
meta.insertBefore(el, meta.firstChild);
}

ProposalOptions.prototype.onvoteerror = function(value) {
classes(o('.change-vote', this.el)).add('hide');
classes(o('.vote-options', this.el)).remove('hide');
classes(o("#voting-error", this.el)).remove('hide');

var meta = o('.meta-data', this.el);
var alert = o('.alert', meta);
if (alert) {
meta.removeChild(alert);
}
}

ProposalOptions.prototype.onvote = function(value) {
classes(o('.change-vote', this.el)).remove('hide');

var cast = o('.votes-cast em', this.el);
var upvotes = this.proposal.upvotes || [];
var downvotes = this.proposal.downvotes || [];
2 changes: 2 additions & 0 deletions lib/proposal-options/proposal-options.styl
Original file line number Diff line number Diff line change
@@ -57,6 +57,8 @@ border_radius = 3px
.delegation-box
display inline-block
.vote-box
#voting-error
color: black
.meta-data
margin-bottom 20px
.change-vote
1 change: 1 addition & 0 deletions lib/translations/lib/en.json
Original file line number Diff line number Diff line change
@@ -183,6 +183,7 @@
"Summary": "Summary",
"Chart": "Chart",
"URL": "URL",
"Connection error when voting": "An error occurred while processing your vote. Please try again",

"Clause": "Clause",
"read more": "Read more",
1 change: 1 addition & 0 deletions lib/translations/lib/es.json
Original file line number Diff line number Diff line change
@@ -184,6 +184,7 @@
"Results will be shown when this law is closed": "Los resultados se mostrarán cuando se cierre la ley",
"Summary": "Resumen",
"Chart": "Gráfico",
"Connection error when voting": "Ocurrió un error al procesar tu voto. Por favor, vuelve a intentar",

"Clause": "Artículo",
"read more": "Leer más",