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:
+39
-10
@@ -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());
|
||||
}
|
||||
})
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
+12
-1
@@ -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">
|
||||
<header>
|
||||
<h2>Kosmos Contributors</h2>
|
||||
</header>
|
||||
|
||||
<div class="content">
|
||||
{{contributor-list contributors=contributorsSorted}}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user