|
| 1 | +import { Component, Injectable } from '@angular/core'; |
| 2 | +import { Alert, AlertController, NavController, ViewController } from 'ionic-angular'; |
| 3 | +import { PhoneService } from '../../services/phone'; |
| 4 | +import { LoginPage } from '../login/login'; |
| 5 | +import { ProfilePage } from '../profile/profile'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'chats-options', |
| 9 | + templateUrl: 'chats-options.html' |
| 10 | +}) |
| 11 | +@Injectable() |
| 12 | +export class ChatsOptionsComponent { |
| 13 | + constructor( |
| 14 | + private alertCtrl: AlertController, |
| 15 | + private navCtrl: NavController, |
| 16 | + private phoneService: PhoneService, |
| 17 | + private viewCtrl: ViewController |
| 18 | + ) {} |
| 19 | + |
| 20 | + editProfile(): void { |
| 21 | + this.viewCtrl.dismiss().then(() => { |
| 22 | + this.navCtrl.push(ProfilePage); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + logout(): void { |
| 27 | + const alert = this.alertCtrl.create({ |
| 28 | + title: 'Logout', |
| 29 | + message: 'Are you sure you would like to proceed?', |
| 30 | + buttons: [ |
| 31 | + { |
| 32 | + text: 'Cancel', |
| 33 | + role: 'cancel' |
| 34 | + }, |
| 35 | + { |
| 36 | + text: 'Yes', |
| 37 | + handler: () => { |
| 38 | + this.handleLogout(alert); |
| 39 | + return false; |
| 40 | + } |
| 41 | + } |
| 42 | + ] |
| 43 | + }); |
| 44 | + |
| 45 | + this.viewCtrl.dismiss().then(() => { |
| 46 | + alert.present(); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + handleLogout(alert: Alert): void { |
| 51 | + alert.dismiss().then(() => { |
| 52 | + return this.phoneService.logout(); |
| 53 | + }) |
| 54 | + .then(() => { |
| 55 | + this.navCtrl.setRoot(LoginPage, {}, { |
| 56 | + animate: true |
| 57 | + }); |
| 58 | + }) |
| 59 | + .catch((e) => { |
| 60 | + this.handleError(e); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + handleError(e: Error): void { |
| 65 | + console.error(e); |
| 66 | + |
| 67 | + const alert = this.alertCtrl.create({ |
| 68 | + title: 'Oops!', |
| 69 | + message: e.message, |
| 70 | + buttons: ['OK'] |
| 71 | + }); |
| 72 | + |
| 73 | + alert.present(); |
| 74 | + } |
| 75 | +} |
0 commit comments