/
chats.ts
46 lines (40 loc) · 1.12 KB
/
chats.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
import { Component, OnInit } from '@angular/core';
import { Observable } from "rxjs";
import { Chat } from "api/models/whatsapp-models";
import { Chats, Messages } from "api/collections/whatsapp-collections";
import { NavController } from "ionic-angular";
@Component({
templateUrl: 'chats.html'
})
export class ChatsPage implements OnInit {
chats;
constructor(private navCtrl: NavController) {
}
ngOnInit() {
this.chats = Chats
.find({})
.mergeMap((chats: Chat[]) =>
Observable.combineLatest(
...chats.map((chat: Chat) =>
Messages
.find({chatId: chat._id})
.startWith(null)
.map(messages => {
if (messages) chat.lastMessage = messages[0];
return chat;
})
)
)
).zone();
}
showMessages(chat): void {
this.navCtrl.push(MessagesPage, {chat});
}
removeChat(chat: Chat): void {
this.chats = this.chats.map<Chat[]>(chatsArray => {
const chatIndex = chatsArray.indexOf(chat);
chatsArray.splice(chatIndex, 1);
return chatsArray;
});
}
}