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'; 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. */ add() { var operation = new Operation(); operation.account_id = this.accountIdService.get(); return this.modify(operation); }; /* * Load operations. */ load(minDate, maxDate) { this.minDate = minDate; this.maxDate = maxDate; return this.operationService.query( this.accountIdService.get(), minDate, maxDate ).subscribe((operations: Operation[]) => { this.operations = operations; }); }; /* * Toggle pointed indicator for an operation. */ togglePointed(operation, rowform) { operation.pointed = !operation.pointed; this.save(operation); }; /* * Toggle cancel indicator for an operation. */ toggleCanceled(operation) { operation.canceled = !operation.canceled; this.save(operation); }; /* * Save an operation and return a promise. */ save(operation) { operation.confirmed = true; var observable: Observable; if(operation.id){ observable = this.operationService.update(operation); } else { observable = this.operationService.create(operation); } return observable.subscribe((operation) => { this.toastrService.success('Operation #' + operation.id + ' saved.'); this.load(this.minDate, this.maxDate); return operation; }, (result) => { this.toastrService.error( 'Error while saving operation: ' + result.message ); }); }; /* * Delete an operation and return a promise. */ confirmDelete(operation) { var title = "Delete operation #" + operation.id; this.$modal({ templateUrl: operationDeleteTmpl, controller: function($scope, title, operation, $delete) { $scope.title = title; $scope.operation = operation; $scope.$delete = () => { $scope.$hide(); $delete($scope.operation); }; }, locals: { title: title, operation: operation, $delete: (operation: Operation) => { this.delete(operation); } } }); }; delete(operation) { var id = operation.id; return this.operationService.delete(operation).subscribe(() => { this.toastrService.success('Operation #' + id + ' deleted.'); this.load(this.minDate, this.maxDate); return operation; }, (result) => { this.toastrService.error( 'An error occurred while trying to delete operation #' + id + ':
' + result ); }); }; /* * Open the popup to modify the operation, save it on confirm. * @returns a promise. */ modify(operation) { // FIXME Alexis Lahouze 2017-06-15 i18n var title = "Operation"; if (operation.id) { title = title + " #" + operation.id; } this.$modal({ templateUrl: operationFormTmpl, controller: function($scope, title, operation, $save) { $scope.title = title; $scope.operation = operation; $scope.$save = () => { $scope.$hide(); $save($scope.operation); }; }, locals: { title: title, operation: operation, $save: (operation: Operation) => { this.save(operation); } } }); }; onUpdate(dateRange) { this.load(dateRange.minDate, dateRange.maxDate); }; };