66 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-05-11 10:46:27 +02:00
import { Component, OnInit, ViewChild } from '@angular/core';
import { Title } from "@angular/platform-browser";
import { MatTable } from "@angular/material";
import { ActivatedRoute, Router } from "@angular/router";
2018-05-11 10:46:27 +02:00
import { Judge } from "../shared/judge";
import { JudgeService } from "../shared/judge.service";
import { JudgeTitle } from "../shared/judge-title";
2018-05-10 16:39:05 +02:00
@Component({
selector: 'app-judge-editor',
templateUrl: './judge-editor.component.html',
styleUrls: ['./judge-editor.component.css']
})
export class JudgeEditorComponent implements OnInit {
@ViewChild(MatTable) private table: MatTable<Array<JudgeTitle>>;
2018-05-11 10:46:27 +02:00
public judgedYearInput: JudgeTitle = new JudgeTitle();
2018-05-11 10:46:27 +02:00
public displayedColumns = ['buttons', 'year', 'title'];
2018-05-10 16:39:05 +02:00
public judge: Judge = new Judge();
2018-05-10 16:39:05 +02:00
2018-05-11 10:46:27 +02:00
constructor(
private judgeService: JudgeService,
private titleService: Title,
private route: ActivatedRoute,
private router: Router
2018-05-11 10:46:27 +02:00
) {}
2018-05-10 16:39:05 +02:00
ngOnInit() {
2018-05-11 10:46:27 +02:00
this.titleService.setTitle('Edit judge');
this.route.data.subscribe((data: {
judge: Judge,
2018-05-11 10:46:27 +02:00
}) => {
this.judge = data.judge ? data.judge : new Judge();
2018-05-11 10:46:27 +02:00
});
}
get canAdd(): boolean {
return this.judgedYearInput.year != null
&& this.judgedYearInput.title.trim().length > 0;
2018-05-10 16:39:05 +02:00
}
get canSave(): boolean {
return this.judge.name.trim().length > 0;
}
2018-05-11 10:46:27 +02:00
public addToYear() {
let appendable = Object.assign({}, this.judgedYearInput);
this.judge.titles.push(appendable);
this.judgedYearInput = new JudgeTitle();
if (this.table) { this.table.renderRows(); }
2018-05-11 10:46:27 +02:00
}
public removeFromYear(year: number) {
this.judge.titles = this.judge.titles.filter(
2018-05-11 10:46:27 +02:00
row => row.year !== year
);
}
public saveJudge() {
if (this.canSave) {
this.judgeService.persist(this.judge).subscribe(() => this.router.navigate(['/judges']));
}
}
2018-05-10 16:39:05 +02:00
}