import { ChangeDetectionStrategy , ChangeDetectorRef , Component , OnDestroy , OnInit } from '@angular/core' ; import { Router } from '@angular/router' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { Subject } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; import { environment } from '@env/environment' ; import { PdfService } from '@app/core/services/pdf/pdf.service' ; import { Logger , Question , QuestionService } from '@app/core' ; const log = new Logger ( 'HomeComponent' ) ; @ Component ( { selector : 'app-home' , templateUrl : './home.component.html' , styleUrls : [ './home.component.scss' ] , changeDetection : ChangeDetectionStrategy . OnPush } ) export class HomeComponent implements OnInit , OnDestroy { private unsubscribe$ : Subject < boolean > = new Subject < boolean > ( ) ; private readonly state ? : { [ k ] : any ; } ; public btnPdf5 : boolean = false ; public btnPdf10 : boolean = false ; public constructor ( private questionService , private pdfService , private snackBar , private router , private cdr ) { this . state = this . router . getCurrentNavigation ( ) . extras . state ; } ngOnInit ( ) { if ( this . state !== undefined ) { this . openSnackBar ( `You answered correctly to ${ this . state . score } questions from ${ environment . configPHP . examSize }` , 'blue-snackbar' ) ; } } ngOnDestroy ( ) { this . unsubscribe$ . next ( ) ; this . unsubscribe$ . complete ( ) ; } public generatePDF ( questionNumber ) { if ( questionNumber === 5 ) { this . btnPdf5 = true ; } if ( questionNumber === 10 ) { this . btnPdf10 = true ; } this . cdr . detectChanges ( ) ; const questionArray = [ ] ; this . questionService . getQuestion ( questionNumber ) . pipe ( takeUntil ( this . unsubscribe$ ) ) . subscribe ( question => questionArray . push ( question ) , error => log . error ( error ) , ( ) => { log . info ( 'PDF test Done!' ) ; if ( questionArray . length === questionNumber ) { this . pdfService . generatePDF ( questionArray ) ; } this . btnPdf5 = this . btnPdf10 = false ; this . cdr . markForCheck ( ) ; } ) ; } private openSnackBar ( data , className , action = 'close' ) { const snack = this . snackBar . open ( data , action , { duration : 5 * 1000 , panelClass : [ className ] } ) ; } }