srumboard_frontend/src/app/components/forms/task/task-form.component.ts

272 lines
7.0 KiB
TypeScript

/**
* Importing necessary components and interfaces.
*/
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']
})
/**
* Class implements the logic for a popup window form to create and modify tasks.
*/
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();
}
/**
* If no task exists a new on will be created.
* In other cases the task exists and gets modifiable.
*/
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();
}
/**
* A new created task will be saved in the backend (POST).
* If a task already exists, modifying results an update (PUT) to the backend.
*/
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');
} else {
// Copy properties returned by the API
Object.assign(this.task, response.body);
}
});
}
// Closes the popup window after submitting/canceling.
this.activeModalService.close(this.task);
}
/**
* Closes the popup form window (by clicking "close button").
*/
onClose() {
this.activeModalService.dismiss(this.task);
}
/**
* Getting the userstory which is related to a task.
* The related story will be shown in popup window of a 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;
}
});
}
/**
* Getting all userstories from backend to show in a dropdown in popup window.
*/
getUserStories() {
this.backendService.getUserstories().subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.userstories.push(...response.body);
}
});
}
/**
* Getting all available status from backend to list it in status-dropdown in popup window.
*/
getTaskStatus() {
this.backendService.getAllStatus().subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allStatus.push(...response.body);
}
});
}
/**
* If desired a new arbitrary status (such as "Waiting") can be created, which will be stored in an array.
* The new status is available to all tasks.
* @param status Scrumstatus to store in the database
*/
createTaskStatus(status: ScrumStatus) {
this.backendService.postStatus(status).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allStatus.push(response.body);
}
});
}
/**
* If desired a new arbitrary user (such as "Testuser") can be created, which will be stored in an array.
* The new user is available to all tasks.
* @param user ScrumUser to store in the database
*/
createUser(user: ScrumUser) {
this.backendService.postUser(user).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allUser.push(response.body);
}
});
}
/**
* A custom status can even be deleted if not used anymore.
* This will remove the status from status-array
* @param id the id of the chosen status
*/
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;
});
}
/**
* A custom user can even be deleted if not used anymore.
* This will remove the user from user-array
* @param id the id of the chosen user
*/
deleteUser(id: number) {
var user = this.allUser.find((x) => x.id === id);
this.backendService.deleteUser(user).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
const index = this.allUser.indexOf(user);
if (index !== -1) {
this.allUser.splice(index, 1);
}
}
this.task.assignedToId = null;
});
}
/**
* Getting the value of the priority enum to be shown in a dropdown in popup window.
*/
getAllPriorities(): Priority[] {
return Object.values(Priority);
}
/**
* Shows the before choosen userstory in the userstory-field in the popup window
* @param id reference to the userstory object
*/
getUserstoryTitleById(id: number): string {
if (!id) {
return null;
}
var story = this.userstories.find((x) => x.id === id);
if (!story) {
return null;
}
return story.title;
}
/**
* Shows the before choosen status in the status-field in the popup window.
* @param id reference to the status object
*/
getStatusTitleById(id: number): string {
if (!id) {
return null;
}
var status = this.allStatus.find((x) => x.id === id);
if (!status) {
return null;
}
return status.title;
}
/**
* Getting all taskboard users from the backend to show in a dropdown in popup window.
*/
getAllUsers() {
this.backendService.getUsers().subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allUser.push(...response.body);
}
});
}
/**
* Shows the before assigned user in the author-field in the popup window.
* @param id reference to the author object
*/
getAuthorNameById(id: number): string {
if (!id) {
return null;
}
var user = this.allUser.find((x) => x.id === id);
if (user == null) {
return null;
}
return user.name;
}
}