From 9ac1e565ac249f1bbc789d6c96cffafb9fb4cfba Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Fri, 13 Jan 2017 01:05:52 -0300 Subject: [PATCH] Clean code. Fix coinbase tx modal --- src/js/controllers/coinbase.js | 51 ++++++++++--------- .../controllers/modals/coinbaseTxDetails.js | 24 ++++++--- src/js/services/coinbaseService.js | 28 ++++++---- www/views/coinbase.html | 20 ++++---- www/views/modals/coinbase-tx-details.html | 16 ++---- 5 files changed, 77 insertions(+), 62 deletions(-) diff --git a/src/js/controllers/coinbase.js b/src/js/controllers/coinbase.js index 5f3c9684c..3492245c6 100644 --- a/src/js/controllers/coinbase.js +++ b/src/js/controllers/coinbase.js @@ -8,31 +8,34 @@ angular.module('copayApp.controllers').controller('coinbaseController', function var init = function() { var config = configService.getSync().wallet.settings; $scope.currency = getCurrency(config.alternativeIsoCode); - ongoingProcess.set('connectingCoinbase', true); - coinbaseService.init(function(err, data) { - ongoingProcess.set('connectingCoinbase', false); - if (err || lodash.isEmpty(data)) { - if (err) { - popupService.showAlert(gettextCatalog.getString('Error'), err); + coinbaseService.getStoredToken(function(at) { + $scope.accessToken = at; + + // Update Access Token if necessary + coinbaseService.init(function(err, data) { + if (err || lodash.isEmpty(data)) { + if (err) { + popupService.showAlert(gettextCatalog.getString('Error'), err); + } + return; } - return; - } - // Show rates - coinbaseService.buyPrice(data.accessToken, $scope.currency, function(err, b) { - $scope.buyPrice = b.data || null; - }); - coinbaseService.sellPrice(data.accessToken, $scope.currency, function(err, s) { - $scope.sellPrice = s.data || null; - }); + // Show rates + coinbaseService.buyPrice(data.accessToken, $scope.currency, function(err, b) { + $scope.buyPrice = b.data || null; + }); + coinbaseService.sellPrice(data.accessToken, $scope.currency, function(err, s) { + $scope.sellPrice = s.data || null; + }); - // Updating accessToken and accountId - $timeout(function() { - $scope.accessToken = data.accessToken; - $scope.accountId = data.accountId; - $scope.updateTransactions(); - $scope.$apply(); - }, 100); + // Updating accessToken and accountId + $timeout(function() { + $scope.accessToken = data.accessToken; + $scope.accountId = data.accountId; + $scope.updateTransactions(); + $scope.$apply(); + }, 100); + }); }); }; @@ -104,8 +107,8 @@ angular.module('copayApp.controllers').controller('coinbaseController', function scope: $scope, animation: 'slide-in-up' }).then(function(modal) { - $scope.coinbaseTxDetailsModal = modal; - $scope.coinbaseTxDetailsModal.show(); + $scope.modal = modal; + $scope.modal.show(); }); }; diff --git a/src/js/controllers/modals/coinbaseTxDetails.js b/src/js/controllers/modals/coinbaseTxDetails.js index 3d952b168..bb9cd3020 100644 --- a/src/js/controllers/modals/coinbaseTxDetails.js +++ b/src/js/controllers/modals/coinbaseTxDetails.js @@ -1,18 +1,28 @@ 'use strict'; -angular.module('copayApp.controllers').controller('coinbaseTxDetailsController', function($scope, $rootScope, coinbaseService) { +angular.module('copayApp.controllers').controller('coinbaseTxDetailsController', function($scope, coinbaseService, popupService) { $scope.remove = function() { - coinbaseService.savePendingTransaction($scope.tx, { - remove: true - }, function(err) { - $rootScope.$emit('Local/CoinbaseTx'); - $scope.close(); + coinbaseService.setCredentials(); + $scope.updateRequired = false; + var message = 'Are you sure you want to remove this transaction?'; + popupService.showConfirm(null, message, null, null, function(ok) { + if (!ok) { + return; + } + coinbaseService.savePendingTransaction($scope.tx, { + remove: true + }, function(err) { + $scope.updateRequired = true; + $scope.close(); + }); }); }; $scope.close = function() { - $scope.coinbaseTxDetailsModal.hide(); + $scope.modal.hide().then(function() { + if ($scope.updateRequired) $scope.updateTransactions(); + }); }; }); diff --git a/src/js/services/coinbaseService.js b/src/js/services/coinbaseService.js index 6eaa27b11..7d04d100a 100644 --- a/src/js/services/coinbaseService.js +++ b/src/js/services/coinbaseService.js @@ -97,6 +97,13 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ return credentials.NETWORK; }; + root.getStoredToken = function(cb) { + storageService.getCoinbaseToken(credentials.NETWORK, function(err, accessToken) { + if (err || !accessToken) return cb(); + return cb(accessToken); + }); + }; + root.getOauthCodeUrl = function() { return credentials.HOST + '/oauth/authorize?response_type=code&client_id=' @@ -478,16 +485,17 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ }; root.getPendingTransactions = function(coinbasePendingTransactions) { - root.init(function(err, data) { - if (err || lodash.isEmpty(data)) { - if (err) $log.error(err); - return; - } - var accessToken = data.accessToken; - var accountId = data.accountId; - storageService.getCoinbaseTxs(credentials.NETWORK, function(err, txs) { - txs = txs ? JSON.parse(txs) : {}; - coinbasePendingTransactions.data = lodash.isEmpty(txs) ? null : txs; + storageService.getCoinbaseTxs(credentials.NETWORK, function(err, txs) { + txs = txs ? JSON.parse(txs) : {}; + coinbasePendingTransactions.data = lodash.isEmpty(txs) ? null : txs; + + root.init(function(err, data) { + if (err || lodash.isEmpty(data)) { + if (err) $log.error(err); + return; + } + var accessToken = data.accessToken; + var accountId = data.accountId; lodash.forEach(coinbasePendingTransactions.data, function(dataFromStorage, txId) { if ((dataFromStorage.type == 'sell' && dataFromStorage.status == 'completed') || diff --git a/www/views/coinbase.html b/www/views/coinbase.html index baaa4f54a..48cc8a7c0 100644 --- a/www/views/coinbase.html +++ b/www/views/coinbase.html @@ -13,7 +13,7 @@ -
+
@@ -53,20 +53,22 @@
-
+
-