From 313f7eaaae29b4b50202157f3552fc6ccbdc35de Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 30 Sep 2014 11:36:24 -0700 Subject: [PATCH] lock form inputs during fetch. add fetch timeout. see bitpay/bitcore#518. see #1508. --- js/controllers/send.js | 43 +++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index 84c6d7efc..d9267d08d 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -522,11 +522,36 @@ angular.module('copayApp.controllers').controller('SendController', return; } + var apply = function() { + if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') { + $rootScope.$apply(); + } + }; + notification.info('Fetching Payment', 'Retrieving Payment Request from ' + uri.merchant); + scope.loading = true; + apply(); + + var timeout = setTimeout(function() { + timeout = null; + scope.loading = false; + scope.sendForm.address.$setViewValue(''); + scope.sendForm.address.$render(); + scope.sendForm.address.$isValid = false; + notification.error('Error', 'Payment server timed out.'); + apply(); + }, 10 * 1000); + // Payment Protocol URI (BIP-72) scope.wallet.fetchPaymentTx(uri.merchant, function(err, merchantData) { + if (!timeout) return; + clearTimeout(timeout); + + scope.loading = false; + apply(); + var balance = $rootScope.availableBalance; var available = +(balance * w.settings.unitToSatoshi).toFixed(0); @@ -536,8 +561,6 @@ angular.module('copayApp.controllers').controller('SendController', } if (err) { - scope.sendForm.address.$isValid = false; - if (err.amount) { scope.sendForm.amount.$setViewValue(+err.amount / w.settings.unitToSatoshi); scope.sendForm.amount.$render(); @@ -551,18 +574,18 @@ angular.module('copayApp.controllers').controller('SendController', scope.sendForm.amount.$setViewValue(''); scope.sendForm.amount.$render(); unregister(); - if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') { - $rootScope.$apply(); - } + apply(); } }); + } else { + scope.sendForm.address.$setViewValue(''); + scope.sendForm.address.$render(); } + scope.sendForm.address.$isValid = false; notification.error('Error', err.message || 'Bad payment server.'); - if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') { - $rootScope.$apply(); - } + apply(); return; } @@ -590,9 +613,7 @@ angular.module('copayApp.controllers').controller('SendController', $scope.clearMerchant(unregister); }); - if ($rootScope.$$phase !== '$apply' && $rootScope.$$phase !== '$digest') { - $rootScope.$apply(); - } + apply(); notification.info('Payment Request', 'Server is requesting ' + merchantData.unitTotal +