import { Component } from '@angular/core' ; import { NavParams , NavController , LoadingController , AlertController } from 'ionic-angular' ; import { LoginPage } from '../login/login' ; import { HomePage } from '../home/home' ; import { WordpressService } from '../../services/wordpress.service' ; import { AuthenticationService } from '../../services/authentication.service' ; import { Observable } from "rxjs/Observable" ; import 'rxjs/add/operator/map' ; import 'rxjs/add/observable/forkJoin' ; @ Component ( { selector : 'page-post' , templateUrl : 'post.html' } ) export class PostPage { post : any ; user : string ; comments : Array < any > = new Array < any > ( ) ; categories : Array < any > = new Array < any > ( ) ; morePagesAvailable : boolean = true ; constructor ( public navParams , public navCtrl , public loadingCtrl , public alertCtrl , public wordpressService , public authenticationService ) { } ionViewWillEnter ( ) { this . morePagesAvailable = true ; let loading = this . loadingCtrl . create ( ) ; loading . present ( ) ; this . post = this . navParams . get ( 'item' ) ; Observable . forkJoin ( this . getAuthorData ( ) , this . getCategories ( ) , this . getComments ( ) ) . subscribe ( data => { this . user = data [ 0 ] . name ; this . categories = data [ 1 ] ; this . comments = data [ 2 ] ; loading . dismiss ( ) ; } ) ; } getAuthorData ( ) { return this . wordpressService . getAuthor ( this . post . author ) ; } getCategories ( ) { return this . wordpressService . getPostCategories ( this . post ) ; } getComments ( ) { return this . wordpressService . getComments ( this . post . id ) ; } loadMoreComments ( infiniteScroll ) { let page = ( this . comments . length / 10 ) + 1 ; this . wordpressService . getComments ( this . post . id , page ) . subscribe ( data => { for ( let item of data ) { this . comments . push ( item ) ; } infiniteScroll . complete ( ) ; } , err => { console . log ( err ) ; this . morePagesAvailable = false ; } ) ; } goToCategoryPosts ( categoryId , categoryTitle ) { this . navCtrl . push ( HomePage , { id : categoryId , title : categoryTitle } ) ; } createComment ( ) { let user ; this . authenticationService . getUser ( ) . then ( res => { user = res ; let alert = this . alertCtrl . create ( { title : 'Add a comment' , inputs : [ { name : 'comment' , placeholder : 'Comment' } ] , buttons : [ { text : 'Cancel' , role : 'cancel' , handler : data => { console . log ( 'Cancel clicked' ) ; } } , { text : 'Accept' , handler : data => { let loading = this . loadingCtrl . create ( ) ; loading . present ( ) ; this . wordpressService . createComment ( this . post . id , user , data . comment ) . subscribe ( ( data ) => { console . log ( "ok" , data ) ; this . getComments ( ) ; loading . dismiss ( ) ; } , ( err ) => { console . log ( "err" , err ) ; loading . dismiss ( ) ; } ) ; } } ] } ) ; alert . present ( ) ; } , err => { let alert = this . alertCtrl . create ( { title : 'Please login' , message : 'You need to login in order to comment' , buttons : [ { text : 'Cancel' , role : 'cancel' , handler : ( ) => { console . log ( 'Cancel clicked' ) ; } } , { text : 'Login' , handler : ( ) => { this . navCtrl . push ( LoginPage ) ; } } ] } ) ; alert . present ( ) ; } ) ; } }