covidassist/src/store/index.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-03-21 17:11:07 +01:00
import Vue from 'vue'
import Vuex from 'vuex'
2020-03-21 19:39:07 +01:00
import axios from 'axios';
2020-03-21 21:54:20 +01:00
import router from "@/router";
2020-03-21 17:11:07 +01:00
2020-03-21 19:39:07 +01:00
Vue.use(Vuex)
2020-03-21 18:57:15 +01:00
export default new Vuex.Store({
state: {
currentQuestion: 0,
answers: [],
2020-03-21 19:39:07 +01:00
questions: []
2020-03-21 18:57:15 +01:00
},
mutations: {
nextQuestion() {
2020-03-21 21:54:20 +01:00
if (this.state.currentQuestion <
2020-03-21 18:57:15 +01:00
this.state.questions.length - 1)
this.state.currentQuestion += 1
2020-03-21 21:54:20 +01:00
else {
router.push('/results')
}
2020-03-21 18:57:15 +01:00
},
previousQuestion() {
if (this.state.currentQuestion > 0) {
this.state.currentQuestion -= 1
}
2020-03-21 19:39:07 +01:00
},
2020-03-21 20:20:55 +01:00
answerQuestion(state, value) {
this.state.answers[this.state.currentQuestion] = value
},
2020-03-21 19:39:07 +01:00
init() {
axios.get('https://avian-safeguard-214619.firebaseio.com/questions.json')
.then(res => {
console.log(res)
this.state.questions = res.data
2020-03-21 21:54:20 +01:00
this.state.answers = Array.apply(0, Array(res.data.length)).map(function () {})
2020-03-21 19:39:07 +01:00
})
2020-03-21 18:57:15 +01:00
}
},
2020-03-21 21:54:20 +01:00
getters: {
numberQuestions: (state) => {
return state.questions.length
}
},
2020-03-21 19:39:07 +01:00
actions: {
},
2020-03-21 18:57:15 +01:00
modules: {}
2020-03-21 17:11:07 +01:00
})