From 4071893ed5df8e9780cda9f1022b823421e10dbf Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Thu, 14 Jun 2018 11:50:03 +0200 Subject: [PATCH] Improve Operation loading in edit component. --- src/operations/operationEdit.component.ts | 41 ++++++++++++++--------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/operations/operationEdit.component.ts b/src/operations/operationEdit.component.ts index 5e19d8a..c65174c 100644 --- a/src/operations/operationEdit.component.ts +++ b/src/operations/operationEdit.component.ts @@ -2,7 +2,7 @@ import { Location } from '@angular/common'; import { Component, ViewChild } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; +import { ActivatedRoute, Router, Params } from '@angular/router'; import { Logger } from '@nsalaun/ng-logger'; import { ToastrService } from 'ngx-toastr'; @@ -15,7 +15,7 @@ import { OperationService } from './operation.service'; templateUrl: './operationEdit.component.html' }) export class OperationEditComponent { - public operation = new Operation(); + public operation: Operation = new Operation(); //dateMask = [/\d{4}/, '-', /0[1-9]|1[0-2]/, '-', /[0-2]\d|3[0-1]/]; dateMask = ['2', '0', /\d/, /\d/, '-', /[0-1]/, /\d/, '-', /[0-3]/, /\d/]; @@ -23,29 +23,38 @@ export class OperationEditComponent { constructor( private location: Location, private router: Router, - private activatedRoute: ActivatedRoute, + private route: ActivatedRoute, private logger: Logger, private toastrService: ToastrService, private operationService: OperationService ) {} ngOnInit() { - let operationId = this.activatedRoute.snapshot.paramMap.get('operationId'); + this.route.paramMap.subscribe((params: Params) => { + let operationId = params.get('operationId'); - if (operationId) { - // Load Operation - this.operationService.get( - +operationId - ).subscribe((operation: Operation) => { - this.operation = operation; + if (operationId) { + this.logger.info('Loading operation with id', operationId); - this.logger.info(operation); - }); - } else { - let accountId = this.activatedRoute.snapshot.paramMap.get('accountId'); + // Load Operation + this.operationService.get( + +operationId + ).subscribe((operation: Operation) => { + this.operation = operation; - this.operation.account_id = +accountId; - } + this.logger.info(operation); + }); + } else { + this.logger.info('Initialize new operation'); + + let accountId = params.get('accountId'); + + this.operation = new Operation(); + this.operation.account_id = +accountId; + + this.logger.info(this.operation); + } + }); } submit(): void {