accountant-ui/src/scheduler/scheduleEditModal.component.ts
2017-10-28 22:16:55 +02:00

65 lines
1.7 KiB
TypeScript

// vim: set tw=80 ts=2 sw=2 sts=2:
import { Component, Inject, ViewChild } from '@angular/core';
import { MdDialogRef, MD_DIALOG_DATA } from '@angular/material';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Schedule } from './schedule';
import { ScheduleFormComponent } from './scheduleForm.component';
@Component({
selector: 'schedule-edit-modal',
template: `
<h3 md-dialog-title>{{ title() }}</h3>
<md-dialog-content>
<schedule-form [schedule]="schedule" (submit)="submit()" #scheduleForm="scheduleForm"></schedule-form>
</md-dialog-content>
<md-dialog-actions>
<button md-raised-button color="primary" [disabled]="!scheduleForm?.form.valid" (click)="submit()">
Save
</button>
<button md-raised-button color="warn" md-dialog-close>
Cancel
</button>
</md-dialog-actions>
`
})
export class ScheduleEditModalComponent {
private schedule: Schedule;
@ViewChild('scheduleForm') scheduleForm: ScheduleFormComponent;
constructor(
@Inject(MD_DIALOG_DATA) public data: any,
public dialogRef: MdDialogRef<ScheduleEditModalComponent>,
) {
this.schedule = data.schedule;
}
title(): string {
if(this.schedule.id) {
return "Schedule #" + this.schedule.id;
} else {
return "New schedule";
}
}
submit(): void {
let formModel = this.scheduleForm.form.value;
let schedule = Object.assign({}, this.schedule);
schedule.id = this.schedule.id;
schedule.start_date = formModel.startDate;
schedule.stop_date = formModel.stopDate;
schedule.day = formModel.day;
schedule.frequency = formModel.frequency;
schedule.label = formModel.label;
schedule.value = formModel.value;
schedule.category = formModel.category;
this.dialogRef.close(schedule);
}
}