Wallet/src/js/controllers/buyGlidera.js

140 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
angular.module('copayApp.controllers').controller('buyGlideraController',
2015-09-12 23:49:14 -03:00
function($scope, $timeout, $modal, profileService, addressService, glideraService, bwsError, lodash, isChromeApp) {
2015-09-01 18:53:47 -03:00
2015-09-11 13:11:41 -03:00
var self = this;
2015-09-02 19:16:59 -03:00
this.show2faCodeInput = null;
this.error = null;
this.success = null;
2015-09-08 12:04:27 -03:00
this.loading = null;
2015-09-01 18:53:47 -03:00
2015-09-11 13:11:41 -03:00
// DISABLE ANIMATION ON CHROMEAPP
if (isChromeApp) {
var animatedSlideUp = 'full';
} else {
var animatedSlideUp = 'full animated slideInUp';
}
this.otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network;
});
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
self.selectedWalletId = null;
self.selectedWalletName = null;
self.selectedWalletAddr = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.wallets = wallets;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
2015-09-12 23:49:14 -03:00
self.error = bwsError.msg({'code': 'WALLET_NOT_COMPLETE'}, 'Could not choose the wallet');
2015-09-11 13:11:41 -03:00
$modalInstance.dismiss('cancel');
return;
}
addressService.getAddress(walletId, false, function(err, walletAddr) {
if (err) {
2015-09-12 23:49:14 -03:00
self.error = bwsError.cb(err, 'Could not create address');
2015-09-11 13:11:41 -03:00
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
'walletAddr': walletAddr
});
});
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
windowClass: animatedSlideUp,
controller: ModalInstanceCtrl,
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass('slideOutDown');
});
modalInstance.result.then(function(obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
self.selectedWalletAddr = obj.walletAddr;
$scope.$apply();
}, 100);
});
};
2015-08-31 18:12:04 -03:00
this.getBuyPrice = function(token, price) {
var self = this;
2015-09-10 16:19:07 -03:00
this.error = null;
2015-09-02 19:16:59 -03:00
if (!price || (price && !price.qty && !price.fiat)) {
this.buyPrice = null;
return;
}
this.gettingBuyPrice = true;
2015-09-07 13:35:59 -03:00
glideraService.buyPrice(token, price, function(err, buyPrice) {
self.gettingBuyPrice = false;
2015-09-07 13:35:59 -03:00
if (err) {
2015-09-12 23:49:14 -03:00
self.error = 'Could not get exchange information. Please, try again.';
2015-09-07 13:35:59 -03:00
}
else {
self.buyPrice = buyPrice;
}
2015-08-31 18:12:04 -03:00
});
2015-09-01 18:53:47 -03:00
};
2015-09-02 19:16:59 -03:00
this.get2faCode = function(token) {
2015-09-01 18:53:47 -03:00
var self = this;
2015-09-12 23:49:14 -03:00
this.loading = 'Sending 2FA code...';
2015-09-02 19:16:59 -03:00
$timeout(function() {
2015-09-07 13:35:59 -03:00
glideraService.get2faCode(token, function(err, sent) {
2015-09-07 17:43:55 -03:00
self.loading = null;
2015-09-07 13:35:59 -03:00
if (err) {
2015-09-12 23:49:14 -03:00
self.error = 'Could not send confirmation code to your phone';
2015-09-07 13:35:59 -03:00
}
else {
2015-09-07 17:43:55 -03:00
self.error = null;
2015-09-07 13:35:59 -03:00
self.show2faCodeInput = sent;
}
2015-09-02 19:16:59 -03:00
});
}, 100);
2015-09-01 18:53:47 -03:00
};
2015-09-07 13:35:59 -03:00
this.sendRequest = function(token, permissions, twoFaCode) {
2015-09-01 18:53:47 -03:00
var self = this;
2015-09-07 13:35:59 -03:00
self.error = null;
2015-09-12 23:49:14 -03:00
self.loading = 'Buying bitcoin...';
2015-09-11 13:11:41 -03:00
var data = {
destinationAddress: self.selectedWalletAddr,
qty: self.buyPrice.qty,
priceUuid: self.buyPrice.priceUuid,
useCurrentPrice: false,
ip: null
};
$timeout(function() {
glideraService.buy(token, twoFaCode, data, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
}
else {
self.success = data;
$scope.$emit('Local/GlideraTx');
}
});
}, 100);
2015-09-01 18:53:47 -03:00
};
2015-08-31 18:12:04 -03:00
});