diff --git a/js/src/views/ParityBar/accountStore.js b/js/src/views/ParityBar/accountStore.js index b53f40dd2..f1d704d50 100644 --- a/js/src/views/ParityBar/accountStore.js +++ b/js/src/views/ParityBar/accountStore.js @@ -24,7 +24,9 @@ export default class AccountStore { constructor (api) { this._api = api; - this.loadAccounts(); + this.loadDefaultAccount() + .then(() => this.loadAccounts()); + this.subscribeDefaultAccount(); } @@ -33,7 +35,15 @@ export default class AccountStore { } @action setDefaultAccount = (defaultAccount) => { - this.defaultAccount = defaultAccount; + transaction(() => { + this.accounts = this.accounts.map((account) => { + account.default = account.address === defaultAccount; + + return account; + }); + + this.defaultAccount = defaultAccount; + }); } @action setLoading = (isLoading) => { @@ -47,6 +57,9 @@ export default class AccountStore { .map((account) => account.address) ); + // Have optimistic UI: https://www.smashingmagazine.com/2016/11/true-lies-of-optimistic-user-interfaces/?utm_source=codropscollective + this.setDefaultAccount(address); + return this._api.parity .setNewDappsWhitelist(accounts) .catch((error) => { @@ -54,6 +67,12 @@ export default class AccountStore { }); } + loadDefaultAccount () { + return this._api.parity + .defaultAccount() + .then((address) => this.setDefaultAccount(address)); + } + loadAccounts () { this.setLoading(true); diff --git a/js/src/views/ParityBar/accountStore.spec.js b/js/src/views/ParityBar/accountStore.spec.js index 6dd219806..c13c62aa9 100644 --- a/js/src/views/ParityBar/accountStore.spec.js +++ b/js/src/views/ParityBar/accountStore.spec.js @@ -89,6 +89,16 @@ describe('views/ParityBar/AccountStore', () => { }); }); + describe('loadDefaultAccount', () => { + beforeEach(() => { + return store.loadDefaultAccount(); + }); + + it('load and set the default account', () => { + expect(store.defaultAccount).to.equal(ACCOUNT_DEFAULT); + }); + }); + describe('makeDefaultAccount', () => { beforeEach(() => { return store.makeDefaultAccount(ACCOUNT_NEW); diff --git a/js/src/views/ParityBar/parityBar.js b/js/src/views/ParityBar/parityBar.js index ac251d0a3..fe737a873 100644 --- a/js/src/views/ParityBar/parityBar.js +++ b/js/src/views/ParityBar/parityBar.js @@ -343,15 +343,22 @@ class ParityBar extends Component { } renderAccount = (account) => { - const onMakeDefault = () => { + const makeDefaultAccount = () => { + return this.accountStore + .makeDefaultAccount(account.address) + .then(() => this.accountStore.loadAccounts()); + }; + + const onDoubleClick = () => { this.toggleAccountsDisplay(); - this.accountStore.makeDefaultAccount(account.address); + makeDefaultAccount(); }; return (
{