import { Component , OnInit , Input } from "@angular/core" ; import { MicropostListService } from "./micropost-list.service" ; import { Micropost , UserStats } from "../../core/domains" ; import { MicropostService } from "../../core/services/micropost.service" ; import { UserService } from "../../core/services/user.service" ; import { HttpErrorHandler } from "../../core/services/http-error-handler" ; import { styles } from "./micropost-list.component.styles" ; @ Component ( { selector : 'mpt-micropost-list' , templateUrl : 'micropost-list.component.html' , providers : [ MicropostListService ] , } ) export class MicropostListComponent implements OnInit { @ Input ( ) userId : string ; styles : any = styles ; posts : Micropost [ ] = [ ] ; noMorePosts : boolean = false ; userStats : UserStats | undefined ; constructor ( private userMicropostService , private micropostService , private userService , private errorHandler ) { } ngOnInit ( ) { this . list ( ) ; this . loadUserStats ( ) ; } delete ( postId ) { if ( ! window . confirm ( 'Are you sure?' ) ) return ; this . micropostService . delete ( postId ) . subscribe ( ( ) => { } , e => this . errorHandler . handle ( e ) , ( ) => this . posts = this . posts . filter ( p => p . id !== postId ) ) ; } loadMore ( ) { const lastPost = this . posts [ this . posts . length - 1 ] ; if ( ! lastPost ) return false ; this . list ( lastPost . id ) ; } private list ( maxId = null ) { this . userMicropostService . list ( this . userId , { maxId : maxId , count : 5 } ) . subscribe ( posts => { this . posts = [ ... this . posts , ... posts ] ; this . noMorePosts = posts . length === 0 ; } , e => this . errorHandler . handle ( e ) ) ; } private loadUserStats ( ) { this . userService . get ( this . userId ) . subscribe ( user => { this . userStats = user . userStats ; } , e => this . errorHandler . handle ( e ) ) ; } }