import { Component , AfterContentInit } from '@angular/core' ; import { Alert , AlertController , NavController } from 'ionic-angular' ; import { PhoneService } from '../../services/phone' ; import { VerificationPage } from '../verification/verification' ; import template from './login.html' ; @ Component ( { template } ) export class LoginPage implements AfterContentInit { phone = '' ; constructor ( private alertCtrl , private phoneService , private navCtrl ) { } ngAfterContentInit ( ) { this . phoneService . getNumber ( ) . then ( ( phone ) => { if ( phone ) { this . login ( phone ) ; } } ) ; } onInputKeypress ( { keyCode } ) { if ( keyCode === 13 ) { this . login ( ) ; } } login ( phone = this . phone ) { const alert = this . alertCtrl . create ( { title : 'Confirm' , message : `Would you like to proceed with the phone number ${ phone }?` , buttons : [ { text : 'Cancel' , role : 'cancel' } , { text : 'Yes' , handler : ( ) => { this . handleLogin ( alert ) ; return false ; } } ] } ) ; alert . present ( ) ; } handleLogin ( alert ) { alert . dismiss ( ) . then ( ( ) => { return this . phoneService . verify ( this . phone ) ; } ) . then ( ( ) => { this . navCtrl . push ( VerificationPage , { phone : this . phone } ) ; } ) . catch ( ( e ) => { this . handleError ( e ) ; } ) ; } handleError ( e ) { console . error ( e ) ; const alert = this . alertCtrl . create ( { title : 'Oops!' , message : e . message , buttons : [ 'OK' ] } ) ; alert . present ( ) ; } }