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 3 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
19 changes: 19 additions & 0 deletions lib/laws-phase-two/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "laws-phase-two",
"description": "Collection of laws",
"dependencies": {
"component/emitter": "1.1.3",
"visionmedia/debug": "0.7.4"
},
"locals": [
"citizen",
"request",
"render",
"stateful"
],
"scripts": [
"laws.js",
"proto.js"
],
"main": "laws.js"
}
7 changes: 7 additions & 0 deletions lib/laws-phase-two/laws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Module dependencies.
*/

var Laws = require('./proto');

var laws = module.exports = new Laws();
129 changes: 129 additions & 0 deletions lib/laws-phase-two/proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Module dependencies.
*/

var citizen = require('citizen');
var Emitter = require('emitter');
var request = require('request');
var render = require('render');
var Stateful = require('stateful');
var log = require('debug')('democracyos:laws-proto');

/**
* Expose `Laws` proto constructor
*/

module.exports = Laws;

/**
* Laws collection constructor
*/

function Laws() {
if (!(this instanceof Laws)) {
return new Laws();
};

// instance bindings
this.middleware = this.middleware.bind(this);
this.fetch = this.fetch.bind(this);

this.state('initializing');

// Re-fetch laws on citizen sign-in
citizen.on('loaded', this.fetch);

this.fetch();
}

/**
* Mixin Laws prototype with Emitter
*/

// Emitter(Laws.prototype);
Stateful(Laws);

/**
* Fetch `laws` from source
*
* @param {String} src
* @api public
*/

Laws.prototype.fetch = function(src) {
log('request in process');
src = src || '/api/law/phaseTwo';

this.state('loading');

request
.get(src)
.end(onresponse.bind(this));

function onresponse(err, res) {
if (err || !res.ok) {
var message = 'Unable to load laws. Please try reloading the page. Thanks!';
return this.error(message);
};

this.set(res.body);
}
}

/**
* Set items to `v`
*
* @param {Array} v
* @return {Laws} Instance of `Laws`
* @api public
*/

Laws.prototype.set = function(v) {
this.items = v;
this.state('loaded');
return this;
}

/**
* Get current `items`
*
* @return {Array} Current `items`
* @api public
*/

Laws.prototype.get = function() {
return this.items;
}

/**
* Middleware for `page.js` like
* routers
*
* @param {Object} ctx
* @param {Function} next
* @api public
*/

Laws.prototype.middleware = function(ctx, next) {
this.ready(next);
}

/**
* Handle errors
*
* @param {String} error
* @return {Laws} Instance of `Laws`
* @api public
*/

Laws.prototype.error = function(message) {
// TODO: We should use `Error`s instead of
// `Strings` to handle errors...
// Ref: http://www.devthought.com/2011/12/22/a-string-is-not-an-error/
this.state('error', message);
log('error found: %s', message);

// Unregister all `ready` listeners
this.off('ready');
return this;
}
17 changes: 11 additions & 6 deletions lib/models/law.js
Original file line number Diff line number Diff line change
@@ -301,11 +301,16 @@ 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) return cb(err);
Copy link
Member

Choose a reason for hiding this comment

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

you're calling cb without knowing if it's defined


this.votes.push(vote);
// Add citizen as participant
this.addParticipant(citizen);
this.save(cb);
Copy link
Member

Choose a reason for hiding this comment

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

You're missing the if (cb) here

}
};

/**
@@ -346,7 +351,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",