diff --git a/src/operations/operationList.component.ts b/src/operations/operationList.component.ts index 243fc4f..4c03b6d 100644 --- a/src/operations/operationList.component.ts +++ b/src/operations/operationList.component.ts @@ -48,9 +48,9 @@ import { OperationEditModalComponent } from './operationEditModal.component'; - + diff --git a/src/operations/operationNew.component.ts b/src/operations/operationNew.component.ts index f196bdf..4395582 100644 --- a/src/operations/operationNew.component.ts +++ b/src/operations/operationNew.component.ts @@ -1,19 +1,83 @@ // vim: set tw=80 ts=2 sw=2 sts=2 : -import { Component, OnInit } from '@angular/core'; +import { Location } from '@angular/common'; +import { Component, ViewChild } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; +import { Logger } from '@nsalaun/ng-logger'; +import { ToastrService } from 'ngx-toastr'; + +import { Operation } from './operation'; +import { OperationService } from './operation.service'; +import { OperationFormComponent } from './operationForm.component'; + @Component({ selector: 'operation-new', template: `
+ + +
+ +
+ + +
` }) -export class OperationNewComponent implements OnInit { - ngOnInit(): void { - - } +export class OperationNewComponent { + @ViewChild('operationForm') operationForm: OperationFormComponent; + + constructor( + private location: Location, + private router: Router, + private activatedRoute: ActivatedRoute, + private logger: Logger, + private toastrService: ToastrService, + private operationService: OperationService + ) {} + + submit(): void { + let accountId = this.activatedRoute.snapshot.paramMap.get('accountId'); + let formModel = this.operationForm.form.value; + let operation = new Operation(); + + operation.account_id = +accountId; + operation.operation_date = formModel.operationDate; + operation.label = formModel.label; + operation.value = formModel.value; + operation.category = formModel.category; + + this.save(operation); + } + + /* + * Save an operation and return a promise. + */ + save(operation) { + operation.confirmed = true; + + return this.operationService.create(operation).subscribe( + (operation) => { + this.toastrService.success('Operation #' + operation.id + ' saved.'); + + this.location.back(); + }, (result) => { + this.toastrService.error( + 'Error while saving operation: ' + result.message + ); + } + ); + } + + cancel(): void { + this.location.back(); + } }