I'm trying to find a solution to three little problems in my vue app. The first one is about class binding. I have a bootstrap card that will display a question and relative answers. After the user clicks on a radio input, the answer is saved and the others options are disabled.
I'm checking dynamically if the user answer is correct but I'm unable to change the class of the card border dynamically. I want to have a black border before the answer is given and then if is right change the border class to border-success or border-danger if it's wrong. I've tried with this code but without success, the border will have the border-danger class also if no answer is given.
<div class="card text-dark bg-light border-secondary shadow mt-3 mb-3" :class="[userAnswers[qindex] === correctAnswers[qindex] && answered[qindex] ? 'border-success' : 'border-danger']">
Another small problem with the UI of the app is with the card footer. I have added a card footer that is not visible until the question displayed is answered. It will display the correct answer, but I've noticed that if a correct answer is given it will do a graphic glitch and will appear and then disappear.
<div class="card-footer bg-transparent border-success fw-bold" v-show="userAnswers[qindex] !== correctAnswers[qindex]">La risposta correttà è: {{ correctAnswers[qindex] }}</div>
To try fixing I've tested v-if and the actual v-show, also I've added a transition on the element with css but seems not work
.card-footer {
transition: ease-in .3s;
}
The last and most important problem is with a conditional check. I want to show a modal when the user have answered to all the available questions. I've tried to add an if() statement in the mounted hook of vue but it will never be fired. I've also tried to call the method inside the .then() callback of the axios call that will check each question answer but without success.
export default {
name: 'App',
data(){
return {
isLoading: true,
showResult: false,
elaspedTime: null,
questions: [],
answered: {},
userAnswers: [],
correctAnswers: []
}
},
mounted(){
this.init();
// statement will be never evalued
if( this.answered.length === this.questions.length ){
this.quizResults();
}
},
methods: {
init(){
axios({
method: 'GET',
url: 'http://localhost:8990/init'
}).then( (res) => {
this.questions = [];
this.questions = res.data;
console.log(this.questions);
this.isLoading = false;
});
},
checkAnswer(qindex, index, value){
// console.log(qindex, index, value);
this.answered[qindex] = true;
this.userAnswers.push(value);
axios({
method: 'POST',
url: 'http://localhost:8990/answercheck',
data: {
questionIndex: index,
choice: value
}
}).then( (res) => {
console.log(res.data);
this.correctAnswers.push(res.data.correctAnswer);
});
},
quizResults(){
// some logics after all the questions are answered
console.log('Results');
}
}
}
Can anyone help me to find a solution to fix these problems? Thank you.