forked from ng-book/angular2-rxjs-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-nav-bar.component.ts
51 lines (46 loc) · 1.59 KB
/
chat-nav-bar.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
import {
Component,
OnInit
} from '@angular/core';
import * as _ from 'lodash';
import { combineLatest } from 'rxjs/operators';
import { ThreadsService } from './../thread/threads.service';
import { MessagesService } from './../message/messages.service';
import { Thread } from './../thread/thread.model';
import { Message } from './../message/message.model';
@Component({
selector: 'chat-nav-bar',
templateUrl: './chat-nav-bar.component.html',
styleUrls: ['./chat-nav-bar.component.css']
})
export class ChatNavBarComponent implements OnInit {
unreadMessagesCount: number;
constructor(public messagesService: MessagesService,
public threadsService: ThreadsService) {
}
ngOnInit(): void {
this.messagesService.messages.pipe(
combineLatest(
this.threadsService.currentThread,
(messages: Message[], currentThread: Thread) =>
[currentThread, messages] ))
.subscribe(([currentThread, messages]: [Thread, Message[]]) => {
this.unreadMessagesCount =
_.reduce(
messages,
(sum: number, m: Message) => {
const messageIsInCurrentThread: boolean = m.thread &&
currentThread &&
(currentThread.id === m.thread.id);
// note: in a "real" app you should also exclude
// messages that were authored by the current user b/c they've
// already been "read"
if (m && !m.isRead && !messageIsInCurrentThread) {
sum = sum + 1;
}
return sum;
},
0);
});
}
}