Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Step 6.6: Add new chat controller
  • Loading branch information
DAB0mB authored and Dotan Simha committed Nov 22, 2016
1 parent cdf2737 commit 66f9416
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/controllers/new-chat.controller.js
@@ -0,0 +1,52 @@
import { Meteor } from 'meteor/meteor';
import { Chats } from 'api/collections';
import { Controller } from 'angular-ecmascript/module-helpers';

export default class NewChatCtrl extends Controller {
static $inject = ['$state', 'NewChat', '$ionicPopup', '$log']

constructor() {
super(...arguments);

this.helpers({
users() {
return Meteor.users.find({ _id: { $ne: this.currentUserId } });
}
});
}

newChat(userId) {
let chat = Chats.findOne({ userIds: { $all: [this.currentUserId, userId] } });

if (chat) {
this.hideNewChatModal();
return this.goToChat(chat._id);
}

this.callMethod('newChat', userId, (err, chatId) => {
this.hideNewChatModal();
if (err) return this.handleError(err);
this.goToChat(chatId);
});
}

hideNewChatModal() {
this.NewChat.hideModal();
}

goToChat(chatId) {
this.$state.go('tab.chat', { chatId });
}

handleError(err) {
this.$log.error('New chat creation error ', err);

this.$ionicPopup.alert({
title: err.reason || 'New chat creation failed',
template: 'Please try again',
okType: 'button-positive button-clear'
});
}
}

NewChatCtrl.$name = 'NewChatCtrl';

0 comments on commit 66f9416

Please sign in to comment.