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