import { Component, OnInit } from '@angular/core'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { DropDownsHR } from '../../../_helpers/dropdowns.class'; import { RootService } from '../../../_services/root.service'; import { ApplicationFtsService } from '../application-fts.service'; import { AuthenticationService } from '../../../_services/authentication.service'; import { orderBy, SortDescriptor } from '@progress/kendo-data-query'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { Subject } from 'rxjs/Subject'; import { debounceTime } from 'rxjs/operators/debounceTime'; @Component({ selector: 'app-application-list', templateUrl: './list.component.html', styles: [] }) export class ListComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public dropDowns: DropDownsHR = new DropDownsHR(); public inputChange: Subject; public searchQuery: string = ''; public typeId: number = 0; public statusId: number = 0; constructor(private _rootService: RootService, private _applicationFtsService: ApplicationFtsService, private _authenticationService: AuthenticationService) { } ngOnInit() { this.inputChange = new Subject(); this.inputChange.pipe(debounceTime(300)).subscribe((query) => { this.searchQuery = query; if (this.searchQuery.length <= 2 && this.searchQuery.length != 0) { return; } this.getApplications(); }); this.initializeProps(); this.loadDropDownValues(); this.getApplications(); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 50; this.kGrid.pageSizes = [20,50, 100]; } private loadDropDownValues() { this.getApplicationTypes(); this.getApplicationStatus(); } private getApplicationTypes() { this._rootService.getApplicationTypes().subscribe( (response: any) => { this.dropDowns.applicationTypes = response; this.dropDowns.applicationTypesData = this.dropDowns.applicationTypes; }, err => this.handleError(err) ); } private getApplicationStatus() { this._rootService.getApplicationStatus().subscribe( (response: any) => { this.dropDowns.applicationStatus = response; this.dropDowns.applicationStatusData = this.dropDowns.applicationStatus; }, err => this.handleError(err) ); } public getApplications() { this.kGrid.loading = true; this._applicationFtsService.getApplications(this.kGrid.skip, this.kGrid.pageSize, this.searchQuery, this.typeId, this.statusId).subscribe( (response: any) => { this.kGrid.data = []; this.kGrid.data = response.List; this.kGrid.totalRecords = response.Count; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; }, err => this.handleError(err) ); } public dropdownValueChanged = (value, filter) => { if (filter == 'types') { this.typeId = value.Id; } if (filter == 'status') { this.statusId = value.Id; } } public sortChange(sort: SortDescriptor[]): void { if (sort[0].field == 'asd') { return; } this.kGrid.sort = sort; this.sortData(); } private sortData() { this.kGrid.gridView = { data: orderBy(this.kGrid.data, this.kGrid.sort), total: this.kGrid.totalRecords }; } public pageChange(event: PageChangeEvent): void { this.kGrid.skip = event.skip; this.getApplications(); } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; this.getApplications(); } private handleError(err: any) { this.kGrid.loading = false; if (err.status == 403) { this._authenticationService.logout(); } } public dashifyCNIC(cnic: string) { if (!cnic) return ''; return cnic[0] + cnic[1] + cnic[2] + cnic[3] + cnic[4] + '-' + cnic[5] + cnic[6] + cnic[7] + cnic[8] + cnic[9] + cnic[10] + cnic[11] + '-' + cnic[12]; } }