Skip to content

Commit 0b9a420

Browse files
authored
Merge pull request #33 from xpquiz/feature/31
#31 checking for redirect
2 parents a8586d1 + 76708f7 commit 0b9a420

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/app/correct-answer-window/correct-answer-window.component.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {PathsEnum} from "../../model/enums/PathsEnum";
44
import {StorageService} from "../../service/storage.service";
55
import {AppStorage, WeekScore} from "../../model/AppStorage";
66
import * as moment from "moment";
7+
import {Moment} from "moment/moment";
78

89
@Component({
910
selector: 'app-correct-answer-window',
@@ -25,10 +26,29 @@ export class CorrectAnswerWindowComponent implements OnInit {
2526
}
2627

2728
public async ngOnInit(): Promise<void> {
29+
const quizCanBeAnswered: boolean = this.checkIfQuizCanBeAnswered();
30+
31+
if (!quizCanBeAnswered) {
32+
await this.returnHome();
33+
return;
34+
}
35+
2836
await this.correctAnswerSound.play();
2937
this.saveCurrentScore();
3038
}
3139

40+
private checkIfQuizCanBeAnswered(): boolean {
41+
const appStorage: AppStorage = this.storageService.get();
42+
const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate;
43+
44+
if (lastQuizResponseDate === null) return true;
45+
46+
const now: Moment = moment();
47+
const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours");
48+
49+
return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate);
50+
}
51+
3252
public async returnHome(): Promise<void> {
3353
await this.router.navigateByUrl(PathsEnum.HOME);
3454
}

src/app/question-window/question-window.component.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import {TriviaService} from "../../service/trivia.service";
33
import {TriviaResponse} from "../../model/TriviaResponse";
44
import {Router} from "@angular/router";
55
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
}

src/app/wrong-answer-window/wrong-answer-window.component.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {PathsEnum} from "../../model/enums/PathsEnum";
44
import {StorageService} from "../../service/storage.service";
55
import {AppStorage, WeekScore} from "../../model/AppStorage";
66
import * as moment from "moment";
7+
import {Moment} from "moment";
78

89
@Component({
910
selector: 'app-wrong-answer-window',
@@ -24,10 +25,29 @@ export class WrongAnswerWindowComponent implements OnInit {
2425
}
2526

2627
public async ngOnInit(): Promise<void> {
28+
const quizCanBeAnswered: boolean = this.checkIfQuizCanBeAnswered();
29+
30+
if (!quizCanBeAnswered) {
31+
await this.returnHome();
32+
return;
33+
}
34+
2735
await this.wrongAnswerSound.play();
2836
this.saveCurrentScore();
2937
}
3038

39+
private checkIfQuizCanBeAnswered(): boolean {
40+
const appStorage: AppStorage = this.storageService.get();
41+
const lastQuizResponseDate: string | null = appStorage.lastQuizResponseDate;
42+
43+
if (lastQuizResponseDate === null) return true;
44+
45+
const now: Moment = moment();
46+
const nextResponseMinimumDate: Moment = moment(lastQuizResponseDate).add(3, "hours");
47+
48+
return now.isSame(nextResponseMinimumDate) || now.isAfter(nextResponseMinimumDate);
49+
}
50+
3151
public async returnHome(): Promise<void> {
3252
await this.router.navigateByUrl(PathsEnum.HOME);
3353
}

0 commit comments

Comments
 (0)