All files / src/app/widgets/last-jokes last-jokes.component.ts

100% Statements 21/21
100% Branches 0/0
100% Functions 8/8
100% Lines 19/19

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 721x 1x 1x   1x                 1x   78x   78x                     78x 78x 78x             78x             78x 78x         78x 312x 312x   312x     78x                   1181x        
import { Component, Input, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/internal/operators';
import { JokeModel } from '../../models';
import { AlertService, PageService } from '../../services';
 
/**
 * Last Jokes Component
 */
@Component({
    selector: 'app-last-jokes',
    templateUrl: './last-jokes.component.html'
})
export class LastJokesComponent implements OnInit {
    /** limit number of jokes to show */
    @Input() readonly jokeLimit = 10;
    /** css class of header */
    @Input() readonly headerCssClass = '';
 
    /** joke object array */
    jokes: Array<JokeModel>;
 
    /**
     * constructor of LastJokesComponent
     * @param alert: AlertService
     * @param pageService: PageService
     * @param afs: AngularFirestore
     */
    constructor(private readonly alert: AlertService,
                public pageService: PageService,
                private readonly afs: AngularFirestore) {
    }
 
    /**
     * ngOnInit
     */
    ngOnInit(): void {
        this.getJokes();
    }
 
    /**
     * get jokes
     */
    getJokes(): void {
        this.afs.collection(`jokes_${this.pageService.locale}`,
            ref => ref.orderBy('orderNo')
                .limit(this.jokeLimit)
        )
            .snapshotChanges()
            .pipe(map(jokeList =>
                jokeList.map(joke => {
                    const id = joke.payload.doc.id;
                    const data = joke.payload.doc.data() as JokeModel;
 
                    return { id, ...data };
                })))
            .subscribe(jokes => {
                this.jokes = jokes;
            });
    }
 
    /**
     * track content object array by index
     * @param index: index no
     * @param item: object
     */
    trackByIndex(index, item): number {
        return index;
    }
 
}