From 76708f7b727222e6217797de8c77e08868440e75 Mon Sep 17 00:00:00 2001 From: Isahann Hanacleto Date: Tue, 8 Aug 2023 19:02:01 -0300 Subject: [PATCH] #31 checking for redirect --- .../correct-answer-window.component.ts | 20 +++++++++++++ .../question-window.component.ts | 30 ++++++++++++++++++- .../wrong-answer-window.component.ts | 20 +++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/app/correct-answer-window/correct-answer-window.component.ts b/src/app/correct-answer-window/correct-answer-window.component.ts index 0c2ab49..73d1fc4 100644 --- a/src/app/correct-answer-window/correct-answer-window.component.ts +++ b/src/app/correct-answer-window/correct-answer-window.component.ts @@ -4,6 +4,7 @@ import {PathsEnum} from "../../model/enums/PathsEnum"; import {StorageService} from "../../service/storage.service"; import {AppStorage, WeekScore} from "../../model/AppStorage"; import * as moment from "moment"; +import {Moment} from "moment/moment"; @Component({ selector: 'app-correct-answer-window', @@ -25,10 +26,29 @@ export class CorrectAnswerWindowComponent implements OnInit { } public async ngOnInit(): Promise { + const quizCanBeAnswered: boolean = this.checkIfQuizCanBeAnswered(); + + if (!quizCanBeAnswered) { + await this.returnHome(); + return; + } + await this.correctAnswerSound.play(); this.saveCurrentScore(); } + private checkIfQuizCanBeAnswered(): boolean { + const appStorage: AppStorage = this.storageService.get(); + const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate; + + if (lastQuizResponseDate === null) return true; + + const now: Moment = moment(); + const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours"); + + return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate); + } + public async returnHome(): Promise { await this.router.navigateByUrl(PathsEnum.HOME); } diff --git a/src/app/question-window/question-window.component.ts b/src/app/question-window/question-window.component.ts index eb6e66f..a693658 100644 --- a/src/app/question-window/question-window.component.ts +++ b/src/app/question-window/question-window.component.ts @@ -3,6 +3,10 @@ import {TriviaService} from "../../service/trivia.service"; import {TriviaResponse} from "../../model/TriviaResponse"; import {Router} from "@angular/router"; import {PathsEnum} from "../../model/enums/PathsEnum"; +import {AppStorage} from "../../model/AppStorage"; +import {Moment} from "moment"; +import * as moment from "moment/moment"; +import {StorageService} from "../../service/storage.service"; @Component({ selector: 'app-question-window', @@ -27,15 +31,35 @@ export class QuestionWindowComponent implements OnInit { constructor( private readonly triviaService: TriviaService, - private readonly router: Router + private readonly router: Router, + private readonly storageService: StorageService ) { } public async ngOnInit(): Promise { + const quizCanBeAnswered: boolean = this.checkIfQuizCanBeAnswered(); + + if (!quizCanBeAnswered) { + await this.returnHome(); + return; + } + this.startLoadingProgressBar(); await this.loadQuestion(); } + private checkIfQuizCanBeAnswered(): boolean { + const appStorage: AppStorage = this.storageService.get(); + const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate; + + if (lastQuizResponseDate === null) return true; + + const now: Moment = moment(); + const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours"); + + return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate); + } + public async onClickAnswer(selectedAnswer: string) { this.selectedAnswer = selectedAnswer; await this.confirmAnswerSound.play(); @@ -124,4 +148,8 @@ export class QuestionWindowComponent implements OnInit { } }); } + + private async returnHome() { + await this.router.navigateByUrl(PathsEnum.HOME); + } } diff --git a/src/app/wrong-answer-window/wrong-answer-window.component.ts b/src/app/wrong-answer-window/wrong-answer-window.component.ts index dec1e62..a003a61 100644 --- a/src/app/wrong-answer-window/wrong-answer-window.component.ts +++ b/src/app/wrong-answer-window/wrong-answer-window.component.ts @@ -4,6 +4,7 @@ import {PathsEnum} from "../../model/enums/PathsEnum"; import {StorageService} from "../../service/storage.service"; import {AppStorage, WeekScore} from "../../model/AppStorage"; import * as moment from "moment"; +import {Moment} from "moment"; @Component({ selector: 'app-wrong-answer-window', @@ -24,10 +25,29 @@ export class WrongAnswerWindowComponent implements OnInit { } public async ngOnInit(): Promise { + const quizCanBeAnswered: boolean = this.checkIfQuizCanBeAnswered(); + + if (!quizCanBeAnswered) { + await this.returnHome(); + return; + } + await this.wrongAnswerSound.play(); this.saveCurrentScore(); } + private checkIfQuizCanBeAnswered(): boolean { + const appStorage: AppStorage = this.storageService.get(); + const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate; + + if (lastQuizResponseDate === null) return true; + + const now: Moment = moment(); + const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours"); + + return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate); + } + public async returnHome(): Promise { await this.router.navigateByUrl(PathsEnum.HOME); }