Improve Operation loading in edit component.

This commit is contained in:
Alexis Lahouze 2018-06-14 11:50:03 +02:00
parent 1859efb98c
commit 4071893ed5
1 changed files with 25 additions and 16 deletions

View File

@ -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 {