-
Notifications
You must be signed in to change notification settings - Fork 616
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
+66
−13
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04e8950
[models:law] Fixed misused async behavior when voting. Closes #394.
jfresco 9bcd4d4
[models:law] Removed callback undefined check. Related to #394.
jfresco 1ee8933
[models:law] Voting made async and UI blocks when user clicks on voti…
jfresco f8c9a2a
[models:law] Fix callback handling on voting. Related to #394.
jfresco 509e7bc
Removed laws-phase-two directory.
jfresco afcf1bf
[proposal-options] Updated i18n keys. Related to #394.
jfresco bb499d7
Merge branch 'development' into 394-votes-count
gvilarino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
this.votes.push(vote); | ||
// Add citizen as participant | ||
this.addParticipant(citizen); | ||
this.save(cb); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're missing the |
||
} | ||
}; | ||
|
||
/** | ||
|
@@ -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); | ||
}; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
.meta-data(class= !!citizen.id ? '' : 'hide') | ||
- if(~positives.indexOf(citizen.id)) | ||
include vote-positive | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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