1
|
import { environment } from './../../../../../environments/environment.prod';
|
2
|
import { ErrorHandlingService } from './../../../../shared/services/error-handling/error-handling.service';
|
3
|
import { AuthService } from 'src/app/shared/services/auth.service';
|
4
|
import { CategoryFormComponent } from './../../forms/category-form/category-form.component';
|
5
|
import { Category } from './../../../../shared/models/category.interface';
|
6
|
import { DocumentClassificationsService } from './../../../../shared/services/administration/document-classifications.service';
|
7
|
import { Component, OnInit, Output, EventEmitter, Input, ViewChild } from '@angular/core';
|
8
|
import { FormBuilder } from '@angular/forms';
|
9
|
import { CategoriesService } from 'src/app/shared/services/administration/categories.service';
|
10
|
import { DocumentClassification } from 'src/app/shared/models/document-classification.interface';
|
11
|
import { Page } from '../../../../shared/models/paging/page.interface';
|
12
|
import { USER_RIGHTS } from 'src/app/shared/enums/USER_RIGHTS.enum';
|
13
|
|
14
|
@Component({
|
15
|
selector: 'app-categories-search',
|
16
|
templateUrl: './categories-search.component.html',
|
17
|
styleUrls: ['./categories-search.component.scss']
|
18
|
})
|
19
|
export class CategoriesSearchComponent implements OnInit {
|
20
|
|
21
|
@Input() set paginationEventRequest(value: { page: number, offset: number }) {
|
22
|
|
23
|
// This setter may be called on page setup without arguments.
|
24
|
if (!value) {
|
25
|
return;
|
26
|
}
|
27
|
this._value = Object.assign(value);
|
28
|
this.search(value.page, value.offset);
|
29
|
}
|
30
|
|
31
|
@Input() set valueTableChange(results: any) {
|
32
|
if (results) {
|
33
|
this.search(this._value.page, this._value.offset);
|
34
|
}
|
35
|
}
|
36
|
|
37
|
@Output() searchResults = new EventEmitter<Page<Category>>();
|
38
|
@Output() loading = new EventEmitter<boolean>();
|
39
|
@Output() searchInitiated = new EventEmitter<boolean>();
|
40
|
@Input() passChange: any;
|
41
|
|
42
|
@ViewChild(CategoryFormComponent)
|
43
|
private categoryForm: CategoryFormComponent;
|
44
|
_value: { page: number, offset: number };
|
45
|
lastSearchCriteria: { docClassificationId: number | string, categoryName: string, categoryCode: string };
|
46
|
|
47
|
constructor(
|
48
|
private categoriesService: CategoriesService,
|
49
|
private authService: AuthService,
|
50
|
private errorHandlingService: ErrorHandlingService
|
51
|
) { }
|
52
|
|
53
|
ngOnInit(): void {
|
54
|
}
|
55
|
|
56
|
clear() {
|
57
|
this.categoryForm.resetForm();
|
58
|
}
|
59
|
|
60
|
searchButtonClicked() {
|
61
|
|
62
|
// Inform anyone interested there is a new search underway.
|
63
|
this.searchInitiated.emit(true);
|
64
|
|
65
|
// Remember the search for future pagination/whatever requests.
|
66
|
let categoryFormValue = this.categoryForm.formValue();
|
67
|
let docClassId = categoryFormValue.documentClassification ? categoryFormValue.documentClassification.classificationId : null;
|
68
|
let catName = categoryFormValue.categoryName;
|
69
|
let catCode = categoryFormValue.categoryCode;
|
70
|
|
71
|
this.lastSearchCriteria = {
|
72
|
docClassificationId: docClassId ? docClassId : '',
|
73
|
categoryName: catName ? catName : '',
|
74
|
categoryCode: catCode ? catCode : ''
|
75
|
};
|
76
|
|
77
|
// Then actually search.
|
78
|
this.search();
|
79
|
}
|
80
|
|
81
|
search(pageNumber = 0, pageSize = 10) {
|
82
|
this.loading.emit(true);
|
83
|
|
84
|
// A search may be issued by the paginationEventRequest setter upon setting up the page.
|
85
|
if (!this.lastSearchCriteria) {
|
86
|
return;
|
87
|
}
|
88
|
|
89
|
this.categoriesService.searchByCriteriaPaged(pageNumber, pageSize, this.lastSearchCriteria, 'search').subscribe((results: Page<Category>) => {
|
90
|
this.searchResults.emit(results);
|
91
|
this.loading.emit(false);
|
92
|
},err => {
|
93
|
this.loading.emit(false);
|
94
|
this.errorHandlingService.showHttpResponseError(err);
|
95
|
});
|
96
|
}
|
97
|
|
98
|
canPreviewAllCategories(): boolean {
|
99
|
return this.authService.userHasRightForClient(USER_RIGHTS.H01, environment.globalRightsClientID);
|
100
|
}
|
101
|
}
|