uoa-repository-manager-service/app/features/administration/categories-management/create-category-dialog/create-category-dialog.comp...

87 lines
2.9 KiB
TypeScript

import { CategoryFormComponent } from './../../forms/category-form/category-form.component';
import { Category } from './../../../../shared/models/category.interface';
import { CategoriesService } from '../../../../shared/services/administration/categories.service';
import { Component, Input, OnInit, Output, EventEmitter, ViewChild } from '@angular/core';
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
@Component({
selector: 'app-create-category-dialog',
templateUrl: './create-category-dialog.component.html',
styleUrls: ['./create-category-dialog.component.scss']
})
export class CreateCategoryDialogComponent implements OnInit {
@ViewChild(CategoryFormComponent) private categoryForm: CategoryFormComponent;
@Input() displayDialog: boolean;
@Input() header: string;
@Input() categoryToEdit: Category;
@Output() cancelled = new EventEmitter<boolean>();
@Output() valueChange = new EventEmitter<any>();
displayValidationMessagesEvenIfPristine: boolean;
constructor(private categoriesService: CategoriesService,
private notificationHandling: NotificationsHandlingService) { }
ngOnInit(): void {
this.reset(); // Make sure everything is cleanly initialised.
}
reset() {
this.displayValidationMessagesEvenIfPristine = false;
this.categoryForm?.resetForm();
}
onShow() {
// If a categoryToEdit was provided.
if (this.categoryToEdit) {
this.categoryForm.setValue(this.categoryToEdit);
}
}
cancel() {
this.cancelled.emit(true);
}
addCategory() {
// If the form is not valid, make sure validator messages are displayed and then return without doing anything.
if (!this.categoryForm.isValid()) {
this.displayValidationMessagesEvenIfPristine = true;
return;
}
// TODO: What is returned from backend on create()??
this.categoriesService.createNewCategory(this.categoryForm.formValue()).subscribe(result => {
this.valueChange.emit(result);
this.notificationHandling.showCreateCategorySuccess();
this.cancel();
this.notificationHandling.showCreateNewCategorySuccess();
},
error => {
});
}
editCategory() {
// If the form is not valid, make sure validator messages are displayed and then return without doing anything.
if (!this.categoryForm.isValid()) {
this.displayValidationMessagesEvenIfPristine = true;
return;
}
// TODO: What is returned from backend on create()??
// TODO: It's to ugly having to provide the categoryId separately.
this.categoriesService.updateCategory(this.categoryForm.formValue()).subscribe(result => {
this.valueChange.emit(result);
this.cancel();
this.notificationHandling.showUpdateCategorySuccess();
},
error => {
});
}
}