refactor(client): store

use namespaces
pull/10/head
sundowndev 2019-10-30 11:30:11 +01:00
parent eb7c8c5f3b
commit 69b4a17da8
4 changed files with 16 additions and 20 deletions

View File

@ -26,20 +26,15 @@ export default Vue.extend({
reset: Function, reset: Function,
}, },
computed: { computed: {
...mapState(['score', 'questions']), ...mapGetters('questions', ['questions', 'score']),
...mapGetters([
'questions',
'score',
]),
}, },
methods: { methods: {
text() { text() {
const text = this.messages.filter((msg) => this.score >= msg.min).pop(); const text = this.messages.filter((msg) => this.score >= msg.min);
return text !== undefined ? text.msg : ''; return text[0] !== undefined ? text[0].msg : '';
}, },
}, },
}); });
</script> </script>

View File

@ -14,7 +14,7 @@ const questions: Module<
IStoreQuestions, IStoreQuestions,
any any
> = { > = {
namespaced: false, namespaced: true,
state: { state: {
index: 0, index: 0,
score: 0, score: 0,

View File

@ -9,7 +9,7 @@ export interface IStoreSchools {
} }
const schools: Module<IStoreSchools, any> = { const schools: Module<IStoreSchools, any> = {
namespaced: false, namespaced: true,
state: { state: {
schools: [], schools: [],
}, },

View File

@ -2,7 +2,10 @@
<div class="home"> <div class="home">
<div id="quizz_input" v-show="!finished"> <div id="quizz_input" v-show="!finished">
<h2 id="question_text">{{ currentQuestion.text }}</h2> <h2 id="question_text">{{ currentQuestion.text }}</h2>
<p>Plus Éemien ou Héticien ? <span class="progress">({{ index + 1 }}/{{ questions.length }})</span></p> <p>
Plus Éemien ou Héticien ?
<span class="progress">({{ index + 1 }}/{{ questions.length }})</span>
</p>
<span v-for="(choice) in schools" :key="choice.id"> <span v-for="(choice) in schools" :key="choice.id">
<button class="choice-btn" @click="answer(choice.id)">{{ choice.name }}</button> <button class="choice-btn" @click="answer(choice.id)">{{ choice.name }}</button>
@ -27,17 +30,15 @@ export default Vue.extend({
}; };
}, },
computed: { computed: {
...mapState(['index', 'score', 'questions', 'schools']), ...mapGetters('schools', ['schools']),
...mapGetters([ ...mapGetters('questions', [
'questions', 'questions',
'schools',
'index', 'index',
'score', 'score',
'currentQuestion', 'currentQuestion',
'checkAnswer', 'checkAnswer',
'isLastQuestion', 'isLastQuestion',
]), ]),
...mapMutations(['fetchQuestions', 'increaseIndex', 'increaseScore']),
}, },
methods: { methods: {
answer(answer: number): void { answer(answer: number): void {
@ -45,26 +46,26 @@ export default Vue.extend({
const questions: IQuestion[] = this.questions; const questions: IQuestion[] = this.questions;
if (this.checkAnswer({ answer })) { if (this.checkAnswer({ answer })) {
this.$store.dispatch('increaseScore'); this.$store.dispatch('questions/increaseScore');
} }
if (!this.isLastQuestion) { if (!this.isLastQuestion) {
this.$store.dispatch('increaseIndex'); this.$store.dispatch('questions/increaseIndex');
} else { } else {
this.finished = true; this.finished = true;
} }
}, },
resetGame(): void { resetGame(): void {
this.finished = false; this.finished = false;
this.$store.dispatch('resetState'); this.$store.dispatch('questions/resetState');
}, },
}, },
components: { components: {
ScoreScreen, ScoreScreen,
}, },
async created() { async created() {
await this.$store.dispatch('fetchQuestions'); await this.$store.dispatch('questions/fetchQuestions');
await this.$store.dispatch('fetchSchools'); await this.$store.dispatch('schools/fetchSchools');
}, },
}); });
</script> </script>