From 3bfe094cc1ac4805604dfbf6892415487a3bb1bc Mon Sep 17 00:00:00 2001 From: Vit Stanislav Date: Fri, 2 Jun 2017 12:37:56 +0200 Subject: [PATCH 1/4] Check version compatibility on login - Closes #309 --- src/components/login/login.js | 5 +---- src/services/api/peers.js | 22 ++++++++++++++++++---- test/components/login/login.spec.js | 9 --------- test/services/api/peers.spec.js | 14 ++++++++++++-- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/components/login/login.js b/src/components/login/login.js index cfc3dd103..a90ddbeaf 100644 --- a/src/components/login/login.js +++ b/src/components/login/login.js @@ -7,7 +7,7 @@ app.component('login', { /* eslint no-param-reassign: ["error", { "props": false }] */ constructor($scope, $rootScope, $timeout, $document, $mdMedia, - $cookies, $location, Passphrase, $state, Account, Peers, dialog) { + $cookies, $location, Passphrase, $state, Account, Peers) { this.$scope = $scope; this.$rootScope = $rootScope; this.$timeout = $timeout; @@ -18,7 +18,6 @@ app.component('login', { this.$state = $state; this.account = Account; this.peers = Peers; - this.dialog = dialog; this.Passphrase = Passphrase; this.generatingNewPassphrase = false; @@ -69,8 +68,6 @@ app.component('login', { network: this.network, }); this.$state.go(this.$rootScope.landingUrl || 'main.transactions'); - } else { - this.dialog.errorToast(`Failed to connect to node ${this.network.address}`); } }); } diff --git a/src/services/api/peers.js b/src/services/api/peers.js index 6b57e2424..40b86e225 100644 --- a/src/services/api/peers.js +++ b/src/services/api/peers.js @@ -8,7 +8,7 @@ import lisk from 'lisk-js'; * @module app * @submodule Peers */ -app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope) => { +app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => { /** * The Peers factory constructor class * @@ -49,6 +49,7 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope) => { * @method setActive */ setActive(network) { + this.network = network; let conf = { }; if (network) { conf = network; @@ -63,7 +64,7 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope) => { } this.active = lisk.api(conf); - return this.check(); + return this.check(true); } /** @@ -93,11 +94,24 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope) => { * @private * @memberOf Peer * @method check + * @notifyUser {bool} - Should an error toast be displayed on error? */ - check() { + check(notifyUser) { return this.sendRequestPromise('loader/status', {}) .then(() => this.online = true) - .catch(() => this.online = false); + .catch((data) => { + this.online = false; + if (notifyUser) { + const address = `${this.active.currentPeer}:${this.active.port}`; + let message = `Failed to connect to node ${address}. `; + if (data && data.error && data.error.code === 'EUNAVAILABLE') { + message = `Failed to connect: Node ${address} is not active`; + } else if (!(data && data.error && data.error.code)) { + message += ' Make sure that you are using the latest version of Lisk Nano.'; + } + dialog.errorToast(message); + } + }); } } diff --git a/test/components/login/login.spec.js b/test/components/login/login.spec.js index 73eb076f3..032343a47 100644 --- a/test/components/login/login.spec.js +++ b/test/components/login/login.spec.js @@ -158,15 +158,6 @@ describe('Login controller', () => { $scope.$apply(); expect(spy).to.have.been.calledWith(); }); - - it('shows error toast if peers.setActive sets peers.online = false', () => { - const spy = sinon.spy(controller.dialog, 'errorToast'); - controller.peers.online = false; - controller.passConfirmSubmit(); - deferred.resolve(); - $scope.$apply(); - expect(spy).to.have.been.calledWith(`Failed to connect to node ${controller.network.address}`); - }); }); describe('devTestAccount()', () => { diff --git a/test/services/api/peers.spec.js b/test/services/api/peers.spec.js index 8cca380e5..109650ab0 100644 --- a/test/services/api/peers.spec.js +++ b/test/services/api/peers.spec.js @@ -9,13 +9,15 @@ describe('Factory: Peers', () => { let Peers; let $q; let $rootScope; + let dialog; beforeEach(angular.mock.module('app')); - beforeEach(inject((_Peers_, _$q_, _$rootScope_) => { + beforeEach(inject((_Peers_, _$q_, _$rootScope_, _dialog_) => { Peers = _Peers_; $q = _$q_; $rootScope = _$rootScope_; + dialog = _dialog_; })); describe('setActive(account)', () => { @@ -32,7 +34,7 @@ describe('Factory: Peers', () => { }); }); - describe('check()', () => { + describe('check(notifyUser)', () => { let deffered; let mock; @@ -61,5 +63,13 @@ describe('Factory: Peers', () => { $rootScope.$apply(); expect(Peers.online).to.equal(false); }); + + it('shows error toast if notifyUser is true', () => { + const spy = sinon.spy(dialog, 'errorToast'); + Peers.check(true); + deffered.reject(); + $rootScope.$apply(); + expect(spy).to.have.been.calledWith(); + }); }); }); From b9ec0480bde3a7b3b67aea340325234006574ffa Mon Sep 17 00:00:00 2001 From: Vit Stanislav Date: Fri, 2 Jun 2017 15:08:15 +0200 Subject: [PATCH 2/4] Change peers.check(notifyUser) to peers.check(isOnLogin) --- src/services/api/peers.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/services/api/peers.js b/src/services/api/peers.js index 40b86e225..c1725c665 100644 --- a/src/services/api/peers.js +++ b/src/services/api/peers.js @@ -94,14 +94,14 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => * @private * @memberOf Peer * @method check - * @notifyUser {bool} - Should an error toast be displayed on error? + * @isOnLogin {bool} - Should an error toast be displayed on error? */ - check(notifyUser) { + check(isOnLogin) { return this.sendRequestPromise('loader/status', {}) .then(() => this.online = true) .catch((data) => { this.online = false; - if (notifyUser) { + if (isOnLogin) { const address = `${this.active.currentPeer}:${this.active.port}`; let message = `Failed to connect to node ${address}. `; if (data && data.error && data.error.code === 'EUNAVAILABLE') { @@ -110,6 +110,7 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => message += ' Make sure that you are using the latest version of Lisk Nano.'; } dialog.errorToast(message); + this.active = undefined; } }); } From 02ac40ad84a0528521dcaa89014f239c4cdd099b Mon Sep 17 00:00:00 2001 From: Vit Stanislav Date: Fri, 2 Jun 2017 17:11:22 +0200 Subject: [PATCH 3/4] Show Online/offline toasts --- src/services/api/peers.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/services/api/peers.js b/src/services/api/peers.js index c1725c665..2a31dc1d5 100644 --- a/src/services/api/peers.js +++ b/src/services/api/peers.js @@ -64,7 +64,8 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => } this.active = lisk.api(conf); - return this.check(true); + this.wasOffline = false; + return this.check(); } /** @@ -94,14 +95,19 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => * @private * @memberOf Peer * @method check - * @isOnLogin {bool} - Should an error toast be displayed on error? */ - check(isOnLogin) { + check() { return this.sendRequestPromise('loader/status', {}) - .then(() => this.online = true) + .then(() => { + this.online = true; + if (this.wasOffline) { + dialog.successToast('Connection rerestablished'); + } + this.wasOffline = false; + }) .catch((data) => { this.online = false; - if (isOnLogin) { + if (!this.wasOffline) { const address = `${this.active.currentPeer}:${this.active.port}`; let message = `Failed to connect to node ${address}. `; if (data && data.error && data.error.code === 'EUNAVAILABLE') { @@ -110,8 +116,8 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => message += ' Make sure that you are using the latest version of Lisk Nano.'; } dialog.errorToast(message); - this.active = undefined; } + this.wasOffline = true; }); } } From e6ac1d7a29ea970dff0ab0b7ef7633262f2cc4d8 Mon Sep 17 00:00:00 2001 From: Vit Stanislav Date: Tue, 6 Jun 2017 11:04:38 +0200 Subject: [PATCH 4/4] Fix a typo: rerestablished --- src/services/api/peers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/api/peers.js b/src/services/api/peers.js index 2a31dc1d5..e42069845 100644 --- a/src/services/api/peers.js +++ b/src/services/api/peers.js @@ -101,7 +101,7 @@ app.factory('Peers', ($timeout, $cookies, $location, $q, $rootScope, dialog) => .then(() => { this.online = true; if (this.wasOffline) { - dialog.successToast('Connection rerestablished'); + dialog.successToast('Connection re-established'); } this.wasOffline = false; })