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

100% Statements 22/22
100% Branches 0/0
100% Functions 7/7
100% Lines 18/18

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 701x 1x   1x   1x                 1x   77x   77x                     77x 77x 77x             77x             77x 77x         77x 308x 308x   308x                   1177x        
import { Component, Inject, Input, LOCALE_ID, OnInit } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/internal/operators';
import { JokeModel } from '../../models';
import { AlertService } 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$: Observable<Array<JokeModel>>;
 
    /**
     * constructor of LastJokesComponent
     * @param alert: AlertService
     * @param afs: AngularFirestore
     * @param locale: LOCALE_ID
     */
    constructor(private readonly alert: AlertService,
                private readonly afs: AngularFirestore,
                @Inject(LOCALE_ID) public locale: string) {
    }
 
    /**
     * ngOnInit
     */
    ngOnInit(): void {
        this.getJokes();
    }
 
    /**
     * get jokes
     */
    getJokes(): void {
        this.jokes$ = this.afs.collection(`jokes_${this.locale}`,
            ref => ref.orderBy('orderNo')
                .limit(this.jokeLimit)
        )
            .snapshotChanges()
            .pipe(map(taxonomyList =>
                taxonomyList.map(taxonomy => {
                    const id = taxonomy.payload.doc.id;
                    const data = taxonomy.payload.doc.data() as JokeModel;
 
                    return { id, ...data };
                })));
    }
 
    /**
     * track content object array by index
     * @param index: index no
     * @param item: object
     */
    trackByIndex(index, item): number {
        return index;
    }
 
}