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

100% Statements 45/45
100% Branches 8/8
100% Functions 12/12
100% Lines 43/43

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 117 118 119 120 121 122 123 124 125 126 127 128 129 1301x 1x 1x   1x 1x 1x                   1x             9x                                                   9x 9x 9x 9x 9x 9x 9x             8x 968x 968x   8x             968x 840x   128x 128x         128x 7x 7x 7x                   848x 848x             847x 847x 847x 847x           847x 2289x 2289x 2289x 6x     2289x   847x 847x 766x 766x          
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 { PageModel, PagerModel, QuoteModel } from '../../models';
import { PagerService, PageService, SeoService } from '../../services';
 
/**
 * Quote List Component
 */
@Component({
    selector: 'app-quote-list',
    templateUrl: './quote-list.component.html',
    styleUrls: ['./quote-list.component.scss']
})
export class QuoteListComponent implements OnInit {
    /** current page object */
    page$: Observable<PageModel>;
    /** quote object array */
    quotes$: Observable<Array<QuoteModel>>;
 
    /** pager model */
    pagerModel: PagerModel = {
        currentPageNo: 1,
        maxPageNo: 0,
        pageSize: 3
    };
 
    /** first quote */
    firstItem: QuoteModel;
    /** order no of first quote, also it means (count of items * -1) */
    firstItemOrderNo: number;
 
    /** last item of current page */
    lastItemOfCurrentPage: QuoteModel;
    /** last item order no of current page */
    lastItemOrderNoOfCurrentPage: number;
 
    /**
     * constructor of QuoteListComponent
     * @param afs: AngularFirestore
     * @param seo: SeoService
     * @param router: Router
     * @param route: ActivatedRoute
     * @param pagerService: PagerService
     * @param pageService: PageService
     * @param locale: LOCALE_ID
     */
    constructor(private readonly afs: AngularFirestore,
                private readonly seo: SeoService,
                public router: Router,
                private readonly route: ActivatedRoute,
                private readonly pagerService: PagerService,
                public pageService: PageService,
                @Inject(LOCALE_ID) public locale: string) {
    }
 
    /**
     * ngOnInit
     */
    ngOnInit(): void {
        this.route.paramMap.subscribe(pmap => {
            this.pagerModel.currentPageNo = Number(pmap.get('pageNo'));
            this.initQuotes();
        });
        this.page$ = this.pageService.getPageFromFirestore(PageModel, 'pages', this.pageService.getRoutePathName());
    }
 
    /**
     * init quotes and get first item
     */
    initQuotes(): void {
        if (this.firstItem) { // no need to get firstItem again
            this.getQuotes();
        } else {
            this.afs.collection(`quotes_${this.locale}`,
                ref => ref.orderBy('orderNo')
                    .limit(1)
            )
                .valueChanges()
                .subscribe(quotes => {
                    if (quotes.length > 0) {
                        this.firstItem = quotes[0];
                        this.firstItemOrderNo = this.firstItem.orderNo;
                        this.getQuotes();
                    }
                });
        }
    }
 
    /**
     * check page no properties
     */
    checkPageNo(): void {
        this.pagerModel.maxPageNo = Math.ceil((this.firstItemOrderNo * -1) / this.pagerModel.pageSize);
        this.pagerService.initPager(this.pagerModel);
    }
 
    /**
     * get quotes
     */
    getQuotes(): void {
        this.checkPageNo();
        const startAtOrderNo = this.firstItemOrderNo + ((this.pagerModel.currentPageNo - 1) * this.pagerModel.pageSize);
        this.quotes$ = this.afs.collection(`quotes_${this.locale}`,
            ref => ref.orderBy('orderNo')
                .startAt(startAtOrderNo)
                .limit(this.pagerModel.pageSize)
        )
            .snapshotChanges()
            .pipe(map(actions =>
                actions.map(action => {
                    const id = action.payload.doc.id;
                    const data = action.payload.doc.data() as QuoteModel;
                    if (!data.hasOwnProperty('contentSummary')) {
                        data.contentSummary = data.content;
                    }
 
                    return { id, ...data };
                })));
        this.quotes$.subscribe(quotes => {
            if (quotes.length > 0) {
                this.lastItemOfCurrentPage = quotes[quotes.length - 1];
                this.lastItemOrderNoOfCurrentPage = this.lastItemOfCurrentPage.orderNo;
            }
        });
    }
}