Fix callbacks.

This commit is contained in:
Alexis Lahouze 2017-07-30 09:21:50 +02:00
parent ecf38725e3
commit 3eba873eb5
2 changed files with 61 additions and 50 deletions

View File

@ -36,7 +36,7 @@ import { BalanceChartComponent } from './balanceChart.component';
import { CategoryChartComponent } from './categoryChart.component'; import { CategoryChartComponent } from './categoryChart.component';
import { OperationService } from './operation.service'; import { OperationService } from './operation.service';
var OperationController = require('./operation.controller'); import { OperationController } from './operation.controller';
export default angular.module('accountant.operations', [ export default angular.module('accountant.operations', [
ngResource, ngResource,

View File

@ -1,89 +1,100 @@
var operationFormTmpl = require('./operation.form.tmpl.html'), import { Component, Inject, OnInit } from '@angular/core';
operationDeleteTmpl = require('./operation.delete.tmpl.html');
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
import { ToastrService } from 'ngx-toastr'; import { ToastrService } from 'ngx-toastr';
var operationFormTmpl = require('./operation.form.tmpl.html'),
operationDeleteTmpl = require('./operation.delete.tmpl.html');
import { Account } from '../accounts/account';
import { AccountService } from '../accounts/account.service'; import { AccountService } from '../accounts/account.service';
import { Operation } from './operation'; import { Operation } from './operation';
import { OperationService } from './operation.service'; import { OperationService } from './operation.service';
module.exports = function( export class OperationController {
$modal, private account: Account;
accountIdService, private minDate: Date;
toastrService: ToastrService, private maxDate: Date;
operationService: OperationService, private operations: Operation[];
AccountService: AccountService
){ constructor(
var vm = this; private $modal,
private accountIdService,
private toastrService: ToastrService,
private operationService: OperationService,
private AccountService: AccountService
){
AccountService.get(this.accountIdService.get()).subscribe(account => {
this.account = account
});
}
/* /*
* Add an empty operation. * Add an empty operation.
*/ */
vm.add = function() { add() {
var operation = new Operation(); var operation = new Operation();
operation.account_id = accountIdService.get(); operation.account_id = this.accountIdService.get();
return vm.modify(operation); return this.modify(operation);
}; };
/* /*
* Load operations. * Load operations.
*/ */
vm.load = function(minDate, maxDate) { load(minDate, maxDate) {
vm.minDate = minDate; this.minDate = minDate;
vm.maxDate = maxDate; this.maxDate = maxDate;
return operationService.query( return this.operationService.query(
accountIdService.get(), this.accountIdService.get(),
minDate, minDate,
maxDate maxDate
).subscribe((operations: Operation[]) => { ).subscribe((operations: Operation[]) => {
vm.operations = operations; this.operations = operations;
}); });
}; };
/* /*
* Toggle pointed indicator for an operation. * Toggle pointed indicator for an operation.
*/ */
vm.togglePointed = function(operation, rowform) { togglePointed(operation, rowform) {
operation.pointed = !operation.pointed; operation.pointed = !operation.pointed;
vm.save(operation); this.save(operation);
}; };
/* /*
* Toggle cancel indicator for an operation. * Toggle cancel indicator for an operation.
*/ */
vm.toggleCanceled = function(operation) { toggleCanceled(operation) {
operation.canceled = !operation.canceled; operation.canceled = !operation.canceled;
vm.save(operation); this.save(operation);
}; };
/* /*
* Save an operation and return a promise. * Save an operation and return a promise.
*/ */
vm.save = function(operation) { save(operation) {
operation.confirmed = true; operation.confirmed = true;
var observable: Observable<Operation>; var observable: Observable<Operation>;
if(operation.id){ if(operation.id){
observable = operationService.update(operation); observable = this.operationService.update(operation);
} else { } else {
observable = operationService.create(operation); observable = this.operationService.create(operation);
} }
return observable.subscribe((operation) => { return observable.subscribe((operation) => {
toastrService.success('Operation #' + operation.id + ' saved.'); this.toastrService.success('Operation #' + operation.id + ' saved.');
vm.load(); this.load(this.minDate, this.maxDate);
return operation; return operation;
}, (result) => { }, (result) => {
toastrService.error( this.toastrService.error(
'Error while saving operation: ' + result.message 'Error while saving operation: ' + result.message
); );
}); });
@ -92,15 +103,15 @@ module.exports = function(
/* /*
* Delete an operation and return a promise. * Delete an operation and return a promise.
*/ */
vm.confirmDelete = function(operation) { confirmDelete(operation) {
var title = "Delete operation #" + operation.id; var title = "Delete operation #" + operation.id;
$modal({ this.$modal({
templateUrl: operationDeleteTmpl, templateUrl: operationDeleteTmpl,
controller: function($scope, title, operation, $delete) { controller: function($scope, title, operation, $delete) {
$scope.title = title; $scope.title = title;
$scope.operation = operation; $scope.operation = operation;
$scope.$delete = function() { $scope.$delete = () => {
$scope.$hide(); $scope.$hide();
$delete($scope.operation); $delete($scope.operation);
}; };
@ -108,22 +119,24 @@ module.exports = function(
locals: { locals: {
title: title, title: title,
operation: operation, operation: operation,
$delete: vm.delete $delete: (operation: Operation) => {
this.delete(operation);
}
} }
}); });
}; };
vm.delete = function(operation) { delete(operation) {
var id = operation.id; var id = operation.id;
return operationService.delete(operation).subscribe(() => { return this.operationService.delete(operation).subscribe(() => {
toastrService.success('Operation #' + id + ' deleted.'); this.toastrService.success('Operation #' + id + ' deleted.');
vm.load(); this.load(this.minDate, this.maxDate);
return operation; return operation;
}, (result) => { }, (result) => {
toastrService.error( this.toastrService.error(
'An error occurred while trying to delete operation #' + 'An error occurred while trying to delete operation #' +
id + ':<br />' + result id + ':<br />' + result
); );
@ -134,7 +147,7 @@ module.exports = function(
* Open the popup to modify the operation, save it on confirm. * Open the popup to modify the operation, save it on confirm.
* @returns a promise. * @returns a promise.
*/ */
vm.modify = function(operation) { modify(operation) {
// FIXME Alexis Lahouze 2017-06-15 i18n // FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Operation"; var title = "Operation";
@ -142,12 +155,12 @@ module.exports = function(
title = title + " #" + operation.id; title = title + " #" + operation.id;
} }
$modal({ this.$modal({
templateUrl: operationFormTmpl, templateUrl: operationFormTmpl,
controller: function($scope, title, operation, $save) { controller: function($scope, title, operation, $save) {
$scope.title = title; $scope.title = title;
$scope.operation = operation; $scope.operation = operation;
$scope.$save = function() { $scope.$save = () => {
$scope.$hide(); $scope.$hide();
$save($scope.operation); $save($scope.operation);
}; };
@ -155,16 +168,14 @@ module.exports = function(
locals: { locals: {
title: title, title: title,
operation: operation, operation: operation,
$save: vm.save $save: (operation: Operation) => {
this.save(operation);
}
} }
}); });
}; };
vm.onUpdate = function(dateRange) { onUpdate(dateRange) {
vm.load(dateRange.minDate, dateRange.maxDate); this.load(dateRange.minDate, dateRange.maxDate);
}; };
AccountService.get(accountIdService.get()).subscribe(account => {
vm.account = account
});
}; };