Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit ab4021f

Browse files
committed
fixes up quiz routing
1 parent 8effb5c commit ab4021f

File tree

7 files changed

+142
-166
lines changed

7 files changed

+142
-166
lines changed

src/api/quiz.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ async function submit(answer) {
2323
return result.correct;
2424
}
2525

26-
async function submitFinal(answer, language) {
26+
async function submitFinal(answer, language, checkOnly) {
2727
const result = await request(routes.questionsapi_answer_final_question, {
2828
data: {
29+
checkOnly,
2930
text: answer,
3031
language
3132
}

src/plugins/router.js

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from "vue";
22
import VueRouter from "vue-router";
3-
import { auth } from "@/api";
3+
import { auth, quiz } from "@/api";
44
import store from "@/store";
55

66
Vue.use(VueRouter);
@@ -61,95 +61,53 @@ const routes = [
6161
{
6262
path: "/quiz",
6363
name: "quiz",
64-
component: () => import("@/views/Quiz/Quiz"),
65-
meta: {
66-
secured: true
67-
},
68-
beforeEnter: async (to, from, next) => {
64+
component: async () => {
6965
await store.dispatch("Quiz/refresh");
7066

67+
// CHALLENGE IS OVER
7168
if (store.state.Quiz.quizHasEnded) {
72-
next({ name: 'quiz-finished' });
73-
return;
69+
return import("@/views/Quiz/QuizFinished");
7470
}
7571

72+
// CHALLENGE HAS NOT STARTED
7673
if (!store.state.Quiz.quizHasStarted) {
77-
// quiz has not started
78-
next({ name: 'quiz-countdown' });
79-
return;
74+
return import("@/views/Quiz/QuizCountdown");
8075
}
8176

82-
if (store.state.Quiz.awaitNextQuestion) {
83-
// next question is not unlocked
84-
next({ name: 'quiz-countdown' });
85-
return;
77+
// USER HAS FINISHED QUIZ
78+
if (store.state.Quiz.maxRank === store.state.User.rank - 1) {
79+
return import("@/views/Quiz/QuizFinished");
8680
}
8781

88-
if (!store.state.Quiz.hasSeenIntro && store.state.User.rank == 0) {
89-
// user probably should see the intro video
90-
next({ name: "quiz-intro" });
91-
return;
82+
// MUST WAIT FOR NEXT QUESTION
83+
if (store.state.Quiz.awaitNextQuestion) {
84+
return import("@/views/Quiz/QuizCountdown");
9285
}
9386

87+
88+
// SHOW THE LAST QUESTION
9489
if (store.state.Quiz.isLastQuestion) {
95-
next({ name: 'quiz-final' });
96-
return;
90+
return import("@/views/Quiz/QuizFinalQuestion");
9791
}
9892

99-
// user is okay to take quiz
100-
next();
101-
}
102-
},
103-
{
104-
path: "/quiz/countdown",
105-
name: "quiz-countdown",
106-
component: () => import("@/views/Quiz/QuizCountdown"),
107-
meta: {
108-
secured: true
93+
// NORMAL QUIZ MODE
94+
return import("@/views/Quiz/Quiz");
10995
},
110-
beforeEnter: async (to, from, next) => {
111-
await store.dispatch("Quiz/refresh");
112-
113-
if ((store.state.Quiz.quizHasEnded || store.state.Quiz.quizHasStarted) && !store.state.Quiz.awaitNextQuestion) {
114-
// quiz has not started
115-
next({ name: 'quiz' });
116-
return;
96+
beforeEnter(from, to, next) {
97+
// USER MUST SEE INTRO VIDEO
98+
if (!store.state.Quiz.hasSeenIntro && store.state.User.rank == 1) {
99+
next({ name: 'quiz-intro' });
117100
}
118-
119101
next();
120-
}
121-
},
122-
{
123-
path: "/quiz/finished",
124-
name: "quiz-finished",
125-
component: () => import("@/views/Quiz/QuizFinished")
126-
},
127-
{
128-
path: "/quiz/final",
129-
name: "quiz-final",
130-
component: () => import("@/views/Quiz/QuizFinalQuestion"),
102+
},
131103
meta: {
132104
secured: true
133105
},
134-
beforeEnter: async (to, from, next) => {
135-
await store.dispatch("Quiz/refresh");
136-
137-
if (!store.state.Quiz.isLastQuestion) {
138-
// user is not on the last question
139-
next({ name: 'quiz' });
140-
return;
141-
}
142-
143-
next();
144-
}
145106
},
146107
{
147-
path: "/quiz/intro",
148-
name: "quiz-intro",
149-
component: () => import("@/views/Quiz/QuizIntro"),
150-
meta: {
151-
secured: true
152-
}
108+
path: '/quiz/intro',
109+
name: 'quiz-intro',
110+
component: () => import("@/views/Quiz/QuizIntro")
153111
},
154112
{
155113
path: "*",

src/store/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const state = {
2323
const actions = {
2424
async refresh({ commit }) {
2525
const user = api.auth.currentUser();
26-
26+
user.rank = user.rank + 1;
2727
if (user.auth) {
2828
commit("set", user);
2929
} else {

src/views/Quiz/Quiz.vue

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="mt-6" v-if="!isLoading">
2+
<div class="mt-6">
33
<v-row justify="center">
44
<quiz-scroll>
55
<template v-slot:title>Level {{ rank }}</template>
@@ -8,7 +8,7 @@
88
<div class="scroll-content" v-html="question" />
99
</template>
1010
</quiz-scroll>
11-
<quiz-answer :rank="rank" @next="onNext" :isLoading="isLoading" />
11+
<quiz-answer :rank="rank" @next="onNext" />
1212
</v-row>
1313
<quiz-need-help />
1414
</div>
@@ -30,37 +30,21 @@ export default {
3030
},
3131
data() {
3232
return {
33-
isLoading: false,
3433
question: "",
3534
rank: "",
3635
asset: ""
3736
};
3837
},
39-
async mounted() {
40-
// document.getElementsByTagName("html")[0].style.overflowY = "hidden";
41-
// window.scrollTo(0, 0);
42-
await this.loadQuestion();
38+
async created() {
39+
this.question = this.Quiz.question;
40+
this.rank = this.Quiz.rank;
41+
this.asset = this.Quiz.asset;
4342
},
4443
methods: {
4544
async onNext() {
46-
await this.loadQuestion();
47-
},
48-
async loadQuestion() {
49-
this.isLoading = true;
50-
await this.$store.dispatch("Quiz/refresh");
51-
if (this.Quiz.awaitNextQuestion) {
52-
this.$router.push({ name: "quiz-countdown" });
53-
} else {
54-
this.question = this.Quiz.question;
55-
this.rank = this.Quiz.rank;
56-
this.asset = this.Quiz.asset;
57-
}
58-
this.isLoading = false;
45+
this.$router.go();
5946
}
6047
},
61-
// beforeDestroy() {
62-
// // document.getElementsByTagName("html")[0].style.overflowY = "scroll";
63-
// },
6448
computed: {
6549
...User.mapState(),
6650
...Quiz.mapState()

src/views/Quiz/QuizAnswer.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ import { User, Quiz } from "@/store";
8787
8888
export default {
8989
name: "quizAnswer",
90-
props: ["isLoading", "rank"],
90+
props: ["rank"],
9191
computed: {
9292
...Quiz.mapState(),
9393
...User.mapState(),
9494
isDisabled() {
95-
return this.isSubmitting || this.isLoading || this.wasCorrect;
95+
return this.isSubmitting || this.wasCorrect;
9696
},
9797
successMessage() {
9898
return this.successMessages[

src/views/Quiz/QuizFinalQuestion.vue

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
</v-row>
2020
</v-alert>
2121
</v-card-text>
22+
<v-card-text v-if="hasRightAnswer">
23+
<v-alert prominent type="success" color="secondary darken-2">
24+
<v-row align="center">
25+
<v-col
26+
class="grow"
27+
>You got the right answer! Go ahead and review your code. When you're ready to submit go ahead and click "Submit Answer"</v-col>
28+
</v-row>
29+
</v-alert>
30+
</v-card-text>
2231
<v-card-text>
2332
<v-select v-bind="fields.language" v-model="fields.language.value" />
2433
<code-editor
@@ -29,7 +38,18 @@
2938
</v-card-text>
3039
<v-card-actions>
3140
<v-spacer />
32-
<v-btn type="submit" :disabled="isSubmitting" large color="secondary darken-2">Submit Answer</v-btn>
41+
<v-btn
42+
:disabled="isSubmitting"
43+
large
44+
color="secondary darken-2"
45+
@click="checkAnswer"
46+
>Check Answer</v-btn>
47+
<v-btn
48+
type="submit"
49+
:disabled="isSubmitting || !hasRightAnswer "
50+
large
51+
color="secondary darken-2"
52+
>Submit Answer</v-btn>
3353
</v-card-actions>
3454
</v-form>
3555
</v-card>
@@ -64,6 +84,7 @@ output = calculateAnswer()`;
6484
return {
6585
isLoading: false,
6686
isSubmitting: false,
87+
hasRightAnswer: false,
6788
question: "",
6889
rank: "",
6990
asset: "",
@@ -100,56 +121,70 @@ output = calculateAnswer()`;
100121
}
101122
},
102123
async mounted() {
103-
await this.loadQuestion();
124+
this.question = this.Quiz.question;
125+
this.rank = this.Quiz.rank;
126+
this.asset = this.Quiz.asset;
104127
},
105128
methods: {
106-
async submit() {
129+
async makeRequest(checkOnly) {
130+
const response = await api.quiz.submitFinal(
131+
this.fields.code.value,
132+
this.fields.language.value === "javascript" ? "js" : "py",
133+
checkOnly
134+
);
135+
const isCorrect = response.correct;
136+
137+
if (isCorrect) {
138+
return true;
139+
} else {
140+
if (!!response.js_error) {
141+
return Promise.reject(response.js_error);
142+
} else {
143+
return Promise.reject(
144+
"Hmmm.... Your code doesn't seem to generate the correct answer. Try again."
145+
);
146+
}
147+
}
148+
},
149+
async checkAnswer() {
107150
if (this.isSubmitting) {
108151
return false;
109152
}
153+
this.hasRightAnswer = false;
154+
this.errorMessage = false;
110155
this.isSubmitting = true;
111-
try {
112-
const response = await api.quiz.submitFinal(
113-
this.fields.code.value,
114-
this.fields.language.value === "javascript" ? "js" : "py"
115-
);
116-
const isCorrect = response.correct;
117156
118-
if (isCorrect) {
119-
// await this.$store.dispatch("Quiz/clearWrongCount");
120-
// await api.auth.fetchState();
121-
// await this.$store.dispatch("Quiz/refresh");
122-
// this.showSuccessModal = true;
123-
// this.fields.answer.successMessages = ["Your answer was correct!"];
124-
// this.fields.answer.success = true;
157+
try {
158+
const response = await this.makeRequest(true);
159+
if (response !== true) {
160+
return Promise.reject(response);
125161
} else {
126-
if (!!response.js_error) {
127-
this.errorMessage = response.js_error;
128-
} else {
129-
this.errorMessage =
130-
"Hmmm.... Your code doesn't seem to generate the correct answer. Try again.";
131-
}
162+
this.hasRightAnswer = true;
132163
}
133164
} catch (err) {
134-
if (err.status === 429) {
135-
this.showRateLimitModal = true;
136-
} else {
137-
this.$store.dispatch("Snackbar/showError", err);
138-
}
165+
this.errorMessage = err;
139166
}
140167
this.isSubmitting = false;
141168
},
142-
async loadQuestion() {
143-
this.isLoading = true;
144-
await this.$store.dispatch("Quiz/refresh");
145-
if (this.Quiz.awaitNextQuestion) {
146-
this.$router.push({ name: "quiz-countdown" });
147-
} else {
148-
this.question = this.Quiz.question;
149-
this.rank = this.Quiz.rank;
150-
this.asset = this.Quiz.asset;
169+
async submit() {
170+
if (this.isSubmitting) {
171+
return false;
151172
}
152-
this.isLoading = false;
173+
this.hasRightAnswer = false;
174+
this.errorMessage = false;
175+
this.isSubmitting = true;
176+
177+
try {
178+
const response = await this.makeRequest(false);
179+
if (response !== true) {
180+
return Promise.reject(response);
181+
} else {
182+
this.$router.go();
183+
}
184+
} catch (err) {
185+
this.errorMessage = err;
186+
}
187+
this.isSubmitting = false;
153188
}
154189
},
155190
computed: {

0 commit comments

Comments
 (0)