2022-10-07 14:15:53 +00:00
|
|
|
import {Module, VuexModule, Mutation} from 'vuex-module-decorators'
|
|
|
|
import {Question} from "~/repositories/models/question.model";
|
|
|
|
import {QuizRate, Response} from "~/repositories/models/quiz.model";
|
|
|
|
|
|
|
|
@Module({
|
|
|
|
name: 'quiz',
|
|
|
|
stateFactory: true,
|
|
|
|
namespaced: true,
|
|
|
|
})
|
|
|
|
export default class Quiz extends VuexModule {
|
|
|
|
|
2022-10-10 08:29:25 +00:00
|
|
|
responses = new Map<number, QuizRate>;
|
|
|
|
questionsRatedPerAxe = new Map<number, { questionId: number; rated: boolean }[]>;
|
2022-10-07 14:15:53 +00:00
|
|
|
|
|
|
|
@Mutation
|
|
|
|
initialize(questions: Map<number, Question[]>) {
|
|
|
|
questions.forEach((questions, axeId) => this.questionsRatedPerAxe.set(axeId, questions.map(value => {
|
|
|
|
return {
|
2022-10-10 08:29:25 +00:00
|
|
|
questionId: value.id,
|
|
|
|
rated: this.responses.has(value.id)
|
2022-10-07 14:15:53 +00:00
|
|
|
}
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Mutation
|
|
|
|
reset() {
|
|
|
|
this.responses.clear();
|
|
|
|
this.questionsRatedPerAxe.forEach((questions) => {
|
|
|
|
questions
|
|
|
|
.map(value => {
|
|
|
|
value.rated = false;
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Mutation
|
|
|
|
updateScoreResponse(response: Response) {
|
|
|
|
const previous = this.responses.get(response.questionId);
|
|
|
|
if (previous) {
|
|
|
|
this.responses.set(response.questionId, {
|
|
|
|
comment: previous.comment,
|
|
|
|
score: response.score
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.responses.set(response.questionId, {
|
|
|
|
score: response.score
|
|
|
|
});
|
|
|
|
}
|
2022-10-10 13:26:11 +00:00
|
|
|
console.info(this.responses);
|
2022-10-07 14:15:53 +00:00
|
|
|
const questionsRated = this.questionsRatedPerAxe.get(response.axeId);
|
|
|
|
if (questionsRated) {
|
|
|
|
questionsRated
|
|
|
|
.filter(value => value.questionId === response.questionId)
|
|
|
|
.map(value => {
|
|
|
|
value.rated = true;
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// else should not happen
|
|
|
|
}
|
|
|
|
|
|
|
|
@Mutation
|
|
|
|
updateCommentResponse(response: Response) {
|
|
|
|
const previous = this.responses.get(response.questionId);
|
|
|
|
if (previous) {
|
|
|
|
this.responses.set(response.questionId, {
|
|
|
|
score: previous.score,
|
|
|
|
comment: response.comment
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.responses.set(response.questionId, {
|
|
|
|
comment: response.comment
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|