Skip to content

Commit

Permalink
Step 7.16: Subscribe to 'chats'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored and DAB0mB committed Dec 17, 2016
1 parent c92be61 commit 18999f7
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions client/imports/pages/chats/chats.component.ts
Expand Up @@ -2,6 +2,7 @@ import {Component, OnInit} from "@angular/core";
import template from "./chats.component.html"
import {Observable} from "rxjs";
import {Meteor} from 'meteor/meteor';
import {MeteorObservable} from 'meteor-rxjs';
import {Chat} from "../../../../both/models/chat.model";
import * as moment from "moment";
import style from "./chats.component.scss";
Expand Down Expand Up @@ -32,31 +33,36 @@ export class ChatsComponent implements OnInit {

ngOnInit() {
this.senderId = Meteor.userId();
this.chats = Chats
.find({})
.mergeMap<Chat[]>(chats =>
Observable.combineLatest(
...chats.map(chat =>

Messages.find({ chatId: chat._id }, { sort: { createdAt: -1 }, limit: 1 })
.startWith(null)
.map(messages => {
if (messages) chat.lastMessage = messages[0];
return chat;
})
MeteorObservable.subscribe('chats').subscribe(() => {
MeteorObservable.autorun().subscribe(() => {
this.chats = Chats
.find({})
.mergeMap<Chat[]>(chats =>
Observable.combineLatest(
...chats.map(chat =>

)
)
).map(chats => {
chats.forEach(chat => {
const receiver = Meteor.users.findOne(chat.memberIds.find(memberId => memberId !== this.senderId))
Messages.find({ chatId: chat._id }, { sort: { createdAt: -1 }, limit: 1 })
.startWith(null)
.map(messages => {
if (messages) chat.lastMessage = messages[0];
return chat;
})

chat.title = receiver.profile.name;
chat.picture = receiver.profile.picture;
});
)
)
).map(chats => {
chats.forEach(chat => {
const receiver = Meteor.users.findOne(chat.memberIds.find(memberId => memberId !== this.senderId))

return chats;
}).zone();
chat.title = receiver.profile.name;
chat.picture = receiver.profile.picture;
});

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

addChat(): void {
Expand Down

0 comments on commit 18999f7

Please sign in to comment.