covidassist/src/store/index.js
2020-03-22 00:33:29 +01:00

53 lines
1.3 KiB
JavaScript

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