import { Component, OnInit, OnDestroy } from '@angular/core'; import { ucapAnimations } from '@ucap-webmessenger/ui'; import { Store, select } from '@ngrx/store'; import { NGXLogger } from 'ngx-logger'; import { Observable, Subscriber, Subscription } from 'rxjs'; import { Info, EventType } from '@ucap-webmessenger/protocol-event'; import * as AppStore from '@app/store'; import * as EventStore from '@app/store/messenger/event'; import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { LoginInfo, KEY_LOGIN_INFO } from '@app/types'; import { RoomInfo } from '@ucap-webmessenger/protocol-room'; import { map, tap } from 'rxjs/operators'; @Component({ selector: 'app-layout-messenger-messages', templateUrl: './messages.component.html', styleUrls: ['./messages.component.scss'], animations: ucapAnimations }) export class MessagesComponent implements OnInit, OnDestroy { loginRes: LoginResponse; loginResSubscription: Subscription; eventList$: Observable; roomInfo: RoomInfo; roomInfoSubscription: Subscription; constructor( private store: Store, private sessionStorageService: SessionStorageService, private logger: NGXLogger ) {} ngOnInit() { const loginInfo = this.sessionStorageService.get(KEY_LOGIN_INFO); this.loginResSubscription = this.store .pipe( select(AppStore.AccountSelector.AuthenticationSelector.loginRes), tap(loginRes => { this.loginRes = loginRes; }) ) .subscribe(); this.roomInfoSubscription = this.store .pipe( select(AppStore.MessengerSelector.RoomSelector.roomInfo), tap(roomInfo => { this.roomInfo = roomInfo; }) ) .subscribe(); this.eventList$ = this.store.pipe( select(AppStore.MessengerSelector.EventSelector.infoList) ); } ngOnDestroy(): void { if (!!this.loginResSubscription) { this.loginResSubscription.unsubscribe(); } if (!!this.roomInfoSubscription) { this.roomInfoSubscription.unsubscribe(); } } selectContact() {} onSendMessage(message: string) { this.store.dispatch( EventStore.send({ senderSeq: this.loginRes.userSeq, req: { roomSeq: this.roomInfo.roomSeq, eventType: EventType.Character, sentMessage: message } }) ); } }