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

274 lines
7.6 KiB
TypeScript

/**
* Importing necessary components and interfaces.
*/
import { Component, OnInit, Input } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { BackendService, ScrumUserstory, Priority, ScrumStatus, ScrumCategory, ScrumUser } from '../../../services/backend.service';
@Component({
selector: 'app-userstory-form',
templateUrl: './userstory-form.component.html',
styleUrls: ['./userstory-form.component.css']
})
/**
* Class implements the logic for a popup window form to create and modify userstories.
*/
export class UserstoryFormComponent implements OnInit {
@Input() public userstory: ScrumUserstory;
public allStatus: any[] = [];
public status: ScrumStatus = { title: '', description: '' };
public allUser: any[] = [];
public user: ScrumUser = { name: '' };
public allCategories: any[] = [];
public category: ScrumCategory = { title: '' };
public editing: boolean;
constructor(private backendService: BackendService, private activeModalService: NgbActiveModal) {
this.getUserstoryStatus();
this.getAllUsers();
this.getUserstoryCategory();
}
/**
* If no userstory exists a new one will be created.
* In other cases the userstory exists and gets modifiable.
*/
ngOnInit(): void {
if (this.userstory === null || this.userstory === undefined) {
this.userstory = { title: '' };
this.editing = false;
} else {
this.editing = true;
}
document.getElementById('titleField').focus();
}
/**
* A new created userstory will be saved in the backend (POST).
* If a userstory already exists, modifying results an update (PUT) to the backend.
*/
onSubmit() {
if (this.editing) {
this.backendService.putUserstory(this.userstory).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
}
});
} else {
this.backendService.postUserstory(this.userstory).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
// Copy properties returned by the API
Object.assign(this.userstory, response.body);
}
});
}
// Closes the popup window after submitting/canceling.
this.activeModalService.close(this.userstory);
}
/**
* Closes the popup form window (by clicking "close button").
*/
onClose() {
this.activeModalService.dismiss(this.userstory);
}
/**
* Getting all available status from backend to list it in status-dropdown in popup window.
*/
getUserstoryStatus() {
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 userstories.
* @param status the status object which will be created
*/
createUserstoryStatus(status: ScrumStatus) {
this.backendService.postStatus(status).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allStatus.push(response.body);
}
});
}
/**
* A custom status can even be deleted if not used anymore.
* This will remove the status from status-array
* @param id reference to the deletable status object
*/
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.userstory.statusId = null;
});
}
/**
* Getting the values of the Priority enum to be shown in a dropdown in popup window.
*/
getAllPriorities(): Priority[] {
return Object.values(Priority);
}
/**
* 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 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
*/
getAuthorById(id: number): string {
if (!id) {
return null;
}
var user = this.allUser.find((x) => x.id === id);
if (!user) {
return null;
}
return user.name;
}
/**
* Getting all available categories from backend to list it in status-dropdown in popup window.
*/
getUserstoryCategory() {
this.backendService.getCategories().subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allCategories.push(...response.body);
}
});
}
/**
* If desired a new arbitrary category can be created, which will be stored in an array.
* The new category is available to all userstories.
* @param category the category object which will be created
*/
createUserstoryCategory(category: ScrumCategory) {
this.backendService.postCategory(category).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
this.allCategories.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 userstories.
* @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 category can even be deleted if not used anymore.
* This will remove the category from category-array.
* @param id reference to the deletable category
*/
deleteCategory(id: number) {
var category = this.allCategories.find((x) => x.id === id);
this.backendService.deleteCategory(category).subscribe((response) => {
if (response.status > 399) {
alert('Fehler');
} else {
const index = this.allCategories.indexOf(category);
if (index !== -1) {
this.allCategories.splice(index, 1);
}
}
this.userstory.categoryId = 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.userstory.createdById = null;
});
}
/**
* Shows the before choosen category in the category-field in the popup window.
* @param id reference to the category
*/
getCategoryById(id: number): string {
if (!id) {
return null;
}
var category = this.allCategories.find((x) => x.id === id);
if (!category) {
return null;
}
return category.title;
}
}