1
|
import { CategoryFormComponent } from './../../forms/category-form/category-form.component';
|
2
|
import { Category } from './../../../../shared/models/category.interface';
|
3
|
import { CategoriesService } from '../../../../shared/services/administration/categories.service';
|
4
|
import { Component, Input, OnInit, Output, EventEmitter, ViewChild } from '@angular/core';
|
5
|
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
|
6
|
|
7
|
@Component({
|
8
|
selector: 'app-create-category-dialog',
|
9
|
templateUrl: './create-category-dialog.component.html',
|
10
|
styleUrls: ['./create-category-dialog.component.scss']
|
11
|
})
|
12
|
export class CreateCategoryDialogComponent implements OnInit {
|
13
|
|
14
|
@ViewChild(CategoryFormComponent) private categoryForm: CategoryFormComponent;
|
15
|
@Input() displayDialog: boolean;
|
16
|
@Input() header: string;
|
17
|
@Input() categoryToEdit: Category;
|
18
|
@Output() cancelled = new EventEmitter<boolean>();
|
19
|
@Output() valueChange = new EventEmitter<any>();
|
20
|
|
21
|
displayValidationMessagesEvenIfPristine: boolean;
|
22
|
|
23
|
constructor(private categoriesService: CategoriesService,
|
24
|
private notificationHandling: NotificationsHandlingService) { }
|
25
|
|
26
|
ngOnInit(): void {
|
27
|
this.reset(); // Make sure everything is cleanly initialised.
|
28
|
}
|
29
|
|
30
|
reset() {
|
31
|
this.displayValidationMessagesEvenIfPristine = false;
|
32
|
this.categoryForm?.resetForm();
|
33
|
}
|
34
|
|
35
|
onShow() {
|
36
|
// If a categoryToEdit was provided.
|
37
|
if (this.categoryToEdit) {
|
38
|
this.categoryForm.setValue(this.categoryToEdit);
|
39
|
}
|
40
|
}
|
41
|
|
42
|
cancel() {
|
43
|
this.cancelled.emit(true);
|
44
|
}
|
45
|
|
46
|
addCategory() {
|
47
|
// If the form is not valid, make sure validator messages are displayed and then return without doing anything.
|
48
|
if (!this.categoryForm.isValid()) {
|
49
|
this.displayValidationMessagesEvenIfPristine = true;
|
50
|
return;
|
51
|
}
|
52
|
|
53
|
// TODO: What is returned from backend on create()??
|
54
|
this.categoriesService.createNewCategory(this.categoryForm.formValue()).subscribe(result => {
|
55
|
this.valueChange.emit(result);
|
56
|
this.notificationHandling.showCreateCategorySuccess();
|
57
|
this.cancel();
|
58
|
this.notificationHandling.showCreateNewCategorySuccess();
|
59
|
|
60
|
},
|
61
|
error => {
|
62
|
|
63
|
});
|
64
|
|
65
|
}
|
66
|
|
67
|
editCategory() {
|
68
|
// If the form is not valid, make sure validator messages are displayed and then return without doing anything.
|
69
|
if (!this.categoryForm.isValid()) {
|
70
|
this.displayValidationMessagesEvenIfPristine = true;
|
71
|
return;
|
72
|
}
|
73
|
|
74
|
// TODO: What is returned from backend on create()??
|
75
|
// TODO: It's to ugly having to provide the categoryId separately.
|
76
|
this.categoriesService.updateCategory(this.categoryForm.formValue()).subscribe(result => {
|
77
|
this.valueChange.emit(result);
|
78
|
this.cancel();
|
79
|
this.notificationHandling.showUpdateCategorySuccess();
|
80
|
|
81
|
},
|
82
|
error => {
|
83
|
|
84
|
});
|
85
|
}
|
86
|
}
|