refactor controller
This commit is contained in:
parent
2d08a8258d
commit
57ae7485a4
2 changed files with 87 additions and 39 deletions
1
copay.js
1
copay.js
|
|
@ -7,6 +7,7 @@ module.exports.HDPath = require('./js/models/HDPath');
|
||||||
module.exports.HDParams = require('./js/models/HDParams');
|
module.exports.HDParams = require('./js/models/HDParams');
|
||||||
module.exports.crypto = require('./js/util/crypto');
|
module.exports.crypto = require('./js/util/crypto');
|
||||||
module.exports.logger = require('./js/util/log');
|
module.exports.logger = require('./js/util/log');
|
||||||
|
module.exports.csv = require('./js/util/csv');
|
||||||
|
|
||||||
|
|
||||||
// components
|
// components
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('HistoryController',
|
angular.module('copayApp.controllers').controller('HistoryController',
|
||||||
function($scope, $rootScope, $filter, rateService) {
|
function($scope, $rootScope, $filter, $timeout, rateService, notification) {
|
||||||
var w = $rootScope.wallet;
|
var w = $rootScope.wallet;
|
||||||
|
|
||||||
$rootScope.title = 'History';
|
$rootScope.title = 'History';
|
||||||
|
|
@ -26,27 +26,60 @@ angular.module('copayApp.controllers').controller('HistoryController',
|
||||||
var w = $rootScope.wallet;
|
var w = $rootScope.wallet;
|
||||||
if (!w) return;
|
if (!w) return;
|
||||||
|
|
||||||
|
var filename = "copay_history.csv";
|
||||||
|
var descriptor = {
|
||||||
|
columns: [
|
||||||
|
{ label: 'Date', property: 'ts', type: 'date' },
|
||||||
|
{ label: 'Amount (' + w.settings.unitName + ')', property: 'amount', type: 'number' },
|
||||||
|
{ label: 'Amount (' + w.settings.alternativeIsoCode + ')', property: 'alternativeAmount' },
|
||||||
|
{ label: 'Action', property: 'action' },
|
||||||
|
{ label: 'AddressTo', property: 'addressTo' },
|
||||||
|
{ label: 'Comment', property: 'comment' },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
if (w.isShared()) {
|
||||||
|
descriptor.columns.push({
|
||||||
|
label: 'Signers',
|
||||||
|
property: function(obj) {
|
||||||
|
if (!obj.actionList) return '';
|
||||||
|
return _.map(obj.actionList, function(action) {
|
||||||
|
return w.publicKeyRing.nicknameForCopayer(action.cId);
|
||||||
|
}).join('|');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$scope.generating = true;
|
$scope.generating = true;
|
||||||
|
|
||||||
w.getTransactionHistoryCsv(function(csvContent) {
|
$scope._getTransactions(w, null, function(err, res) {
|
||||||
if (csvContent && csvContent !== 'ERROR') {
|
if (err) {
|
||||||
var filename = "copay_history.csv";
|
$scope.generating = false;
|
||||||
|
logger.error(err);
|
||||||
var encodedUri = encodeURI(csvContent);
|
notification.error('Could not get transaction history');
|
||||||
var link = document.createElement("a");
|
return;
|
||||||
link.setAttribute("href", encodedUri);
|
|
||||||
link.setAttribute("download", filename);
|
|
||||||
|
|
||||||
link.click();
|
|
||||||
}
|
}
|
||||||
$scope.generating = false;
|
$scope._addRates(w, res.items, function (err) {
|
||||||
$scope.$digest();
|
copay.csv.toCsv(res.items, descriptor, function (err, res) {
|
||||||
})
|
if (err) {
|
||||||
|
$scope.generating = false;
|
||||||
|
logger.error(err);
|
||||||
|
notification.error('Could not generate csv file');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var csvContent = "data:text/csv;charset=utf-8," + res;
|
||||||
|
var encodedUri = encodeURI(csvContent);
|
||||||
|
var link = document.createElement("a");
|
||||||
|
link.setAttribute("href", encodedUri);
|
||||||
|
link.setAttribute("download", filename);
|
||||||
|
link.click();
|
||||||
|
$scope.generating = false;
|
||||||
|
$scope.$digest();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$scope.update = function() {
|
$scope.update = function() {
|
||||||
$scope.getTransactions();
|
$scope.getTransactions();
|
||||||
};
|
};
|
||||||
|
|
@ -58,6 +91,37 @@ angular.module('copayApp.controllers').controller('HistoryController',
|
||||||
}, 1);
|
}, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$scope._getTransactions = function (w, opts, cb) {
|
||||||
|
w.getTransactionHistory(opts, function(err, res) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
if (!res) return cb();
|
||||||
|
|
||||||
|
var now = new Date();
|
||||||
|
var items = res.items;
|
||||||
|
_.each(items, function(tx) {
|
||||||
|
tx.ts = tx.minedTs || tx.sentTs;
|
||||||
|
tx.rateTs = Math.floor((tx.ts || now) / 1000);
|
||||||
|
tx.amount = $filter('noFractionNumber')(tx.amount);
|
||||||
|
});
|
||||||
|
return cb(null, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
$scope._addRates = function (w, txs, cb) {
|
||||||
|
if (!txs || txs.length == 0) return cb();
|
||||||
|
var index = _.indexBy(txs, 'rateTs');
|
||||||
|
rateService.getHistoricRates(w.settings.alternativeIsoCode, _.keys(index), function(err, res) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
if (!res) return cb();
|
||||||
|
_.each(res, function(r) {
|
||||||
|
var tx = index[r.ts];
|
||||||
|
var alternativeAmount = (r.rate != null ? tx.amountSat * rateService.SAT_TO_BTC * r.rate : null);
|
||||||
|
tx.alternativeAmount = alternativeAmount ? $filter('noFractionNumber')(alternativeAmount, 2) : null;
|
||||||
|
});
|
||||||
|
return cb();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
$scope.getTransactions = function() {
|
$scope.getTransactions = function() {
|
||||||
var w = $rootScope.wallet;
|
var w = $rootScope.wallet;
|
||||||
if (!w) return;
|
if (!w) return;
|
||||||
|
|
@ -65,7 +129,7 @@ angular.module('copayApp.controllers').controller('HistoryController',
|
||||||
$scope.blockchain_txs = w.cached_txs || [];
|
$scope.blockchain_txs = w.cached_txs || [];
|
||||||
$scope.loading = true;
|
$scope.loading = true;
|
||||||
|
|
||||||
w.getTransactionHistory({
|
$scope._getTransactions(w, {
|
||||||
currentPage: $scope.currentPage,
|
currentPage: $scope.currentPage,
|
||||||
itemsPerPage: $scope.itemsPerPage,
|
itemsPerPage: $scope.itemsPerPage,
|
||||||
}, function(err, res) {
|
}, function(err, res) {
|
||||||
|
|
@ -78,28 +142,11 @@ angular.module('copayApp.controllers').controller('HistoryController',
|
||||||
}
|
}
|
||||||
|
|
||||||
var items = res.items;
|
var items = res.items;
|
||||||
var now = new Date();
|
$scope._addRates(w, items, function (err) {
|
||||||
_.each(items, function(tx) {
|
$timeout(function() {
|
||||||
tx.ts = tx.minedTs || tx.sentTs;
|
$scope.$digest();
|
||||||
tx.rateTs = Math.floor((tx.ts || now) / 1000);
|
}, 1);
|
||||||
tx.amount = $filter('noFractionNumber')(tx.amount);
|
})
|
||||||
});
|
|
||||||
|
|
||||||
if (items.length > 0) {
|
|
||||||
var index = _.indexBy(items, 'rateTs');
|
|
||||||
rateService.getHistoricRates(w.settings.alternativeIsoCode, _.keys(index), function(err, res) {
|
|
||||||
if (!err && res) {
|
|
||||||
_.each(res, function(r) {
|
|
||||||
var tx = index[r.ts];
|
|
||||||
var alternativeAmount = (r.rate != null ? tx.amountSat * rateService.SAT_TO_BTC * r.rate : null);
|
|
||||||
tx.alternativeAmount = alternativeAmount ? $filter('noFractionNumber')(alternativeAmount, 2) : null;
|
|
||||||
});
|
|
||||||
setTimeout(function() {
|
|
||||||
$scope.$digest();
|
|
||||||
}, 1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.blockchain_txs = w.cached_txs = items;
|
$scope.blockchain_txs = w.cached_txs = items;
|
||||||
$scope.nbPages = res.nbPages;
|
$scope.nbPages = res.nbPages;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue