import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core'; import { Observable, combineLatest, Subscription } from 'rxjs'; import { map, tap } from 'rxjs/operators'; import { Store, select } from '@ngrx/store'; import { ucapAnimations, DialogService } from '@ucap-webmessenger/ui'; import { UserInfo, GroupDetailData } from '@ucap-webmessenger/protocol-sync'; import * as AppStore from '@app/store'; import * as ChatStore from '@app/store/messenger/chat'; import * as QueryStore from '@app/store/messenger/query'; import { NGXLogger } from 'ngx-logger'; import { Company } from '@ucap-webmessenger/api-external'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { LoginInfo, KEY_LOGIN_INFO } from '@app/types'; import { ExpansionPanelComponent as GroupExpansionPanelComponent } from '@ucap-webmessenger/ui-group'; import { CreateGroupDialogComponent, CreateGroupDialogData, CreateGroupDialogResult } from '@app/layouts/messenger/dialogs/create-group.dialog.component'; import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; import { DeptSearchType, UserInfoSS, UserInfoF, UserInfoDN } from '@ucap-webmessenger/protocol-query'; import { MatMenuTrigger } from '@angular/material'; import { VersionInfo2Response } from '@ucap-webmessenger/api-public'; import { KEY_VER_INFO } from '@app/types/ver-info.type'; import { DeleteGroupDialogComponent, DeleteGroupDialogData, DeleteGroupDialogResult } from '@app/layouts/messenger/dialogs/delete-group.dialog.component'; import { EditGroupDialogComponent, EditGroupDialogData, EditGroupDialogResult } from '@app/layouts/messenger/dialogs/edit-group.dialog.component'; import { EditGroupMemberDialogComponent, EditGroupMemberDialogData, EditGroupMemberDialogResult } from '@app/layouts/messenger/dialogs/edit-group-member.dialog.component'; @Component({ selector: 'app-layout-chat-left-sidenav-group', templateUrl: './group.component.html', styleUrls: ['./group.component.scss'], animations: ucapAnimations }) export class GroupComponent implements OnInit, OnDestroy { @ViewChild('groupExpansionPanel', { static: true }) groupExpansionPanel: GroupExpansionPanelComponent; @ViewChild('profileContextMenuTrigger', { static: true }) profileContextMenuTrigger: MatMenuTrigger; @ViewChild('groupContextMenuTrigger', { static: true }) groupContextMenuTrigger: MatMenuTrigger; profileContextMenuPosition = { x: '0px', y: '0px' }; groupContextMenuPosition = { x: '0px', y: '0px' }; groupBuddyList$: Observable< { group: GroupDetailData; buddyList: UserInfo[] }[] >; favoritBuddyList$: Observable; companyList$: Observable; companyCode: string; loginRes: LoginResponse; loginResSubscription: Subscription; sessionVerinfo = this.sessionStorageService.get( KEY_VER_INFO ); constructor( private store: Store, private sessionStorageService: SessionStorageService, private dialogService: DialogService, private logger: NGXLogger ) {} ngOnInit() { const loginInfo = this.sessionStorageService.get(KEY_LOGIN_INFO); this.companyCode = loginInfo.companyCode; this.loginResSubscription = this.store .pipe( select(AppStore.AccountSelector.AuthenticationSelector.loginRes), tap(loginRes => { this.loginRes = loginRes; }) ) .subscribe(); this.companyList$ = this.store.pipe( select(AppStore.SettingSelector.CompanySelector.companyList) ); this.groupBuddyList$ = combineLatest([ this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.selectAllBuddy2) ), this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.selectAllGroup2) ) ]).pipe( map(([buddyList, groupList]) => { const groupBuddyList: { group: GroupDetailData; buddyList: UserInfo[]; }[] = []; for (const group of groupList) { groupBuddyList.push({ group, buddyList: buddyList.filter(buddy => { return group.userSeqs.indexOf(buddy.seq) > -1; }) }); } return groupBuddyList; }) ); this.favoritBuddyList$ = this.store .pipe(select(AppStore.MessengerSelector.SyncSelector.selectAllBuddy2)) .pipe( map(buddyInfoList => { return buddyInfoList .filter(buddy => buddy.isFavorit) .sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)); }) ); } ngOnDestroy(): void { if (!!this.loginResSubscription) { this.loginResSubscription.unsubscribe(); } } async onClickGroupMenu(menuType: string) { this.logger.debug('menuType', menuType); switch (menuType) { case 'GROUP_NEW': { const result = await this.dialogService.open< CreateGroupDialogComponent, CreateGroupDialogData, CreateGroupDialogResult >(CreateGroupDialogComponent, { width: '220px', data: { title: 'Logout', message: 'Logout ?' } }); } break; case 'GROUP_EXPAND_MORE': { this.groupExpansionPanel.expandMore(); } break; case 'GROUP_EXPAND_LESS': { this.groupExpansionPanel.expandLess(); } break; case 'GROUP_SAVE': break; case 'GROUP_RESTORE': break; default: break; } } onSelectBuddy(buddy: UserInfo) { this.logger.debug('onSelectBuddy', buddy); this.store.dispatch(ChatStore.openRoom({ userSeqList: [buddy.seq] })); } getStatusBulkInfo(buddy: UserInfo) { return this.store.pipe( select( AppStore.MessengerSelector.StatusSelector.selectEntitiesStatusBulkInfo ), map(statusBulkInfo => !!statusBulkInfo ? statusBulkInfo[buddy.seq] : undefined ) ); } onKeyDownEnterOrganizationTenantSearch(params: { companyCode: string; searchWord: string; }) { this.store.dispatch( QueryStore.deptUser({ divCd: 'GRP', companyCode: params.companyCode, searchRange: DeptSearchType.All, search: params.searchWord, senderCompanyCode: params.companyCode, senderEmployeeType: this.loginRes.userInfo.employeeType }) ); } onClickProfileContextMenu( menuType: string, userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN ) { this.logger.debug( 'onClickProfileContextMenu', 'menuType', menuType, 'userInfo', userInfo ); } onContextMenuProfile( event: MouseEvent, userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN ) { event.preventDefault(); event.stopPropagation(); this.profileContextMenuPosition.x = event.clientX + 'px'; this.profileContextMenuPosition.y = event.clientY + 'px'; this.profileContextMenuTrigger.menu.focusFirstItem('mouse'); this.profileContextMenuTrigger.menuData = { userInfo }; this.profileContextMenuTrigger.openMenu(); } async onClickGroupContextMenu(menuType: string, group: GroupDetailData) { this.logger.debug( 'onClickGroupContextMenu', 'menuType', menuType, 'group', group ); switch (menuType) { case 'CHAT': this.store.dispatch( ChatStore.openRoom({ userSeqList: group.userSeqs }) ); break; case 'SEND_NOTE': break; case 'RENAME': { const result = await this.dialogService.open< EditGroupDialogComponent, EditGroupDialogData, EditGroupDialogResult >(EditGroupDialogComponent, { width: '220px', data: { title: 'Logout', message: 'Logout ?' } }); } break; case 'EDIT_MEMBER': { const result = await this.dialogService.open< EditGroupMemberDialogComponent, EditGroupMemberDialogData, EditGroupMemberDialogResult >(EditGroupMemberDialogComponent, { width: '220px', data: { title: 'Logout', message: 'Logout ?' } }); } break; case 'DELETE': { const result = await this.dialogService.open< DeleteGroupDialogComponent, DeleteGroupDialogData, DeleteGroupDialogResult >(DeleteGroupDialogComponent, { width: '220px', data: { title: 'Logout', message: 'Logout ?' } }); } break; case 'EDIT_MEMBER': { const result = await this.dialogService.open< EditGroupMemberDialogComponent, EditGroupMemberDialogData, EditGroupMemberDialogResult >(EditGroupMemberDialogComponent, { width: '220px', data: { title: 'Logout', message: 'Logout ?' } }); } break; default: break; } } onMoreGroup(params: { event: MouseEvent; group: GroupDetailData }) { params.event.preventDefault(); params.event.stopPropagation(); this.groupContextMenuPosition.x = params.event.clientX + 'px'; this.groupContextMenuPosition.y = params.event.clientY + 'px'; this.groupContextMenuTrigger.menu.focusFirstItem('mouse'); this.groupContextMenuTrigger.menuData = { group: params.group }; this.groupContextMenuTrigger.openMenu(); } }