Ionic modals. Fix select wallet to buy/sell

This commit is contained in:
Gustavo Maximiliano Cortez 2016-06-13 15:00:17 -03:00
commit 2b60bc3066
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
6 changed files with 243 additions and 298 deletions

View file

@ -1,9 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('amazonController',
function($rootScope, $scope, $timeout, $modal, profileService, configService, amazonService, animationService, lodash) {
window.ignoreMobilePause = true;
function($scope, $timeout, $ionicModal, configService, amazonService) {
this.init = function() {
var self = this;
@ -21,90 +19,15 @@ angular.module('copayApp.controllers').controller('amazonController',
});
});
};
this.openCardModal = function(card) {
$rootScope.modalOpened = true;
var self = this;
var fc = profileService.focusedClient;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.card = card;
$scope.card = card;
$scope.cancelGiftCard = function() {
$scope.refresh = true;
var dataSrc = {
creationRequestId: $scope.card.creationRequestId,
gcId: $scope.card.gcId,
bitpayInvoiceId: $scope.card.bitpayInvoiceId,
bitpayInvoiceUrl: $scope.card.bitpayInvoiceUrl,
date: $scope.card.date
};
$scope.loading = true;
amazonService.cancelGiftCard(dataSrc, function(err, data) {
$scope.loading = null;
if (err || data.status != 'SUCCESS') {
$scope.error = err || data.status;
return;
}
$scope.refreshGiftCard();
});
};
$scope.remove = function() {
amazonService.saveGiftCard($scope.card, {remove: true}, function(err) {
$modalInstance.close(true);
});
};
$scope.refreshGiftCard = function() {
$scope.refresh = true;
var dataSrc = {
creationRequestId: $scope.card.creationRequestId,
amount: $scope.card.cardInfo.value.amount,
currencyCode: $scope.card.cardInfo.value.currencyCode,
bitpayInvoiceId: $scope.card.bitpayInvoiceId,
bitpayInvoiceUrl: $scope.card.bitpayInvoiceUrl,
date: $scope.card.date
};
$scope.loading = true;
amazonService.createGiftCard(dataSrc, function(err, data) {
$scope.loading = null;
if (err) {
$scope.error = err;
return;
}
$scope.card = data;
$timeout(function() {
$scope.$digest();
});
});
};
$scope.cancel = lodash.debounce(function() {
$modalInstance.close($scope.refresh);
}, 0, 1000);
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/amazon-card-details.html',
windowClass: animationService.modalAnimated.slideRight,
controller: ModalInstanceCtrl,
$ionicModal.fromTemplateUrl('views/modals/amazon-card-details.html', {
scope: $scope
}).then(function(modal) {
$scope.amazonCardDetailsModal = modal;
$scope.amazonCardDetailsModal.show();
});
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.close($scope.refresh);
});
modalInstance.result.finally(function() {
$rootScope.modalOpened = false;
disableCloseModal();
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutRight);
});
modalInstance.result.then(function(refresh) {
if (refresh) self.init();
}, function() {});
};
});

View file

@ -1,22 +1,15 @@
'use strict';
angular.module('copayApp.controllers').controller('buyAmazonController',
function($rootScope, $scope, $modal, $log, $timeout, lodash, profileService, animationService, bwsError, configService, walletService, fingerprintService, amazonService) {
function($rootScope, $scope, $ionicModal, $log, $timeout, lodash, profileService, bwsError, configService, walletService, fingerprintService, amazonService) {
window.ignoreMobilePause = true;
var self = this;
var fc;
var client;
var minimumAmount = 5;
var stepAmount = 1;
var multiplierAmount = 5;
var maximumAmount = 500;
var otherWallets = function(network) {
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
var handleEncryptedWallet = function(client, cb) {
if (!walletService.isEncrypted(client)) return cb();
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
@ -31,68 +24,38 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
amazonService.setCredentials(network);
amazonService.healthCheckRequest();
amazonService.initUuid();
self.otherWallets = otherWallets(network);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
self.allWallets = profileService.getWallets(network, 1);
client = profileService.focusedClient;
if (client && client.credentials.m == 1) {
$timeout(function() {
self.selectedWalletId = client.credentials.walletId;
self.selectedWalletName = client.credentials.walletName;
$scope.$apply();
}, 100);
}
};
$scope.openWalletsModal = function(wallets) {
if (self.loading) return;
self.error = null;
self.errorInfo = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.type = 'SELL';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
self.error = bwsError.msg('WALLET_NOT_COMPLETE');
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
});
};
};
$scope.type = 'SELL';
$scope.wallets = wallets;
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
$ionicModal.fromTemplateUrl('views/modals/wallets.html', {
scope: $scope
}).then(function(modal) {
$scope.walletsModal = modal;
$scope.walletsModal.show();
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutDown);
});
modalInstance.result.then(function(obj) {
$scope.$on('walletSelected', function(ev, obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
fc = profileService.getClient(obj.walletId);
client = obj.client;
$scope.$apply();
}, 100);
$scope.walletsModal.hide();
});
};
@ -156,7 +119,7 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
};
self.loading = 'Creating transaction...';
walletService.createTx(fc, txp, function(err, createdTxp) {
walletService.createTx(client, txp, function(err, createdTxp) {
self.loading = null;
if (err) {
self.loading = null;
@ -204,38 +167,38 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
this.confirmTx = function(txp, cb) {
fingerprintService.check(fc, function(err) {
fingerprintService.check(client, function(err) {
if (err) {
$log.debug(err);
return cb(err);
}
handleEncryptedWallet(fc, function(err) {
handleEncryptedWallet(client, function(err) {
if (err) {
$log.debug(err);
return bwsError.cb(err, null, cb);
}
walletService.publishTx(fc, txp, function(err, publishedTxp) {
walletService.publishTx(client, txp, function(err, publishedTxp) {
if (err) {
$log.debug(err);
return bwsError.cb(err, null, cb);
}
walletService.signTx(fc, publishedTxp, function(err, signedTxp) {
walletService.lock(fc);
walletService.signTx(client, publishedTxp, function(err, signedTxp) {
walletService.lock(client);
if (err) {
$log.debug(err);
walletService.removeTx(fc, signedTxp, function(err) {
walletService.removeTx(client, signedTxp, function(err) {
if (err) $log.debug(err);
});
return bwsError.cb(err, null, cb);
}
walletService.broadcastTx(fc, signedTxp, function(err, broadcastedTxp) {
walletService.broadcastTx(client, signedTxp, function(err, broadcastedTxp) {
if (err) {
$log.debug(err);
walletService.removeTx(fc, broadcastedTxp, function(err) {
walletService.removeTx(client, broadcastedTxp, function(err) {
if (err) $log.debug(err);
});
return bwsError.cb(err, null, cb);

View file

@ -0,0 +1,57 @@
'use strict';
angular.module('copayApp.controllers').controller('amazonCardDetailsController', function($scope, $timeout, amazonService) {
$scope.cancelGiftCard = function() {
var dataSrc = {
creationRequestId: $scope.card.creationRequestId,
gcId: $scope.card.gcId,
bitpayInvoiceId: $scope.card.bitpayInvoiceId,
bitpayInvoiceUrl: $scope.card.bitpayInvoiceUrl,
date: $scope.card.date
};
$scope.loading = true;
amazonService.cancelGiftCard(dataSrc, function(err, data) {
$scope.loading = null;
if (err || data.status != 'SUCCESS') {
$scope.error = err || data.status;
return;
}
$scope.refreshGiftCard();
});
};
$scope.remove = function() {
amazonService.saveGiftCard($scope.card, {remove: true}, function(err) {
$scope.cancel();
});
};
$scope.refreshGiftCard = function() {
var dataSrc = {
creationRequestId: $scope.card.creationRequestId,
amount: $scope.card.cardInfo.value.amount,
currencyCode: $scope.card.cardInfo.value.currencyCode,
bitpayInvoiceId: $scope.card.bitpayInvoiceId,
bitpayInvoiceUrl: $scope.card.bitpayInvoiceUrl,
date: $scope.card.date
};
$scope.loading = true;
amazonService.createGiftCard(dataSrc, function(err, data) {
$scope.loading = null;
if (err) {
$scope.error = err;
return;
}
$scope.card = data;
$timeout(function() {
$scope.$digest();
});
});
};
$scope.cancel = function() {
$scope.amazonCardDetailsModal.hide();
};
});