From 558988a57ae196607f58340f9ba3e33469bff0cd Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sun, 30 Jul 2017 13:15:49 +0200 Subject: [PATCH] Add Operation Row component. --- src/operations/operation.module.ts | 3 + src/operations/operationRow.component.ts | 169 +++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 src/operations/operationRow.component.ts diff --git a/src/operations/operation.module.ts b/src/operations/operation.module.ts index b229b3b..34f87ef 100644 --- a/src/operations/operation.module.ts +++ b/src/operations/operation.module.ts @@ -10,6 +10,7 @@ import { RestangularModule } from 'ngx-restangular'; import { BalanceChartComponent } from './balanceChart.component'; import { CategoryChartComponent } from './categoryChart.component'; +import { OperationRowComponent } from './operationRow.component'; import { CategoryService } from './category.service'; import { OperationService } from './operation.service'; @@ -45,10 +46,12 @@ export function accountIdServiceFactory(i: any) { declarations: [ BalanceChartComponent, CategoryChartComponent, + OperationRowComponent, ], entryComponents: [ BalanceChartComponent, CategoryChartComponent, + OperationRowComponent, ] }) export class OperationModule {} diff --git a/src/operations/operationRow.component.ts b/src/operations/operationRow.component.ts new file mode 100644 index 0000000..e97e1f6 --- /dev/null +++ b/src/operations/operationRow.component.ts @@ -0,0 +1,169 @@ +// vim: set tw=80 ts=2 sw=2 sts=2 : +import { CurrencyPipe } from '@angular/common'; +import { Component, Inject, Input, Output, EventEmitter } from '@angular/core'; + +import { ToastrService } from 'ngx-toastr'; + +var operationFormTmpl = require('./operation.form.tmpl.html'), + operationDeleteTmpl = require('./operation.delete.tmpl.html'); + +import { Account } from '../accounts/account'; +import { Operation } from './operation'; +import { OperationService } from './operation.service'; + +@Component({ + selector: 'tr[operation-row]', + host: { + "[id]": "operation.id", + "[class.stroke]": "operation.canceled", + "[class.italic]": "!operation.confirmed", + "[class.warning]": "operation.balance < 0, danger: operation.balance < operationsCtrl.account.authorized_overdraft" + }, + template: ` +{{ operation.operation_date | date:"yyyy-MM-dd" }} + +{{ operation.label }} + +{{ operation.value | currency:'EUR':yes }} + + + {{ operation.balance | currency:'EUR':true }} + + +{{ operation.category }} + + +
+ + + + + + + + + + + +
+ + ` +}) +export class OperationRowComponent { + @Input() operation: Operation; + @Output() needsReload: EventEmitter = new EventEmitter(); + + constructor( + private operationService: OperationService, + private toastrService: ToastrService, + @Inject('$modal') private $modal, + ) {} + + togglePointed(operation, rowform) { + operation.pointed = !operation.pointed; + + this.save(operation); + }; + + toggleCanceled(operation) { + operation.canceled = !operation.canceled; + + this.save(operation); + }; + + save(operation) { + return this.operationService.update(operation).subscribe((operation) => { + operation.confirmed = true; + + this.toastrService.success('Operation #' + operation.id + ' saved.'); + + this.needsReload.emit(); + }, (result) => { + this.toastrService.error( + 'Error while saving operation: ' + result.message + ); + }); + } + + 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.needsReload.emit(); + }, (result) => { + this.toastrService.error( + 'An error occurred while trying to delete operation #' + + id + ':
' + result + ); + }); + }; + + modify(operation) { + // FIXME Alexis Lahouze 2017-06-15 i18n + var title = "Modify operation #" + 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); + } + } + }); + }; +}