-
Notifications
You must be signed in to change notification settings - Fork 87
/
chat-window.component.ts
66 lines (59 loc) · 1.58 KB
/
chat-window.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {
Component,
Inject,
ElementRef
} from '@angular/core';
import * as Redux from 'redux';
import { AppStore } from '../app.store';
import { User } from '../user/user.model';
import { Thread } from '../thread/thread.model';
import * as ThreadActions from '../thread/thread.actions';
import {
AppState,
getCurrentThread,
getCurrentUser
} from '../app.reducer';
@Component({
selector: 'chat-window',
templateUrl: './chat-window.component.html',
styleUrls: ['./chat-window.component.css']
})
export class ChatWindowComponent {
currentThread: Thread;
draftMessage: { text: string };
currentUser: User;
constructor(@Inject(AppStore) private store: Redux.Store<AppState>,
private el: ElementRef) {
store.subscribe(() => this.updateState() );
this.updateState();
this.draftMessage = { text: '' };
}
updateState() {
const state = this.store.getState();
this.currentThread = getCurrentThread(state);
this.currentUser = getCurrentUser(state);
this.scrollToBottom();
}
scrollToBottom(): void {
const scrollPane: any = this.el
.nativeElement.querySelector('.msg-container-base');
if (scrollPane) {
setTimeout(() => scrollPane.scrollTop = scrollPane.scrollHeight);
}
}
sendMessage(): void {
this.store.dispatch(ThreadActions.addMessage(
this.currentThread,
{
author: this.currentUser,
isRead: true,
text: this.draftMessage.text
}
));
this.draftMessage = { text: '' };
}
onEnter(event: any): void {
this.sendMessage();
event.preventDefault();
}
}