srumboard_frontend/src/app/task-form/task-form.component.ts
2020-07-06 23:06:05 +02:00

185 lines
4.1 KiB
TypeScript

import { Component, OnInit, Input } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import {
BackendService,
ScrumTask,
Priority,
ScrumStatus,
ScrumCategory,
ScrumUser,
ScrumProject,
ScrumUserstory
} from '../services/backend.service';
import { Observable } from 'rxjs';
import { HttpResponse } from '@angular/common/http';
@Component({
selector: 'app-task-form',
templateUrl: './task-form.component.html',
styleUrls: [ './task-form.component.css' ]
})
export class TaskFormComponent implements OnInit {
@Input() public task: ScrumTask;
public editing: boolean;
public creating: boolean;
public userstoryId: string;
public userstories: any[] = [];
public allStatus: any[] = [];
public status: ScrumStatus = {title: "", description: ""};
public allUser: any[] = [];
public user: ScrumUser = { name: "" };
constructor(private backendService: BackendService, private activeModalService: NgbActiveModal) {
this.getUserStories();
this.getTaskStatus();
this.getAllUsers();
}
ngOnInit(): void {
if (this.task === null || this.task === undefined) {
this.task = { title: '' };
this.editing = false;
this.creating = false;
} else if (this.task.userstoryid) {
this.editing = true;
} else {
this.creating = true;
}
document.getElementById('titleField').focus();
this.getRelatedStory();
}
onSubmit() {
if (this.editing) {
this.backendService.putTask(this.task).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
}
});
} else {
this.backendService.postTask(this.task).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
}
});
}
this.activeModalService.close(this.task);
}
onClose() {
this.activeModalService.dismiss(this.task);
}
getRelatedStory() {
if(!this.task.userstoryid)
{
return null;
}
this.backendService.getUserstory(this.task.userstoryid).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.userstoryId = response.body.title;
}
});
}
getUserStories() {
this.backendService.getUserstories().subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.userstories.push(...response.body);
}
});
}
getTaskStatus() {
this.backendService.getAllStatus().subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allStatus.push(...response.body);
}
});
}
createTaskStatus(status:ScrumStatus) {
this.backendService.postStatus(status).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
}
else
{
this.allStatus.push(response.body);
}
});
}
deleteStatus(id: number) {
var status = this.allStatus.find((x) => x.id === id);
this.backendService.deleteStatus(status).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
}
else
{
const index = this.allStatus.indexOf(status);
if (index !== -1) {
this.allStatus.splice(index, 1);
}
}
this.task.statusid=null;
});
}
getAllPriorities(): Priority[] {
return Object.values(Priority);
}
getUserstoryTitleById(id: number): string{
if (!id) {
return null;
}
var story = this.userstories.find((x) => x.id === id);
if (!story) {
return null;
}
return story.title;
}
getStatusTitleById(id: number) :string{
if (!id) {
return null;
}
var status = this.allStatus.find((x) => x.id === id);
if (!status) {
return null;
}
return status.title;
}
// Methods for choosing creator of a userstory.
getAllUsers() {
this.backendService.getUsers().subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allUser.push(...response.body);
}
});
}
getAuthorById(id: number): string {
if (!id) {
return null;
}
var user = this.allUser.find((x) => x.id === id);
if (!user) {
return null;
}
return user.name;
}
}