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.
This commit is contained in:
2018-04-06 16:45:55 +02:00
parent b9d78efaff
commit bbea07b2a7
2 changed files with 51 additions and 11 deletions
+38 -9
View File
@@ -6,7 +6,7 @@ import RSVP from 'rsvp';
import Ember from 'ember'; import Ember from 'ember';
import Service from 'ember-service'; import Service from 'ember-service';
import injectService from 'ember-service/inject'; 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 config from 'kredits-web/config/environment';
import Proposal from 'kredits-web/models/proposal'; import Proposal from 'kredits-web/models/proposal';
@@ -30,6 +30,17 @@ export default Service.extend({
ethProvider: null, ethProvider: null,
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded. 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 // 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 // and we can rely on the ethProvider and the potential currentUserAccounts to be available
@@ -44,8 +55,15 @@ export default Service.extend({
ethProvider.listAccounts().then((accounts) => { ethProvider.listAccounts().then((accounts) => {
this.set('currentUserAccounts', accounts); this.set('currentUserAccounts', accounts);
this.set('ethProvider', ethProvider); this.set('ethProvider', ethProvider);
if (accounts.length > 0) {
this.get('getCurrentUser').then((contributorData) => {
this.set('currentUser', contributorData);
resolve(ethProvider); resolve(ethProvider);
}); });
} else {
resolve(ethProvider);
}
});
} else { } else {
debug('[kredits] Creating new instance from npm module class'); debug('[kredits] Creating new instance from npm module class');
let providerUrl = localStorage.getItem('config:web3ProviderUrl') || config.web3ProviderUrl; let providerUrl = localStorage.getItem('config:web3ProviderUrl') || config.web3ProviderUrl;
@@ -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() { registryContract: computed('ethProvider', function() {
let networkId = this.get('ethProvider').chainId; let networkId = this.get('ethProvider').chainId;
let registry = new ethers.Contract(addresses['Registry'][networkId], abis['Registry'], this.get('ethProvider')); 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());
}
})
});
})
}); });
+12 -1
View File
@@ -1,8 +1,19 @@
{{#if kredits.hasAccounts }}
<section id="user-account">
{{#if kredits.currentUser }}
{{ kredits.currentUser.name }}
{{#if kredits.currentUserIsCore }}
(core)
{{/if}}
Account: {{kredits.currentUser.address}}
{{/if}}
</section>
{{/if}}
<section id="contributors"> <section id="contributors">
<header> <header>
<h2>Kosmos Contributors</h2> <h2>Kosmos Contributors</h2>
</header> </header>
<div class="content"> <div class="content">
{{contributor-list contributors=contributorsSorted}} {{contributor-list contributors=contributorsSorted}}