Skip to content

Commit

Permalink
Step 6.14: Implement sendTextMessage method
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB committed Feb 13, 2017
1 parent adc40ff commit 9c3be2d
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions client/imports/pages/messages/messages.ts
@@ -1,18 +1,23 @@
import { Component, OnInit } from '@angular/core';
import { NavParams } from 'ionic-angular';
import { MeteorObservable } from 'meteor-rxjs';
import { Observable } from 'rxjs';
import { Messages } from '../../../../imports/collections';
import { Chat, Message } from '../../../../imports/models';
import { Chat, Message, MessageType } from '../../../../imports/models';
import template from './messages.html';

@Component({
template
})
export class MessagesPage implements OnInit {
selectedChat: Chat;
title: string;
picture: string;
messages: Observable<Message[]>;
message: string = '';

constructor(navParams: NavParams) {
this.selectedChat = <Chat>navParams.get('chat');

this.title = this.selectedChat.title;
this.picture = this.selectedChat.picture;
}
Expand All @@ -32,4 +37,25 @@ export class MessagesPage implements OnInit {
return messages;
});
}

onInputKeypress({ keyCode }: KeyboardEvent): void {
if (keyCode === 13) {
this.sendTextMessage();
}
}

sendTextMessage(): void {
// If message was yet to be typed, abort
if (!this.message) {
return;
}

MeteorObservable.call('addMessage', MessageType.TEXT,
this.selectedChat._id,
this.message
).zone().subscribe(() => {
// Zero the input field
this.message = '';
});
}
}

0 comments on commit 9c3be2d

Please sign in to comment.