Added pinia and stores

This commit is contained in:
Vivek Santayana 2022-08-31 00:50:48 +01:00
parent 6451073a62
commit 17e15712af
4 changed files with 62 additions and 0 deletions

View File

@ -2,7 +2,9 @@ import { createApp } from 'vue'
import '@/style.css' import '@/style.css'
import App from '@/App.vue' import App from '@/App.vue'
import Router from '@/router' import Router from '@/router'
import { createPinia } from 'pinia'
import '@/assets/js/quiz.js' import '@/assets/js/quiz.js'
const app = createApp(App) const app = createApp(App)
app.use(createPinia())
app.use(Router) app.use(Router)
app.mount('#app') app.mount('#app')

View File

@ -0,0 +1,22 @@
import { defineStore } from 'pinia'
export const useAnswersStore = defineStore({
id: 'answers',
state: () => ({
answers: [],
}),
actions: {
isAnswered(index) {
if (!Array.isArray(this.answers[index]) ) {
return this.answers[index] != null
} else {
return this.answers[index].length > 0
}
},
makeArray(index, selectNumber) {
if (selectNumber > 1 && !Array.isArray(this.answers[index])) {
this.answers[index] = [ ]
}
}
}
})

18
client/src/stores/app.js Normal file
View File

@ -0,0 +1,18 @@
import { defineStore } from 'pinia'
export const useAppStore = defineStore({
id: 'app',
state: () => ({
hasData: false,
results: {}
}),
actions: {
toggleData() {
this.hasData = !this.hasData
console.log('Toggled Has Data. New value', this.hasData)
},
storeResults(results) {
this.results = JSON.parse(JSON.stringify(results))
}
}
})

View File

@ -0,0 +1,20 @@
import { defineStore } from 'pinia'
export const useQuestionStore = defineStore({
id: 'questions',
state: () => ({
questions: [],
currentQuestion: 0
}),
actions: {
storeQuestions(questionArray) {
this.questions = [...questionArray]
},
nextQuestion() {
this.currentQuestion ++
},
previousQuestion() {
this.currentQuestion --
}
}
})