import { Component, OnInit, Inject, ViewChild, OnDestroy } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material'; import { KEY_LOGIN_RES_INFO } from '@app/types/login-res-info.type'; import { KEY_VER_INFO } from '@app/types/ver-info.type'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { Store, select } from '@ngrx/store'; import * as AppStore from '@app/store'; import * as ChatStore from '@app/store/messenger/chat'; import * as SyncStore from '@app/store/messenger/sync'; import * as AuthenticationStore from '@app/store/account/authentication'; import { UserInfo, GroupDetailData } from '@ucap-webmessenger/protocol-sync'; import { UserInfoSS, UserInfoF, UserInfoDN } from '@ucap-webmessenger/protocol-query'; import { DialogService, ConfirmDialogComponent, ConfirmDialogData, ConfirmDialogResult, SnackBarService } from '@ucap-webmessenger/ui'; import { VersionInfo2Response } from '@ucap-webmessenger/api-public'; import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; import { map, take, finalize } from 'rxjs/operators'; import { Subscription } from 'rxjs'; import { SelectGroupDialogComponent, SelectGroupDialogData, SelectGroupDialogResult } from '../group/select-group.dialog.component'; import { FileUploadItem } from '@ucap-webmessenger/api'; import { CommonApiService } from '@ucap-webmessenger/api-common'; import { EnvironmentsInfo, KEY_ENVIRONMENTS_INFO } from '@app/types'; import { StatusCode } from '@ucap-webmessenger/api'; export interface ProfileDialogData { userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN; } export interface ProfileDialogResult {} @Component({ selector: 'app-profile.dialog', templateUrl: './profile.dialog.component.html', styleUrls: ['./profile.dialog.component.scss'] }) export class ProfileDialogComponent implements OnInit, OnDestroy { userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN; loginRes: LoginResponse; sessionVerinfo: VersionInfo2Response; environmentsInfo: EnvironmentsInfo; isMe: boolean; isBuddy: boolean; isFavorit: boolean; selectAllBuddy2Subscription: Subscription; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: ProfileDialogData, private dialogService: DialogService, private sessionStorageService: SessionStorageService, private commonApiService: CommonApiService, private snackBarService: SnackBarService, private store: Store ) { this.sessionVerinfo = this.sessionStorageService.get( KEY_VER_INFO ); this.loginRes = this.sessionStorageService.get( KEY_LOGIN_RES_INFO ); this.environmentsInfo = this.sessionStorageService.get( KEY_ENVIRONMENTS_INFO ); this.userInfo = data.userInfo; } ngOnInit() { this.isMe = this.loginRes.userSeq === this.data.userInfo.seq; this.selectAllBuddy2Subscription = this.store .pipe( select(AppStore.MessengerSelector.SyncSelector.selectAllBuddy2), map(buddyList => { const users = buddyList.filter( buddy => buddy.seq === this.data.userInfo.seq ); this.isBuddy = users.length > 0; if (this.isBuddy) { this.isFavorit = users[0].isFavorit; } }) ) .subscribe(); } ngOnDestroy(): void { if (!!this.selectAllBuddy2Subscription) { this.selectAllBuddy2Subscription.unsubscribe(); } } onClickChat(userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN) { if (userInfo.seq === this.loginRes.userSeq) { this.store.dispatch( ChatStore.openRoom({ userSeqList: [this.loginRes.talkWithMeBotSeq] }) ); } else { this.store.dispatch(ChatStore.openRoom({ userSeqList: [userInfo.seq] })); } this.dialogRef.close(); } onClickToggleFavorit(param: { userInfo: UserInfo | UserInfoF; isFavorit: boolean; }) { this.store.dispatch( SyncStore.updateBuddy({ seq: param.userInfo.seq, isFavorit: param.isFavorit }) ); } async onClickToggleBuddy(param: { userInfo: UserInfo | UserInfoF; isBuddy: boolean; }) { if (param.isBuddy) { // 동료추가. const result = await this.dialogService.open< SelectGroupDialogComponent, SelectGroupDialogData, SelectGroupDialogResult >(SelectGroupDialogComponent, { width: '600px', data: { title: 'Group Select' } }); if (!!result && !!result.choice && result.choice) { if (!!result.group) { const oldGroup: GroupDetailData = result.group; const trgtUserSeq: number[] = []; result.group.userSeqs.map(seq => trgtUserSeq.push(seq)); trgtUserSeq.push(param.userInfo.seq); this.store.dispatch( SyncStore.updateGroupMember({ oldGroup, trgtUserSeq }) ); } } } else { // 동료삭제. const result = await this.dialogService.open< ConfirmDialogComponent, ConfirmDialogData, ConfirmDialogResult >(ConfirmDialogComponent, { width: '360px', data: { title: 'Delete Buddy', html: `[${param.userInfo.name} ${param.userInfo.grade}]를 그룹에서 삭제하시겠습니까?
프로필에서 삭제하면 모든 그룹에서 삭제됩니다.` } }); if (!!result && !!result.choice && result.choice) { this.store.dispatch( SyncStore.delBuddyAndClear({ seq: param.userInfo.seq }) ); this.isBuddy = false; } } } onUploadProfileImage(profileImageFileUploadItem: FileUploadItem) { this.commonApiService .fileProfileSave( { userSeq: this.loginRes.userSeq, deviceType: this.environmentsInfo.deviceType, token: this.loginRes.tokenString, file: profileImageFileUploadItem.file, fileUploadItem: profileImageFileUploadItem }, this.sessionVerinfo.profileUploadUrl ) .pipe( take(1), map(res => { if (!res) { return; } if (StatusCode.Success === res.statusCode) { return res; } else { throw res; } }), finalize(() => { setTimeout(() => { profileImageFileUploadItem.uploadingProgress$ = undefined; }, 1000); }) ) .subscribe( res => { const userInfo = { ...this.loginRes.userInfo, profileImageFile: res.profileSubDir }; this.store.dispatch( AuthenticationStore.updateLoginRes({ loginRes: { ...this.loginRes, userInfo } }) ); this.userInfo = userInfo as any; }, error => { this.snackBarService.open( `프로필 이미지 변경중에 문제가 발생하였습니다.`, '', { duration: 3000, verticalPosition: 'bottom' } ); } ); } }