Skip to content

Commit

Permalink
Merge pull request #159 from 67P/feature/pending_changes
Browse files Browse the repository at this point in the history
Gracefully handle pending changes for contributions
  • Loading branch information
raucao authored Oct 18, 2019
2 parents 12b127c + 243a41d commit 1dc1a97
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 12 deletions.
5 changes: 4 additions & 1 deletion app/components/contribution-list/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
{{#unless contribution.vetoed}}
{{#unless (is-confirmed-contribution contribution)}}
<p class="voting">
<button {{action "veto" contribution.id}} class="small danger">veto</button>
{{input type="button" class="button small danger"
click=(action "veto" contribution.id)
disabled=contribution.hasPendingChanges
value="veto"}}
</p>
{{/unless}}
{{/unless}}
Expand Down
2 changes: 1 addition & 1 deletion app/components/topbar-account-panel/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default Component.extend({

userHasEthereumWallet: computed(function() {
return isPresent(window.ethereum);
}).volatile(),
}),

showConnectButton: computed('userHasEthereumWallet',
'kredits.hasAccounts', function() {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/dashboard/contributions/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export default Controller.extend({

ipfsGatewayUrl: computed(function() {
return config.ipfs.gatewayUrl;
}).volatile()
})

});
2 changes: 1 addition & 1 deletion app/controllers/dashboard/contributors/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export default Controller.extend({

ipfsGatewayUrl: computed(function() {
return config.ipfs.gatewayUrl;
}).volatile()
})

});
18 changes: 15 additions & 3 deletions app/helpers/contribution-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ export default Helper.extend({
compute([contribution]) {
this.setupRecompute(contribution);

let status = [];

if (contribution.vetoed) {
return 'vetoed';
status.push('vetoed');
} else if (contribution.confirmedAt > this.currentBlock) {
return 'unconfirmed';
status.push('unconfirmed');
} else {
return 'confirmed'
status.push('confirmed');
}

if (contribution.hasPendingChanges) {
status.push('pending');
}

status.push('pending');

return status.join(' ');
},

destroy () {
Expand All @@ -31,11 +41,13 @@ export default Helper.extend({
contribution.addObserver('vetoed' , this, this.triggerRecompute);
contribution.addObserver('confirmedAt' , this, this.triggerRecompute);
contribution.addObserver('currentBlock' , this, this.triggerRecompute);
contribution.addObserver('hasPendingChanges' , this, this.triggerRecompute);

this.teardown = () => {
contribution.removeObserver('vetoed', this, this.triggerRecompute);
contribution.removeObserver('confirmedAt', this, this.triggerRecompute);
contribution.removeObserver('currentBlock', this, this.triggerRecompute);
contribution.removeObserver('hadPendingChanges', this, this.triggerRecompute);
};
},

Expand Down
8 changes: 7 additions & 1 deletion app/models/contribution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EmberObject, { computed } from '@ember/object';
import { isEmpty } from '@ember/utils';
import { isEmpty, isPresent } from '@ember/utils';
import bignumber from 'kredits-web/utils/cps/bignumber';
import moment from 'moment';

Expand All @@ -24,6 +24,8 @@ export default EmberObject.extend({
time: null,
ipfsData: '',

pendingTx: null,

init () {
this._super(...arguments);
if (isEmpty(this.details)) this.set('details', {});
Expand All @@ -35,6 +37,10 @@ export default EmberObject.extend({

jsDate: computed('iso8601Date', function() {
return moment(this.iso8601Date).toDate();
}),

hasPendingChanges: computed('pendingTx', function() {
return isPresent(this.pendingTx);
})

});
8 changes: 6 additions & 2 deletions app/services/kredits.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ export default Service.extend({

getContributors () {
return this.kredits.Contributor.all()
.then((contributors) => {
return contributors.map((contributor) => {
.then(contributors => {
return contributors.map(contributor => {
return Contributor.create(contributor);
});
});
Expand All @@ -225,6 +225,7 @@ export default Service.extend({
console.debug('[kredits] add contribution response', data);
attributes.contributor = this.contributors.findBy('id', attributes.contributorId);
const contribution = Contribution.create(attributes);
contribution.set('pendingTx', data);
contribution.set('confirmedAtBlock', data.blockNumber + 40320);
this.contributions.pushObject(contribution);
return contribution;
Expand Down Expand Up @@ -278,10 +279,12 @@ export default Service.extend({

veto (contributionId) {
console.debug('[kredits] veto against', contributionId);
const contribution = this.contributions.findBy('id', contributionId);

return this.kredits.Contribution.functions.veto(contributionId, { gasLimit: 300000 })
.then(data => {
console.debug('[kredits] veto response', data);
contribution.set('pendingTx', data);
return data;
});
},
Expand Down Expand Up @@ -367,6 +370,7 @@ export default Service.extend({

if (contribution) {
contribution.set('vetoed', true);
contribution.set('pendingTx', null);
}
},

Expand Down
4 changes: 2 additions & 2 deletions app/styles/_buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ button, input[type=submit], .button {
padding: 0.2rem 0.8rem;
}

&.danger {
&.danger:not(:disabled) {
color: $red;
background-color: rgba(40, 21, 21, 0.6);
border-color: rgba(40, 21, 21, 1);
Expand All @@ -45,7 +45,7 @@ button, input[type=submit], .button {
}
}

&.green {
&.green:not(:disabled) {
color: $green;
background-color: rgba(21, 40, 21, 0.6);
border-color: rgba(21, 40, 21, 1);
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/models/contribution-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ module('Unit | Model | contribution', function(hooks) {
assert.ok(model.jsDate instanceof Date);
assert.equal(model.jsDate.toISOString(), '2019-09-10T09:33:00.141Z');
});

test('hasPendingChanges', function(assert) {
const model = Contribution.create({});
assert.equal(model.hasPendingChanges, false);

model.set('pendingTx', { hash: 'abcdef123456' });
assert.equal(model.hasPendingChanges, true);
});

});

0 comments on commit 1dc1a97

Please sign in to comment.