accountant-ui/src/operations/operationEditModal.component.ts
2017-08-13 12:28:10 +02:00

64 lines
1.6 KiB
TypeScript

// vim: set tw=80 ts=2 sw=2 sts=2:
import { Component, Input, ViewChild } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Operation } from './operation';
import { OperationFormComponent } from './operationForm.component';
@Component({
selector: 'operation-edit-modal',
template: `
<div class="modal-header">
<h3 class="modal-title" id="modal-title">{{ title() }}</h3>
</div>
<div class="modal-body" id="modal-body">
<operation-form [operation]="operation" (submit)="submit()" #operationForm="operationForm"></operation-form>
</div>
<div class="modal-footer">
<button class="btn btn-primary" [disabled]="!operationForm.form.valid" (click)="submit()">
Save
</button>
<button class="btn btn-default" (click)="cancel()">
Cancel
</button>
</div>
`
})
export class OperationEditModalComponent {
@Input() operation: Operation;
@ViewChild('operationForm') operationForm: OperationFormComponent;
valid: boolean = false;
constructor(private activeModal: NgbActiveModal) {}
title(): string {
if(this.operation.id) {
return "Operation #" + this.operation.id;
} else {
return "New operation";
}
}
submit(): void {
let formModel = this.operationForm.form.value;
let operation = Object.assign({}, this.operation);
operation.id = this.operation.id;
operation.operation_date = formModel.operationDate;
operation.label = formModel.label;
operation.value = formModel.value;
operation.category = formModel.category;
this.activeModal.close(operation);
}
cancel(): void {
this.activeModal.dismiss("closed");
}
}