quiz fonctionel
This commit is contained in:
@ -4,4 +4,9 @@
|
|||||||
|
|
||||||
.question {
|
.question {
|
||||||
padding-top: 2rem;
|
padding-top: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.question button{
|
||||||
|
margin-left: auto;
|
||||||
|
display: block;
|
||||||
}
|
}
|
@ -1,17 +1,36 @@
|
|||||||
<template>
|
<template>
|
||||||
|
<div class="question">
|
||||||
<div class="question">
|
<h3>{{ question.question }}</h3>
|
||||||
<h3>{{ question.question }}</h3>
|
<ul>
|
||||||
|
<li v-for="(choice, index) in question.choices" :key="index">
|
||||||
</div>
|
<label :for="'answer' + index">
|
||||||
|
<input :id="'answer' + index" type="radio" name="answer" v-model="answer" :value="choice">
|
||||||
|
{{ choice }}
|
||||||
|
</label>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<button :disabled="!hasAnswer" @click="submitAnswer">Question suivante</button>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
|
import { computed, ref, watch } from 'vue';
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
question : Object
|
question: Object
|
||||||
})
|
});
|
||||||
|
|
||||||
</script>
|
const answer = ref(null);
|
||||||
|
const emit = defineEmits(['answer']);
|
||||||
|
const hasAnswer = computed(() => answer.value !== null);
|
||||||
|
|
||||||
|
const submitAnswer = () => {
|
||||||
|
if (hasAnswer.value) {
|
||||||
|
emit('answer', answer.value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
watch(() => props.question, () => {
|
||||||
|
answer.value = null;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h1>{{ quiz.title }}</h1>
|
<h1>{{ quiz.title }}</h1>
|
||||||
<Progress :value="step" :max="quiz.questions.length -1 " />
|
<Progress :value="step" :max="quiz.questions.length - 1" />
|
||||||
<Question :question="question" v-if="question"/>
|
<Question :key="answers.answers" :question="question" v-if="state === 'question'" @answer="addAnswer"/>
|
||||||
|
<Recap v-if="state === 'recap'" :answers="answers" :quiz="quiz" />
|
||||||
|
{{ answers }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
@ -16,13 +18,26 @@
|
|||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
import Progress from './Progress.vue';
|
import Progress from './Progress.vue';
|
||||||
import Question from './Question.vue';
|
import Question from './Question.vue';
|
||||||
|
import Recap from './Recap.vue';
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
quiz: Object
|
quiz: Object
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const state = ref('question')
|
||||||
|
const answers = ref(props.quiz.questions.map(() => null))
|
||||||
const step = ref(0)
|
const step = ref(0)
|
||||||
const question = computed(() => props.quiz.questions[step.value])
|
const question = computed(() => props.quiz.questions[step.value])
|
||||||
|
|
||||||
|
const addAnswer = (answer) =>{
|
||||||
|
answers.value[step.value] = answer
|
||||||
|
if (step.value === props.quiz.questions.length -1){
|
||||||
|
state.value = 'recap'
|
||||||
|
} else{
|
||||||
|
step.value++
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
29
src/components/Recap.vue
Normal file
29
src/components/Recap.vue
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<template>
|
||||||
|
<h1>Recap</h1>
|
||||||
|
<p>
|
||||||
|
{{ hasWon ? quiz.success_message : quiz.failure_message }}
|
||||||
|
</p>
|
||||||
|
<p>Score : {{ score }}/{{ quiz.questions.length }}</p>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { computed } from 'vue';
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
quiz: Object,
|
||||||
|
answers: Array
|
||||||
|
});
|
||||||
|
|
||||||
|
const score = computed(() => {
|
||||||
|
return props.quiz.questions.reduce((acc, question, k) => {
|
||||||
|
if (question.correct_answer === props.answers[k]) {
|
||||||
|
return acc + 1;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const hasWon = computed(() => score.value >= props.quiz.minimum_score)
|
||||||
|
|
||||||
|
</script>
|
Reference in New Issue
Block a user