From c2a728830c91e39bd2c7d92ab5ff3b45ccee390f Mon Sep 17 00:00:00 2001 From: DAB0mB Date: Thu, 9 Feb 2017 00:07:09 -0200 Subject: [PATCH] Step 7.31: Add chat options component --- client/imports/pages/chats/chats-options.ts | 75 +++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 client/imports/pages/chats/chats-options.ts diff --git a/client/imports/pages/chats/chats-options.ts b/client/imports/pages/chats/chats-options.ts new file mode 100644 index 0000000..41612e1 --- /dev/null +++ b/client/imports/pages/chats/chats-options.ts @@ -0,0 +1,75 @@ +import { Component, Injectable } from '@angular/core'; +import { Alert, AlertController, NavController, ViewController } from 'ionic-angular'; +import { PhoneService } from '../../services/phone'; +import { LoginPage } from '../login/login'; +import { ProfilePage } from '../profile/profile'; +import template from './chats-options.html'; + +@Component({ + template +}) +@Injectable() +export class ChatsOptionsComponent { + constructor( + private alertCtrl: AlertController, + private navCtrl: NavController, + private phoneService: PhoneService, + private viewCtrl: ViewController + ) {} + + editProfile(): void { + this.viewCtrl.dismiss().then(() => { + this.navCtrl.push(ProfilePage); + }); + } + + logout(): void { + const alert = this.alertCtrl.create({ + title: 'Logout', + message: 'Are you sure you would like to proceed?', + buttons: [ + { + text: 'Cancel', + role: 'cancel' + }, + { + text: 'Yes', + handler: () => { + this.handleLogout(alert); + return false; + } + } + ] + }); + + this.viewCtrl.dismiss().then(() => { + alert.present(); + }); + } + + handleLogout(alert: Alert): void { + alert.dismiss().then(() => { + return this.phoneService.logout(); + }) + .then(() => { + this.navCtrl.setRoot(LoginPage, {}, { + animate: true + }); + }) + .catch((e) => { + this.handleError(e); + }); + } + + handleError(e: Error): void { + console.error(e); + + const alert = this.alertCtrl.create({ + title: 'Oops!', + message: e.message, + buttons: ['OK'] + }); + + alert.present(); + } +} \ No newline at end of file