import { Component , Input } from '@angular/core' ; import { Article , ArticleListConfig , ArticlesService } from '../../core' ; @ Component ( { selector : 'app-article-list' , styleUrls : [ 'article-list.component.css' ] , templateUrl : './article-list.component.html' } ) export class ArticleListComponent { constructor ( private articlesService ) { } @ Input ( ) limit : number ; @ Input ( ) set config ( config ) { if ( config ) { this . query = config ; this . currentPage = 1 ; this . runQuery ( ) ; } } query : ArticleListConfig ; results : Article [ ] ; loading = false ; currentPage = 1 ; totalPages : Array < number > = [ 1 ] ; setPageTo ( pageNumber ) { this . currentPage = pageNumber ; this . runQuery ( ) ; } runQuery ( ) { this . loading = true ; this . results = [ ] ; if ( this . limit ) { this . query . filters . limit = this . limit ; this . query . filters . offset = ( this . limit * ( this . currentPage - 1 ) ) ; } this . articlesService . query ( this . query ) . subscribe ( data => { this . loading = false ; this . results = data . articles ; this . totalPages = Array . from ( new Array ( Math . ceil ( data . articlesCount / this . limit ) ) , ( val , index ) => index + 1 ) ; } ) ; } }