11import { Component , OnDestroy , OnInit } from '@angular/core' ;
22import { TriviaService } from "../../service/trivia.service" ;
3- import { TriviaResponse } from "../../model/TriviaResponse" ;
43import { Router } from "@angular/router" ;
54import { PathsEnum } from "../../model/enums/PathsEnum" ;
65import { AppStorageService } from "../../service/app-storage.service" ;
76import { QuestionResultTemplateParams } from "../../model/Template" ;
87import { EncryptionService } from "../../service/encryption.service" ;
98import { Subscription } from "rxjs" ;
9+ import { Question } from "../../model/questions/Question" ;
1010
1111@Component ( {
1212 selector : 'app-question-window' ,
@@ -46,7 +46,10 @@ export class QuestionWindowComponent implements OnInit, OnDestroy {
4646 }
4747
4848 this . startLoadingProgressBar ( ) ;
49- this . loadQuestion ( ) ;
49+
50+ while ( ! this . questionLoaded ) {
51+ await this . loadQuestion ( ) ;
52+ }
5053 }
5154
5255 public ngOnDestroy ( ) {
@@ -64,20 +67,23 @@ export class QuestionWindowComponent implements OnInit, OnDestroy {
6467 return selectedAnswer ? `> ${ answer } <` : answer ;
6568 }
6669
67- private loadQuestion ( ) : void {
68- this . getQuizzesSubscription = this . triviaService . getQuizzes ( ) . subscribe ( ( response : TriviaResponse [ ] ) : void => {
69- const singleQuiz : TriviaResponse = response [ 0 ] ;
70+ private async loadQuestion ( ) : Promise < void > {
71+ const questions : Question [ ] = await this . triviaService . fetchQuestion ( )
72+
73+ if ( questions . length === 0 )
74+ return ;
75+
76+ const singleQuiz : Question = questions [ 0 ] ;
7077
71- this . question = singleQuiz . question . text ;
72- this . correctAnswer = singleQuiz . correctAnswer ;
73- this . answers = [ singleQuiz . correctAnswer , ...singleQuiz . incorrectAnswers ]
74- . map ( ( value ) => ( { value, sort : Math . random ( ) } ) )
75- . sort ( ( a , b ) => a . sort - b . sort )
76- . map ( ( { value} ) => value ) ;
78+ this . question = singleQuiz . question ;
79+ this . correctAnswer = singleQuiz . correctAnswer ;
80+ this . answers = [ singleQuiz . correctAnswer , ...singleQuiz . incorrectAnswers ]
81+ . map ( ( value ) => ( { value, sort : Math . random ( ) } ) )
82+ . sort ( ( a , b ) => a . sort - b . sort )
83+ . map ( ( { value} ) => value ) ;
7784
78- this . questionPoints = this . sumQuestionPoints ( singleQuiz . difficulty , singleQuiz . isNiche ) ;
79- this . questionLoaded = true ;
80- } ) ;
85+ this . questionPoints = this . sumQuestionPoints ( singleQuiz . difficulty , singleQuiz . isNiche ) ;
86+ this . questionLoaded = true ;
8187 }
8288
8389 public async confirmAnswer ( ) : Promise < void > {
@@ -130,15 +136,15 @@ export class QuestionWindowComponent implements OnInit, OnDestroy {
130136 let revertProgressBar : boolean = false ;
131137
132138 while ( true ) {
133- if ( this . loadingProgressBar === 100 ) {
139+ if ( this . loadingProgressBar === 100 ) {
134140 revertProgressBar = true ;
135141
136- if ( this . questionLoaded ) {
142+ if ( this . questionLoaded ) {
137143 this . showQuestion = true ;
138144 await this . questionReadySound . play ( ) ;
139145 break ;
140146 }
141- } else if ( this . loadingProgressBar === 0 ) {
147+ } else if ( this . loadingProgressBar === 0 ) {
142148 revertProgressBar = false ;
143149 }
144150
0 commit comments