forked from ng-book/angular2-rxjs-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatNavBar.ts
62 lines (56 loc) · 1.76 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
57
58
59
60
61
/// <reference path="../../typings/app.d.ts" />
import {Component, View, LifecycleEvent} from "angular2/angular2";
import {MessagesService, ThreadsService} from "../services/services";
import {Message, Thread} from "../models";
import * as _ from "underscore";
@Component({
lifecycle: [ LifecycleEvent.onInit ],
selector: "nav-bar"
})
@View({
directives: [],
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")}"></img> 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 {
unreadMessagesCount: number;
constructor(public messagesService: MessagesService,
public threadsService: ThreadsService) {
}
onInit(): 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);
});
}
}