refactor confirm view

This commit is contained in:
Gabriel Bazán 2016-12-02 14:48:39 -03:00
commit 090b05984b
7 changed files with 164 additions and 104 deletions

View file

@ -340,6 +340,7 @@ angular.module('copayApp.controllers').controller('amountController', function($
});
});
} else if ($scope.isGlidera) {
var amount = $scope.showAlternativeAmount ? fromFiat(_amount) : _amount;
$state.transitionTo('tabs.buyandsell.glidera.confirm', {
toAmount: (amount * unitToSatoshi).toFixed(0),
glideraBuy: $scope.glideraBuy,

View file

@ -9,80 +9,80 @@ angular.module('copayApp.controllers').controller('buyGlideraController',
this.success = null;
$scope.network = glideraService.getEnvironment();
// $scope.$on('Wallet/Changed', function(event, w) {
// if (lodash.isEmpty(w)) {
// $log.debug('No wallet provided');
// return;
// }
// wallet = w;
// $log.debug('Wallet changed: ' + w.name);
// });
//
// $scope.update = function(opts) {
// if (!$scope.token || !$scope.permissions) return;
// $log.debug('Updating Glidera Account...');
// var accessToken = $scope.token;
// var permissions = $scope.permissions;
//
// opts = opts || {};
//
// glideraService.getStatus(accessToken, function(err, data) {
// $scope.status = data;
// });
//
// glideraService.getLimits(accessToken, function(err, limits) {
// $scope.limits = limits;
// });
//
// if (permissions.transaction_history) {
// glideraService.getTransactions(accessToken, function(err, data) {
// $scope.txs = data;
// });
// }
//
// if (permissions.view_email_address && opts.fullUpdate) {
// glideraService.getEmail(accessToken, function(err, data) {
// $scope.email = data.email;
// });
// }
// if (permissions.personal_info && opts.fullUpdate) {
// glideraService.getPersonalInfo(accessToken, function(err, data) {
// $scope.personalInfo = data;
// });
// }
// };
//
// this.getBuyPrice = function(token, price) {
// var self = this;
// if (!price || (price && !price.qty && !price.fiat)) {
// this.buyPrice = null;
// return;
// }
// this.gettingBuyPrice = true;
// glideraService.buyPrice(token, price, function(err, buyPrice) {
// self.gettingBuyPrice = false;
// if (err) {
// popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again'));
// return;
// }
// self.buyPrice = buyPrice;
// });
// };
$scope.$on('Wallet/Changed', function(event, w) {
if (lodash.isEmpty(w)) {
$log.debug('No wallet provided');
return;
}
wallet = w;
$log.debug('Wallet changed: ' + w.name);
});
// this.get2faCode = function(token) {
// var self = this;
// ongoingProcess.set('Sending 2FA code...', true);
// $timeout(function() {
// glideraService.get2faCode(token, function(err, sent) {
// ongoingProcess.set('Sending 2FA code...', false);
// if (err) {
// popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone'));
// return;
// }
// self.show2faCodeInput = sent;
// });
// }, 100);
// };
$scope.update = function(opts) {
if (!$scope.token || !$scope.permissions) return;
$log.debug('Updating Glidera Account...');
var accessToken = $scope.token;
var permissions = $scope.permissions;
opts = opts || {};
glideraService.getStatus(accessToken, function(err, data) {
$scope.status = data;
});
glideraService.getLimits(accessToken, function(err, limits) {
$scope.limits = limits;
});
if (permissions.transaction_history) {
glideraService.getTransactions(accessToken, function(err, data) {
$scope.txs = data;
});
}
if (permissions.view_email_address && opts.fullUpdate) {
glideraService.getEmail(accessToken, function(err, data) {
$scope.email = data.email;
});
}
if (permissions.personal_info && opts.fullUpdate) {
glideraService.getPersonalInfo(accessToken, function(err, data) {
$scope.personalInfo = data;
});
}
};
this.getBuyPrice = function(token, price) {
var self = this;
if (!price || (price && !price.qty && !price.fiat)) {
this.buyPrice = null;
return;
}
this.gettingBuyPrice = true;
glideraService.buyPrice(token, price, function(err, buyPrice) {
self.gettingBuyPrice = false;
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again'));
return;
}
self.buyPrice = buyPrice;
});
};
this.get2faCode = function(token) {
var self = this;
ongoingProcess.set('Sending 2FA code...', true);
$timeout(function() {
glideraService.get2faCode(token, function(err, sent) {
ongoingProcess.set('Sending 2FA code...', false);
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone'));
return;
}
self.show2faCodeInput = sent;
});
}, 100);
};
this.sendRequest = function(token, permissions, twoFaCode) {
var self = this;

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $interval, $filter, $timeout, $ionicScrollDelegate, gettextCatalog, walletService, platformInfo, lodash, configService, rateService, $stateParams, $window, $state, $log, profileService, bitcore, gettext, txFormatService, ongoingProcess, $ionicModal, popupService, $ionicHistory, $ionicConfig, payproService, feeService, amazonService) {
angular.module('copayApp.controllers').controller('confirmController', function($rootScope, $scope, $interval, $filter, $timeout, $ionicScrollDelegate, gettextCatalog, walletService, platformInfo, lodash, configService, rateService, $stateParams, $window, $state, $log, profileService, bitcore, gettext, txFormatService, ongoingProcess, $ionicModal, popupService, $ionicHistory, $ionicConfig, payproService, feeService, amazonService, glideraService, bwcError) {
var cachedTxp = {};
var toAmount;
var isChromeApp = platformInfo.isChromeApp;
@ -24,6 +24,8 @@ angular.module('copayApp.controllers').controller('confirmController', function(
// Glidera parameters
$scope.isGlidera = data.stateParams.isGlidera;
$scope.glideraAccessToken = data.stateParams.glideraAccessToken;
$scope.glideraBuy = data.stateParams.glideraBuy;
$scope.glideraSell = data.stateParams.glideraSell;
toAmount = data.stateParams.toAmount;
cachedSendMax = {};
@ -46,7 +48,8 @@ angular.module('copayApp.controllers').controller('confirmController', function(
var config = configService.getSync().wallet;
$scope.feeLevel = config.settings && config.settings.feeLevel ? config.settings.feeLevel : 'normal';
$scope.network = (new bitcore.Address($scope.toAddress)).network.name;
if ($scope.isGlidera) $scope.network = glideraService.getEnvironment();
else $scope.network = (new bitcore.Address($scope.toAddress)).network.name;
resetValues();
setwallets();
});
@ -129,29 +132,13 @@ angular.module('copayApp.controllers').controller('confirmController', function(
txFormatService.formatAlternativeStr(toAmount, function(v) {
$scope.alternativeAmountStr = v;
});
if ($scope.isGlidera && $scope.glideraBuy) $scope.getBuyPrice();
};
function resetValues() {
$scope.displayAmount = $scope.displayUnit = $scope.fee = $scope.alternativeAmountStr = $scope.insufficientFunds = $scope.noMatchingWallet = null;
};
$scope.getBuyPrice = function(token, price) {
if (!price || (price && !price.qty && !price.fiat)) {
$scope.buyPrice = null;
return;
}
$scope.gettingBuyPrice = true;
glideraService.buyPrice(token, price, function(err, buyPrice) {
$scope.gettingBuyPrice = false;
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again'));
return;
}
$scope.buyPrice = buyPrice;
});
};
$scope.getSendMaxInfo = function() {
resetValues();
@ -330,6 +317,7 @@ angular.module('copayApp.controllers').controller('confirmController', function(
$scope.wallet = wallet;
$scope.fee = $scope.txp = null;
if ($scope.isGlidera) return;
if (stop) {
$timeout.cancel(stop);
stop = null;
@ -445,6 +433,32 @@ angular.module('copayApp.controllers').controller('confirmController', function(
return;
}
if ($scope.isGlidera) {
$scope.get2faCode(function(err, sent) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone'));
return;
}
if (sent) {
var title = gettextCatalog.getString("Please, enter the code below");
var message = gettextCatalog.getString("A SMS containing a confirmation code was sent to your phone.");
popupService.showPrompt(title, message, null, function(twoFaCode) {
$scope.sendRequest(twoFaCode, function(err, data) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err);
return;
}
$scope.success = data;
$timeout(function() {
$scope.$digest();
});
})
});
}
});
return;
}
var wallet = $scope.wallet;
if (!wallet) {
return setSendError(gettextCatalog.getString('No wallet selected'));
@ -558,24 +572,53 @@ angular.module('copayApp.controllers').controller('confirmController', function(
}
};
$scope.get2faCode = function(token, cb) {
$scope.get2faCode = function(cb) {
ongoingProcess.set('Sending 2FA code...', true);
$timeout(function() {
glideraService.get2faCode(token, function(err, sent) {
glideraService.get2faCode($scope.glideraAccessToken, function(err, sent) {
ongoingProcess.set('Sending 2FA code...', false);
return cb(err, sent);
});
}, 100);
};
$scope.sendRequest = function(twoFaCode, cb) {
ongoingProcess.set('Buying Bitcoin...', true);
$timeout(function() {
walletService.getAddress($scope.wallet, false, function(err, walletAddr) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone'));
ongoingProcess.set('Buying Bitcoin...', false);
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.cb(err, 'Could not create address'));
return;
}
var title = gettextCatalog.getString("Please, enter the code below");
var message = gettextCatalog.getString("A SMS containing a confirmation code was sent to your phone.");
popupService.showPrompt(title, message, null, function(code) {
return cb(code);
var data = {
destinationAddress: walletAddr,
qty: $scope.buyPrice.qty,
priceUuid: $scope.buyPrice.priceUuid,
useCurrentPrice: false,
ip: null
};
glideraService.buy($scope.glideraAccessToken, twoFaCode, data, function(err, data) {
ongoingProcess.set('Buying Bitcoin...', false);
return cb(err, data)
});
});
}, 100);
};
$scope.getBuyPrice = function() {
var satToBtc = 1 / 100000000;
var price = {};
price.qty = (toAmount * satToBtc).toFixed(8);
glideraService.buyPrice($scope.glideraAccessToken, price, function(err, buyPrice) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again'));
return;
}
$scope.buyPrice = buyPrice;
});
};
function publishAndSign(wallet, txp, onSendStatusChange) {
walletService.publishAndSign(wallet, txp, function(err, txp) {
if (err) return setSendError(err);