import { Component , OnDestroy } from '@angular/core' ; import { Store } from "@ngrx/store" ; import { State } from "./../../store" ; import * as actionsAccount from './../../store/actions/account' ; import * as selectors from './../../store/selectors' ; import { NavController , ViewController , NavParams , AlertController } from "ionic-angular" ; import { SignUpPage } from './../signup/signup' ; import * as _ from "lodash" ; import { Subscription } from "rxjs/Subscription" ; import { AsyncOperation } from "./../../store/utils" ; @ Component ( { selector : "page-login" , templateUrl : "login.html" } ) export class LoginPage implements OnDestroy { public creds : { email ? : string ; password ? : string ; } ; public loginOp : AsyncOperation ; private isModal : boolean = false ; private subscription : Subscription = new Subscription ; constructor ( private store , private viewCtrl , private navCtrl , private alertCtrl , private navParams ) { this . isModal = navParams . get ( 'modal' ) ; this . creds = { } ; this . subscription . add ( this . store . select ( selectors . getAccountLoginOp ) . subscribe ( loginOp => this . loginOp = loginOp ) ) ; } ngOnDestroy ( ) { this . subscription . unsubscribe ( ) ; } cancel ( ) { this . dismiss ( ) ; } private dismiss ( ) { if ( this . isModal ) { this . navCtrl . parent . getActive ( ) . dismiss ( ) ; } } facebook ( ) { if ( this . loginOp . pending ) return ; this . alertCtrl . create ( { title : "Not Supported" , message : "Signing in via Facebook is not supported" , buttons : [ "Dismiss" ] , } ) . present ( ) ; } canLogin ( ) { return _ . isEmpty ( this . creds . email ) == false && _ . isEmpty ( this . creds . password ) == false ; } async fakeLogin ( ) { console . log ( "fake login" ) ; if ( this . loginOp . pending ) return ; let onLoginOpChange = this . store . select ( selectors . getAccountLoginOp ) . skip ( 1 ) . filter ( op => op . fail ) . first ( ) . toPromise ( ) ; this . store . dispatch ( new actionsAccount . LoginAction ( { loginMethod : actionsAccount . LoginMethod . Email , email : this . creds . email , password : this . creds . password , fake : true , } ) ) ; let loginOp = await onLoginOpChange ; this . alertCtrl . create ( { title : "Login Failed" , message : loginOp . message , buttons : [ "Dismiss" ] , } ) . present ( ) ; } async login ( ) { if ( this . loginOp . pending ) return ; let onLoginOpChange = this . store . select ( selectors . getAccountLoginOp ) . skip ( 1 ) . filter ( op => op . fail ) . first ( ) . toPromise ( ) ; this . store . dispatch ( new actionsAccount . LoginAction ( { loginMethod : actionsAccount . LoginMethod . Email , email : this . creds . email , password : this . creds . password , } ) ) ; let loginOp = await onLoginOpChange ; this . alertCtrl . create ( { title : "Login Failed" , message : loginOp . message , buttons : [ "Dismiss" ] , } ) . present ( ) ; } signup ( ) { this . navCtrl . push ( SignUpPage ) ; } }