From bbea07b2a792b6de966248e82435a31ae7a330bd Mon Sep 17 00:00:00 2001 From: bumi Date: Fri, 6 Apr 2018 16:45:55 +0200 Subject: [PATCH 1/4] Add account badge This gives easy access to test if the "user/visitor" has an account, is a contributor and if she is core. This is used to display some account information. --- app/services/kredits.js | 49 ++++++++++++++++++++++++++++++++--------- app/templates/index.hbs | 13 ++++++++++- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/app/services/kredits.js b/app/services/kredits.js index ba1e404..71b4701 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -6,7 +6,7 @@ import RSVP from 'rsvp'; import Ember from 'ember'; import Service from 'ember-service'; import injectService from 'ember-service/inject'; -import computed from 'ember-computed'; +import computed, { alias } from 'ember-computed'; import config from 'kredits-web/config/environment'; import Proposal from 'kredits-web/models/proposal'; @@ -30,6 +30,17 @@ export default Service.extend({ ethProvider: null, currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded. + currentUser: null, + currentUserIsContributor: computed('currentUser', function() { + return Ember.isPresent(this.get('currentUser')); + }), + currentUserIsCore: alias('currentUser.isCore'), + hasAccounts: computed('currentUserAccounts', function() { + return !Ember.isEmpty(this.get('currentUserAccounts')); + }), + accountNeedsUnlock: computed('currentUserAccounts', function() { + return this.get('currentUserAccounts') && Ember.isEmpty(this.get('currentUserAccounts')); + }), // this is called called in the routes beforeModel(). So it is initialized before everything else // and we can rely on the ethProvider and the potential currentUserAccounts to be available @@ -44,7 +55,14 @@ export default Service.extend({ ethProvider.listAccounts().then((accounts) => { this.set('currentUserAccounts', accounts); this.set('ethProvider', ethProvider); - resolve(ethProvider); + if (accounts.length > 0) { + this.get('getCurrentUser').then((contributorData) => { + this.set('currentUser', contributorData); + resolve(ethProvider); + }); + } else { + resolve(ethProvider); + } }); } else { debug('[kredits] Creating new instance from npm module class'); @@ -58,14 +76,6 @@ export default Service.extend({ }); }, - accountNeedsUnlock: computed('currentUserAccounts', function() { - return this.get('currentUserAccounts') && Ember.isEmpty(this.get('currentUserAccounts')); - }), - - hasAccounts: computed('currentUserAccounts', function() { - return !Ember.isEmpty(this.get('currentUserAccounts')); - }), - registryContract: computed('ethProvider', function() { let networkId = this.get('ethProvider').chainId; let registry = new ethers.Contract(addresses['Registry'][networkId], abis['Registry'], this.get('ethProvider')); @@ -281,4 +291,23 @@ export default Service.extend({ }); }); }, + + getCurrentUser: computed('ethProvider', function() { + if (Ember.isEmpty(this.get('currentUserAccounts'))) { + return new Promise((resolve) => resolve(null)); + } + return this.get('contributorsContract') + .then((contract) => { + console.log('user accounts', this.get('currentUserAccounts')[0]); + return contract.getContributorIdByAddress(this.get('currentUserAccounts')[0]) + .then((id) => { + // check if the user is a contributor or not + if( id.toNumber() === 0) { + return Ember.RSVP.resolve() + } else { + return this.getContributorData(id.toNumber()); + } + }) + }); + }) }); diff --git a/app/templates/index.hbs b/app/templates/index.hbs index 23f9a8a..37617ce 100644 --- a/app/templates/index.hbs +++ b/app/templates/index.hbs @@ -1,8 +1,19 @@ +{{#if kredits.hasAccounts }} +
+ {{#if kredits.currentUser }} + {{ kredits.currentUser.name }} + {{#if kredits.currentUserIsCore }} + (core) + {{/if}} + Account: {{kredits.currentUser.address}} + {{/if}} +
+{{/if}} +

Kosmos Contributors

-
{{contributor-list contributors=contributorsSorted}} From 603224e9061918c33c9df1ac2c109c0a4e3e8ea2 Mon Sep 17 00:00:00 2001 From: bumi Date: Fri, 6 Apr 2018 16:53:01 +0200 Subject: [PATCH 2/4] Use ember style empty promise --- app/services/kredits.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/kredits.js b/app/services/kredits.js index 71b4701..163049f 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -294,7 +294,7 @@ export default Service.extend({ getCurrentUser: computed('ethProvider', function() { if (Ember.isEmpty(this.get('currentUserAccounts'))) { - return new Promise((resolve) => resolve(null)); + return Ember.RSVP.resolve(); } return this.get('contributorsContract') .then((contract) => { From f992fb10bdbcd52c7f8bccce4a33955d63755786 Mon Sep 17 00:00:00 2001 From: bumi Date: Fri, 6 Apr 2018 17:00:25 +0200 Subject: [PATCH 3/4] Fix JS style --- app/services/kredits.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/kredits.js b/app/services/kredits.js index 163049f..8f2f527 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -303,11 +303,11 @@ export default Service.extend({ .then((id) => { // check if the user is a contributor or not if( id.toNumber() === 0) { - return Ember.RSVP.resolve() + return Ember.RSVP.resolve(); } else { return this.getContributorData(id.toNumber()); } - }) + }); }); }) }); From 499b22af151c0c6a8225ac668569584189086861 Mon Sep 17 00:00:00 2001 From: bumi Date: Fri, 6 Apr 2018 17:40:45 +0200 Subject: [PATCH 4/4] Fix JS/Ember style --- app/services/kredits.js | 16 ++++++++-------- app/templates/index.hbs | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/services/kredits.js b/app/services/kredits.js index 8f2f527..3af0ecd 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -7,6 +7,7 @@ import Ember from 'ember'; import Service from 'ember-service'; import injectService from 'ember-service/inject'; import computed, { alias } from 'ember-computed'; +import { isEmpty, isPresent } from 'ember-utils'; import config from 'kredits-web/config/environment'; import Proposal from 'kredits-web/models/proposal'; @@ -32,14 +33,14 @@ export default Service.extend({ currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded. currentUser: null, currentUserIsContributor: computed('currentUser', function() { - return Ember.isPresent(this.get('currentUser')); + return isPresent(this.get('currentUser')); }), currentUserIsCore: alias('currentUser.isCore'), hasAccounts: computed('currentUserAccounts', function() { - return !Ember.isEmpty(this.get('currentUserAccounts')); + return !isEmpty(this.get('currentUserAccounts')); }), accountNeedsUnlock: computed('currentUserAccounts', function() { - return this.get('currentUserAccounts') && Ember.isEmpty(this.get('currentUserAccounts')); + return this.get('currentUserAccounts') && isEmpty(this.get('currentUserAccounts')); }), // this is called called in the routes beforeModel(). So it is initialized before everything else @@ -293,17 +294,16 @@ export default Service.extend({ }, getCurrentUser: computed('ethProvider', function() { - if (Ember.isEmpty(this.get('currentUserAccounts'))) { - return Ember.RSVP.resolve(); + if (isEmpty(this.get('currentUserAccounts'))) { + return RSVP.resolve(); } return this.get('contributorsContract') .then((contract) => { - console.log('user accounts', this.get('currentUserAccounts')[0]); - return contract.getContributorIdByAddress(this.get('currentUserAccounts')[0]) + return contract.getContributorIdByAddress(this.get('currentUserAccounts.firstObject')) .then((id) => { // check if the user is a contributor or not if( id.toNumber() === 0) { - return Ember.RSVP.resolve(); + return RSVP.resolve(); } else { return this.getContributorData(id.toNumber()); } diff --git a/app/templates/index.hbs b/app/templates/index.hbs index 37617ce..49817d1 100644 --- a/app/templates/index.hbs +++ b/app/templates/index.hbs @@ -1,13 +1,13 @@ {{#if kredits.hasAccounts }} -
- {{#if kredits.currentUser }} - {{ kredits.currentUser.name }} - {{#if kredits.currentUserIsCore }} - (core) +
+ {{#if kredits.currentUser }} + {{ kredits.currentUser.name }} + {{#if kredits.currentUserIsCore }} + (core) + {{/if}} + Account: {{kredits.currentUser.address}} {{/if}} - Account: {{kredits.currentUser.address}} - {{/if}} -
+
{{/if}}