All files / src/app/pages/article-list article-list.component.ts

100% Statements 37/37
100% Branches 6/6
100% Functions 10/10
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1171x 1x 1x     1x 1x                 1x             10x                                                 10x 10x 10x 10x 10x 10x             9x 1089x 1089x   9x             1089x 960x   129x 129x         129x 8x 8x 8x                   969x 969x             968x 968x 968x   968x       968x 1888x 1718x 1718x          
import { Component, Inject, LOCALE_ID, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ArticleModel, PageModel, PagerModel } from '../../models';
import { PagerService, PageService, SeoService } from '../../services';
 
/**
 * Article List Component
 */
@Component({
    selector: 'app-article-list',
    templateUrl: './article-list.component.html'
})
export class ArticleListComponent implements OnInit {
    /** current page object */
    page$: Observable<PageModel>;
    /** article object array */
    articles$: Observable<Array<ArticleModel>>;
 
    /** pager model */
    pagerModel: PagerModel = {
        currentPageNo: 1,
        maxPageNo: 0,
        pageSize: 3
    };
 
    /** first article */
    firstItem: ArticleModel;
    /** order no of first article, also it means (count of items * -1) */
    firstItemOrderNo: number;
 
    /** last item of current page */
    lastItemOfCurrentPage: ArticleModel;
    /** last item order no of current page */
    lastItemOrderNoOfCurrentPage: number;
 
    /**
     * constructor of ArticleListComponent
     * @param afs: AngularFirestore
     * @param seo: SeoService
     * @param router: Router
     * @param route: ActivatedRoute
     * @param pagerService: PagerService
     * @param pageService: PageService
     */
    constructor(private readonly afs: AngularFirestore,
                private readonly seo: SeoService,
                public router: Router,
                private readonly route: ActivatedRoute,
                private readonly pagerService: PagerService,
                public pageService: PageService) {
    }
 
    /**
     * ngOnInit
     */
    ngOnInit(): void {
        this.route.paramMap.subscribe(pmap => {
            this.pagerModel.currentPageNo = Number(pmap.get('pageNo'));
            this.initArticles();
        });
        this.page$ = this.pageService.getPageFromFirestore(PageModel, 'pages', this.pageService.getRoutePathName());
    }
 
    /**
     * init articles and get first item
     */
    initArticles(): void {
        if (this.firstItem) { // no need to get firstItem again
            this.getArticles();
        } else {
            this.afs.collection(`articles_${this.pageService.locale}`,
                ref => ref.orderBy('orderNo')
                    .limit(1)
            )
                .valueChanges()
                .subscribe(articles => {
                    if (articles.length > 0) {
                        this.firstItem = articles[0];
                        this.firstItemOrderNo = this.firstItem.orderNo;
                        this.getArticles();
                    }
                });
        }
    }
 
    /**
     * check page no properties
     */
    checkPageNo(): void {
        this.pagerModel.maxPageNo = Math.ceil((this.firstItemOrderNo * -1) / this.pagerModel.pageSize);
        this.pagerService.initPager(this.pagerModel);
    }
 
    /**
     * get articles
     */
    getArticles(): void {
        this.checkPageNo();
        const startAtOrderNo = this.firstItemOrderNo + ((this.pagerModel.currentPageNo - 1) * this.pagerModel.pageSize);
        this.articles$ = this.pageService.getCollectionOfContentFromFirestore(
            `articles_${this.pageService.locale}`,
            ref => ref.orderBy('orderNo')
                .startAt(startAtOrderNo)
                .limit(this.pagerModel.pageSize),
            `${this.pagerModel.pageSize}-${startAtOrderNo}`);
        this.articles$.subscribe(articles => {
            if (articles.length > 0) {
                this.lastItemOfCurrentPage = articles[articles.length - 1];
                this.lastItemOrderNoOfCurrentPage = this.lastItemOfCurrentPage.orderNo;
            }
        });
    }
}