Change account controller into class.

This commit is contained in:
Alexis Lahouze 2017-07-13 17:14:20 +02:00
parent 995af2fbd3
commit bda2c90e3d
2 changed files with 31 additions and 27 deletions

View File

@ -1,14 +1,21 @@
var accountFormTmpl = require('./account.form.tmpl.html'),
accountDeleteTmpl = require('./account.delete.tmpl.html');
module.exports = function(Account, AccountBalances, Notification, $log, $modal) {
var vm = this;
export class AccountController {
static $inject = ['Account', 'AccountBalances', 'Notification', '$log', '$modal'];
accounts: Account[];
constructor(private Account, private AccountBalances, private Notification, private $log, private $modal) {
// Load accounts.
this.load();
}
/*
* Return the class for an account current value compared to authorized
* overdraft.
*/
vm.rowClass = function(account) {
rowClass(account) {
// eslint-disable-next-line camelcase
if (!account || !account.authorized_overdraft || !account.current) {
return;
@ -25,7 +32,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/*
* Return the class for a value compared to account authorized overdraft.
*/
vm.valueClass = function(account, value) {
valueClass(account, value) {
if (!account || !value) {
return;
}
@ -38,10 +45,10 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
}
};
vm.load = function() {
load() {
vm.accounts = Account.query({}, function(result) {
return result.map(function(item) {
item.balances = AccountBalances.get({id: item.id});
item.balances = this.AccountBalances.get({id: item.id});
return item;
});
});
@ -50,39 +57,39 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/*
* Add an empty account.
*/
vm.add = function() {
add() {
var account = new Account({
// eslint-disable-next-line camelcase
authorized_overdraft: 0
});
// Insert account at the begining of the array.
return vm.modify(account);
return this.modify(account);
};
/*
* Save account.
*/
vm.save = function(account) {
save(account) {
return account.$save().then(function(data) {
Notification.success('Account #' + data.id + ' saved.');
this.Notification.success('Account #' + data.id + ' saved.');
vm.load();
this.load();
return data;
}, function(result){
$log.error('Error while saving account', account, result);
this.$log.error('Error while saving account', account, result);
Notification.error(
this.Notification.error(
'Error while saving account: ' + result.message
);
});
};
vm.confirmDelete = function(account) {
confirmDelete(account) {
var title = "Delete account #" + account.id;
$modal({
this.$modal({
templateUrl: accountDeleteTmpl,
controller: function($scope, title, account, $delete) {
$scope.title = title;
@ -95,7 +102,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
locals: {
title: title,
account: account,
$delete: vm.delete
$delete: this.delete
}
});
};
@ -103,17 +110,17 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/*
* Delete an account.
*/
vm.delete = function(account) {
delete(account) {
var id = account.id;
return account.$delete().then(function() {
Notification.success('account #' + id + ' deleted.');
this.Notification.success('account #' + id + ' deleted.');
vm.load();
this.load();
return account;
}, function(result) {
Notification.error(
this.Notification.error(
'An error occurred while trying to delete account #' +
id + ':<br />' + result
);
@ -123,7 +130,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
/*
* Open the popup to modify the account, save it on confirm.
*/
vm.modify = function(account) {
modify(account) {
// FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Account";
@ -131,7 +138,7 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
title = title + " #" + account.id;
}
$modal({
this.$modal({
templateUrl: accountFormTmpl,
controller: function($scope, title, account, $save) {
$scope.title = title;
@ -146,11 +153,8 @@ module.exports = function(Account, AccountBalances, Notification, $log, $modal)
locals: {
title: title,
account: account,
$save: vm.save
$save: this.save
}
});
};
// Load accounts.
vm.load();
};

View File

@ -28,7 +28,7 @@ var ngResource = require('angular-resource'),
var AccountFactory = require('./account.factory');
var AccountBalancesFactory = require('./accountBalances.factory');
var AccountConfig = require('./account.config');
var AccountController = require('./account.controller');
import { AccountController } from './account.controller';
export default angular.module('accountant.accounts', [
ngResource,