import { Component, OnInit, Inject, OnDestroy } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA, PageEvent } from '@angular/material'; import { KEY_LOGIN_RES_INFO } from '@app/types'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { Store } from '@ngrx/store'; import * as StatusStore from '@app/store/messenger/status'; import { UserInfoSS, QueryProtocolService, DeptSearchType, SSVC_TYPE_QUERY_DEPT_USER_DATA, DeptUserData, SSVC_TYPE_QUERY_DEPT_USER_RES, DeptUserResponse } from '@ucap-webmessenger/protocol-query'; import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; import { map, catchError } from 'rxjs/operators'; import { Subscription, of } from 'rxjs'; import { EnvironmentsInfo, KEY_ENVIRONMENTS_INFO } from '@app/types'; import { NGXLogger } from 'ngx-logger'; import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native'; import { environment } from '../../../../../environments/environment'; import { TranslateService } from '@ngx-translate/core'; export interface IntegratedSearchDialogData { keyword: string; } export interface IntegratedSearchDialogResult {} @Component({ selector: 'app-integrated-search.dialog', templateUrl: './integrated-search.dialog.component.html', styleUrls: ['./integrated-search.dialog.component.scss'] }) export class IntegratedSearchDialogComponent implements OnInit, OnDestroy { loginRes: LoginResponse; environmentsInfo: EnvironmentsInfo; searchSubscription: Subscription; searchUserInfos: UserInfoSS[] = []; searchingProcessing = false; currentSearchWord: string; totalCount = 0; pageCurrent = 1; pageListCount = 20; constructor( public dialogRef: MatDialogRef< IntegratedSearchDialogData, IntegratedSearchDialogResult >, @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService, @Inject(MAT_DIALOG_DATA) public data: IntegratedSearchDialogData, private queryProtocolService: QueryProtocolService, private sessionStorageService: SessionStorageService, private store: Store, private logger: NGXLogger ) { this.loginRes = this.sessionStorageService.get( KEY_LOGIN_RES_INFO ); this.environmentsInfo = this.sessionStorageService.get( KEY_ENVIRONMENTS_INFO ); } ngOnInit() { this.onSearch(this.data.keyword); } onSearch(searchWord: string) { this.currentSearchWord = this.data.keyword; if (searchWord.trim().length > 0) { this.searchingProcessing = true; const searchUserInfos: UserInfoSS[] = []; this.searchSubscription = this.queryProtocolService .deptUser({ divCd: 'INT_SRCH', companyCode: this.loginRes.companyCode, searchRange: DeptSearchType.All, search: searchWord.trim(), senderCompanyCode: this.loginRes.companyCode, senderEmployeeType: this.loginRes.userInfo.employeeType, pageCurrent: this.pageCurrent, pageListCount: this.pageListCount }) .pipe( map(res => { switch (res.SSVC_TYPE) { case SSVC_TYPE_QUERY_DEPT_USER_DATA: const userInfos = (res as DeptUserData).userInfos; searchUserInfos.push(...userInfos); break; case SSVC_TYPE_QUERY_DEPT_USER_RES: { const response = res as DeptUserResponse; // 검색 결과 처리. this.searchUserInfos = searchUserInfos; this.totalCount = response.pageTotalCount; this.pageCurrent = response.pageCurrent; this.pageListCount = response.pageListCount; this.searchingProcessing = false; // 검색 결과에 따른 프레즌스 조회. const userSeqList: number[] = []; if (userSeqList.length > 0) { this.store.dispatch( StatusStore.bulkInfo({ divCd: 'INT_SRCH', userSeqs: this.searchUserInfos.map(user => user.seq) }) ); } console.log(this.searchUserInfos); } break; } }), catchError(error => { this.searchingProcessing = false; return of(this.logger.error(error)); }) ) .subscribe(); } else { // clear list. this.searchingProcessing = false; this.searchUserInfos = []; } } ngOnDestroy(): void { if (!!this.searchSubscription) { this.searchSubscription.unsubscribe(); } } onChangePage(event: PageEvent) { this.pageCurrent = event.pageIndex + 1; this.pageListCount = event.pageSize; this.onSearch(this.currentSearchWord); } }