import { Component, OnInit, OnDestroy } from '@angular/core'; import { ucapAnimations } from '@ucap-webmessenger/ui'; import { NGXLogger } from 'ngx-logger'; import { Store, select } from '@ngrx/store'; import { Subscription, combineLatest, Observable } from 'rxjs'; import { RoomInfo, UserInfoShort, UserInfo as RoomUserInfo } from '@ucap-webmessenger/protocol-room'; import * as AppStore from '@app/store'; import * as ChatStore from '@app/store/messenger/chat'; import { tap, map } from 'rxjs/operators'; import { RoomUserDetailData, RoomUserData } from '@ucap-webmessenger/protocol-sync'; import { VersionInfo2Response } from '@ucap-webmessenger/api-public'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { KEY_VER_INFO } from '@app/types/ver-info.type'; import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; @Component({ selector: 'app-layout-chat-left-sidenav-chat', templateUrl: './chat.component.html', styleUrls: ['./chat.component.scss'], animations: ucapAnimations }) export class ChatComponent implements OnInit, OnDestroy { roomList: RoomInfo[]; roomUserList: RoomUserDetailData[]; roomUserShortList: RoomUserData[]; sessionVerinfo: VersionInfo2Response; loginRes$: Observable; roomSubscription: Subscription; constructor( private store: Store, private logger: NGXLogger, private sessionStorageService: SessionStorageService ) {} ngOnInit() { this.roomSubscription = combineLatest([ this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.selectAllRoom) ), this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.selectAllRoomUser) ), this.store.pipe( select(AppStore.MessengerSelector.SyncSelector.selectAllRoomUserShort) ) ]) .pipe( tap(([room, roomUser, roomUserShort]) => { this.roomList = room; this.roomUserList = roomUser; this.roomUserShortList = roomUserShort; }) ) .subscribe(); this.loginRes$ = this.store.pipe( select(AppStore.AccountSelector.AuthenticationSelector.loginRes) ); this.sessionVerinfo = this.sessionStorageService.get( KEY_VER_INFO ); } ngOnDestroy(): void { if (!!this.roomSubscription) { this.roomSubscription.unsubscribe(); } } onSelectedRoom(roomInfo: RoomInfo) { this.store.dispatch(ChatStore.selectedRoom({ roomSeq: roomInfo.roomSeq })); } getRoomUserList(roomInfo: RoomInfo): RoomUserInfo[] | UserInfoShort[] { if (!!this.roomUserList && 0 < this.roomUserList.length) { const i = this.roomUserList.findIndex( value => roomInfo.roomSeq === value.roomSeq ); if (-1 < i) { return this.roomUserList[i].userInfos; } } if (!!this.roomUserShortList && 0 < this.roomUserShortList.length) { const i = this.roomUserShortList.findIndex( value => roomInfo.roomSeq === value.roomSeq ); if (-1 < i) { return this.roomUserShortList[i].userInfos; } } } }