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 { OperationService } from './operation.service';
var OperationController = require('./operation.controller');
import { OperationController } from './operation.controller';
export default angular.module('accountant.operations', [
ngResource,

View File

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