Skip to content
Permalink
Browse files

Step 7.17: Subscribe to 'chats'

  • Loading branch information...
DAB0mB committed Nov 10, 2016
1 parent c513d22 commit 2d4c77763b52948677872bfbda63387c94279d4a
Showing with 38 additions and 28 deletions.
  1. +38 −28 src/pages/chats/chats.ts
@@ -6,6 +6,7 @@ import { NavController, PopoverController, ModalController, AlertController } fr
import { MessagesPage } from "../messages/messages";
import { ChatsOptionsComponent } from "../chat-options/chat-options";
import { NewChatComponent } from "../new-chat/new-chat";
import { MeteorObservable } from 'meteor-rxjs';

@Component({
templateUrl: 'chats.html'
@@ -24,34 +25,43 @@ export class ChatsPage implements OnInit {
ngOnInit() {
this.senderId = Meteor.userId();

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;
})
)
)
).map(chats => {
chats.forEach(chat => {
chat.title = '';
chat.picture = '';

const receiver = Users.findOne(chat.memberIds.find(memberId => memberId !== this.senderId));
if (!receiver) return;

chat.title = receiver.profile.name;
chat.picture = receiver.profile.picture;
});

return chats;
}).zone();
MeteorObservable.subscribe('chats').subscribe(() => {
MeteorObservable.autorun().subscribe(() => {
if (this.chats) {
this.chats.unsubscribe();
this.chats = undefined;
}

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;
})
)
)
).map(chats => {
chats.forEach(chat => {
chat.title = '';
chat.picture = '';

const receiver = Users.findOne(chat.memberIds.find(memberId => memberId !== this.senderId));
if (!receiver) return;

chat.title = receiver.profile.name;
chat.picture = receiver.profile.picture;
});

return chats;
}).zone();
});
});
}

addChat(): void {

0 comments on commit 2d4c777

Please sign in to comment.
You can’t perform that action at this time.