@@ -2,7 +2,11 @@ import {Component, OnInit} from '@angular/core';
22import { TriviaService } from "../../service/trivia.service" ;
33import { TriviaResponse } from "../../model/TriviaResponse" ;
44import { Router } from "@angular/router" ;
5- import { PathsEnum } from "../../model/PathsEnum" ;
5+ import { PathsEnum } from "../../model/enums/PathsEnum" ;
6+ import { AppStorage } from "../../model/AppStorage" ;
7+ import { Moment } from "moment" ;
8+ import * as moment from "moment/moment" ;
9+ import { StorageService } from "../../service/storage.service" ;
610
711@Component ( {
812 selector : 'app-question-window' ,
@@ -27,15 +31,35 @@ export class QuestionWindowComponent implements OnInit {
2731
2832 constructor (
2933 private readonly triviaService : TriviaService ,
30- private readonly router : Router
34+ private readonly router : Router ,
35+ private readonly storageService : StorageService
3136 ) {
3237 }
3338
3439 public async ngOnInit ( ) : Promise < void > {
40+ const quizCanBeAnswered : boolean = this . checkIfQuizCanBeAnswered ( ) ;
41+
42+ if ( ! quizCanBeAnswered ) {
43+ await this . returnHome ( ) ;
44+ return ;
45+ }
46+
3547 this . startLoadingProgressBar ( ) ;
3648 await this . loadQuestion ( ) ;
3749 }
3850
51+ private checkIfQuizCanBeAnswered ( ) : boolean {
52+ const appStorage : AppStorage = this . storageService . get ( ) ;
53+ const lastQuizResponseDate : string | null = appStorage . lastQuizResponseDate ;
54+
55+ if ( lastQuizResponseDate === null ) return true ;
56+
57+ const now : Moment = moment ( ) ;
58+ const nextResponseMinimumDate : Moment = moment ( lastQuizResponseDate ) . add ( 3 , "hours" ) ;
59+
60+ return now . isSame ( nextResponseMinimumDate ) || now . isAfter ( nextResponseMinimumDate ) ;
61+ }
62+
3963 public async onClickAnswer ( selectedAnswer : string ) {
4064 this . selectedAnswer = selectedAnswer ;
4165 await this . confirmAnswerSound . play ( ) ;
@@ -124,4 +148,8 @@ export class QuestionWindowComponent implements OnInit {
124148 }
125149 } ) ;
126150 }
151+
152+ private async returnHome ( ) {
153+ await this . router . navigateByUrl ( PathsEnum . HOME ) ;
154+ }
127155}
0 commit comments