From 90614de5d05eb239390477a6f37c7781b08186e8 Mon Sep 17 00:00:00 2001 From: Richard Park Date: Tue, 8 Oct 2019 14:59:22 +0900 Subject: [PATCH] send message is implemented --- .../components/messages.component.html | 2 +- .../components/messages.component.ts | 18 ++++++++--- .../src/app/store/messenger/event/actions.ts | 10 +++++- .../src/app/store/messenger/event/effects.ts | 31 ++++++++++++++++--- .../src/app/store/messenger/event/reducers.ts | 9 +++++- .../src/lib/protocols/send.ts | 2 +- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.html b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.html index ae9b64c6..186debfc 100644 --- a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.html +++ b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.html @@ -40,7 +40,7 @@ diff --git a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts index c162532a..40e99da2 100644 --- a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts +++ b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts @@ -20,7 +20,8 @@ import { map, tap } from 'rxjs/operators'; animations: ucapAnimations }) export class MessagesComponent implements OnInit, OnDestroy { - loginRes$: Observable; + loginRes: LoginResponse; + loginResSubscription: Subscription; eventList$: Observable; roomInfo: RoomInfo; roomInfoSubscription: Subscription; @@ -34,9 +35,14 @@ export class MessagesComponent implements OnInit, OnDestroy { ngOnInit() { const loginInfo = this.sessionStorageService.get(KEY_LOGIN_INFO); - this.loginRes$ = this.store.pipe( - select(AppStore.AccountSelector.AuthenticationSelector.loginRes) - ); + this.loginResSubscription = this.store + .pipe( + select(AppStore.AccountSelector.AuthenticationSelector.loginRes), + tap(loginRes => { + this.loginRes = loginRes; + }) + ) + .subscribe(); this.roomInfoSubscription = this.store .pipe( @@ -53,6 +59,9 @@ export class MessagesComponent implements OnInit, OnDestroy { } ngOnDestroy(): void { + if (!!this.loginResSubscription) { + this.loginResSubscription.unsubscribe(); + } if (!!this.roomInfoSubscription) { this.roomInfoSubscription.unsubscribe(); } @@ -63,6 +72,7 @@ export class MessagesComponent implements OnInit, OnDestroy { onSendMessage(message: string) { this.store.dispatch( EventStore.send({ + senderSeq: this.loginRes.userSeq, req: { roomSeq: this.roomInfo.roomSeq, eventType: EventType.Character, diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts index cf7bd6f0..f3e10511 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/actions.ts @@ -25,14 +25,22 @@ export const infoFailure = createAction( props<{ error: any }>() ); +export const appendInfoList = createAction( + '[Messenger::Event] Append InfoList', + props<{ + info: Info; + }>() +); + export const send = createAction( '[Messenger::Event] Send', - props<{ req: SendRequest }>() + props<{ senderSeq: number; req: SendRequest }>() ); export const sendSuccess = createAction( '[Messenger::Event] Send Success', props<{ + senderSeq: number; res: SendResponse; }>() ); diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts index 8568bf62..fff6aa99 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/effects.ts @@ -26,7 +26,8 @@ import { infoFailure, send, sendSuccess, - sendFailure + sendFailure, + appendInfoList } from './actions'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; @@ -82,11 +83,11 @@ export class Effects { send$ = createEffect(() => this.actions$.pipe( ofType(send), - map(action => action.req), - exhaustMap(req => - this.eventProtocolService.send(req).pipe( + exhaustMap(action => + this.eventProtocolService.send(action.req).pipe( map((res: SendResponse) => { return sendSuccess({ + senderSeq: action.senderSeq, res }); }), @@ -96,6 +97,28 @@ export class Effects { ) ); + sendSuccess$ = createEffect( + () => { + return this.actions$.pipe( + ofType(sendSuccess), + tap(action => { + const res = action.res; + const appendInfo: Info = { + seq: res.seq, + type: res.eventType, + senderSeq: action.senderSeq, + sendDate: res.sendDate, + sentMessage: res.message, + receiverCount: res.receiverCount + }; + + this.store.dispatch(appendInfoList({ info: appendInfo })); + }) + ); + }, + { dispatch: false } + ); + constructor( private actions$: Actions, private store: Store, diff --git a/projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts b/projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts index 10a813bc..06d0e007 100644 --- a/projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts +++ b/projects/ucap-webmessenger-app/src/app/store/messenger/event/reducers.ts @@ -1,6 +1,6 @@ import { createReducer, on } from '@ngrx/store'; import { initialState } from './state'; -import { infoSuccess } from './actions'; +import { infoSuccess, appendInfoList } from './actions'; export const reducer = createReducer( initialState, @@ -10,5 +10,12 @@ export const reducer = createReducer( infoList: action.infoList, infoStatus: action.res }; + }), + + on(appendInfoList, (state, action) => { + return { + ...state, + infoList: [...state.infoList, action.info] + }; }) ); diff --git a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts index b168f28a..b03682ea 100644 --- a/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts +++ b/projects/ucap-webmessenger-protocol-event/src/lib/protocols/send.ts @@ -29,7 +29,7 @@ export interface SendResponse extends ProtocolResponse { // 이벤트타입(s) eventType: EventType; // 발생일시(s) - sendDate: string; + sendDate: Date; // 이벤트내용(s) message: string; // 수신자수