forked from ng-book/angular2-rxjs-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatNavBar.ts
57 lines (52 loc) · 1.65 KB
/
ChatNavBar.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
import {Component, OnInit} from '@angular/core';
import {MessagesService, ThreadsService} from '../services/services';
import {Message, Thread} from '../models';
import * as _ from 'underscore';
@Component({
selector: 'nav-bar',
template: `
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="https://ng-book.com/2">
<img src="${require('images/logos/ng-book-2-minibook.png')}"/>
ng-book 2
</a>
</div>
<p class="navbar-text navbar-right">
<button class="btn btn-primary" type="button">
Messages <span class="badge">{{unreadMessagesCount}}</span>
</button>
</p>
</div>
</nav>
`
})
export class ChatNavBar implements OnInit {
unreadMessagesCount: number;
constructor(public messagesService: MessagesService,
public threadsService: ThreadsService) {
}
ngOnInit(): void {
this.messagesService.messages
.combineLatest(
this.threadsService.currentThread,
(messages: Message[], currentThread: Thread) =>
[currentThread, messages] )
.subscribe(([currentThread, messages]: [Thread, Message[]]) => {
this.unreadMessagesCount =
_.reduce(
messages,
(sum: number, m: Message) => {
let messageIsInCurrentThread: boolean = m.thread &&
currentThread &&
(currentThread.id === m.thread.id);
if (m && !m.isRead && !messageIsInCurrentThread) {
sum = sum + 1;
}
return sum;
},
0);
});
}
}