accountant-ui/src/operations/operationEdit.component.ts

87 lines
2.2 KiB
TypeScript

// vim: set tw=80 ts=2 sw=2 sts=2 :
import { Location } from '@angular/common';
import { Component } from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { Logger } from '@nsalaun/ng-logger';
import { ToastrService } from 'ngx-toastr';
import { Operation } from './operation';
import { OperationService } from './operation.service';
@Component({
selector: 'operation-edit',
templateUrl: './operationEdit.component.html'
})
export class OperationEditComponent {
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/];
constructor(
private location: Location,
private router: Router,
private route: ActivatedRoute,
private logger: Logger,
private toastrService: ToastrService,
private operationService: OperationService
) {}
ngOnInit() {
this.route.paramMap.subscribe((params: Params) => {
let operationId = params.get('operationId');
if (operationId) {
this.logger.info('Loading operation with id', operationId);
// Load Operation
this.operationService.get(
+operationId
).subscribe((operation: Operation) => {
this.operation = operation;
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 {
this.save(this.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();
}
}