From d5b6af6cdce69b23e97286edf867e5a2a0ce94d2 Mon Sep 17 00:00:00 2001 From: thilo <thilo.billerbeck@officerent.de> Date: Sat, 21 Mar 2020 19:39:07 +0100 Subject: [PATCH 1/3] added fetching questions from firebase --- package.json | 1 + src/store/index.js | 31 ++++++++++++------------------- src/views/Home.vue | 3 +++ yarn.lock | 21 +++++++++++++++++++++ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index a034365..a960dd8 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "lint": "vue-cli-service lint" }, "dependencies": { + "axios": "^0.19.2", "core-js": "^3.6.4", "vue": "^2.6.11", "vue-router": "^3.1.5", diff --git a/src/store/index.js b/src/store/index.js index d84038e..c73e9ac 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,30 +1,15 @@ import Vue from 'vue' import Vuex from 'vuex' +import axios from 'axios'; + Vue.use(Vuex) -const preQuestions = [ - { - type: "slider", - question: "Wie alt sind Sie?" - }, - { - illustration: "", - type: "choice", - question: "Haben Sie sich in letzter Zeit in einem Risikogebiet aufgehalten?" - }, - { - illustration: "", - type: "choice", - question: "Hatten Sie Kontakt zu Personen, die positiv auf COVID-19 getestet wurden?" - } -] - export default new Vuex.Store({ state: { currentQuestion: 0, answers: [], - questions: preQuestions + questions: [] }, mutations: { nextQuestion() { @@ -36,8 +21,16 @@ export default new Vuex.Store({ if (this.state.currentQuestion > 0) { this.state.currentQuestion -= 1 } + }, + init() { + axios.get('https://avian-safeguard-214619.firebaseio.com/questions.json') + .then(res => { + console.log(res) + this.state.questions = res.data + }) } }, - actions: {}, + actions: { + }, modules: {} }) diff --git a/src/views/Home.vue b/src/views/Home.vue index ba47046..c309368 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -18,6 +18,9 @@ export default { name: 'Home', components: { // HelloWorld + }, + mounted() { + this.$store.commit("init") } } </script> diff --git a/yarn.lock b/yarn.lock index ba313e6..497c8c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1580,6 +1580,13 @@ aws4@^1.8.0: resolved "https://registry.npm.taobao.org/aws4/download/aws4-1.9.1.tgz?cache=0&sync_timestamp=1578958168482&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Faws4%2Fdownload%2Faws4-1.9.1.tgz#7e33d8f7d449b3f673cd72deb9abdc552dbe528e" integrity sha1-fjPY99RJs/ZzzXLeuavcVS2+Uo4= +axios@^0.19.2: + version "0.19.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27" + integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA== + dependencies: + follow-redirects "1.5.10" + babel-eslint@^10.0.3: version "10.1.0" resolved "https://registry.npm.taobao.org/babel-eslint/download/babel-eslint-10.1.0.tgz#6968e568a910b78fb3779cdd8b6ac2f479943232" @@ -2676,6 +2683,13 @@ debug@2.6.9, debug@^2.2.0, debug@^2.3.3: dependencies: ms "2.0.0" +debug@=3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" + integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== + dependencies: + ms "2.0.0" + debug@^3.0.0, debug@^3.1.1, debug@^3.2.5: version "3.2.6" resolved "https://registry.npm.taobao.org/debug/download/debug-3.2.6.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdebug%2Fdownload%2Fdebug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" @@ -3579,6 +3593,13 @@ flush-write-stream@^1.0.0: inherits "^2.0.3" readable-stream "^2.3.6" +follow-redirects@1.5.10: + version "1.5.10" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" + integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ== + dependencies: + debug "=3.1.0" + follow-redirects@^1.0.0: version "1.10.0" resolved "https://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.10.0.tgz#01f5263aee921c6a54fb91667f08f4155ce169eb" From 5246711deb85d37f66f71ad5b1534374168d06b7 Mon Sep 17 00:00:00 2001 From: thilo <thilo.billerbeck@officerent.de> Date: Sat, 21 Mar 2020 20:20:55 +0100 Subject: [PATCH 2/3] add ability to save answers --- src/components/Question.vue | 18 +++++++++++++----- src/store/index.js | 4 ++++ src/views/Home.vue | 3 --- src/views/Questionnaire.vue | 5 ++++- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/components/Question.vue b/src/components/Question.vue index 23a9992..b454e1a 100644 --- a/src/components/Question.vue +++ b/src/components/Question.vue @@ -1,16 +1,18 @@ <template> <div class="question"> <p>{{ question }}</p> - <div v-if="type === 'choice'" class="question-choice"> - <button>Ja</button> - <button>Nein</button> + <div v-if="type === 'yesno'" class="question-choice"> + <button v-on:click="$store.commit('nextQuestion')">Ja</button> + <button v-on:click="$store.commit('nextQuestion')">Nein</button> </div> <div v-if="type === 'slider'" class="question-slider"> <p>{{ selection }}</p> <input v-model="selection" type="range" min="0" max="150" value="18" class="slider"> </div> - <div v-if="type === 'options'" class="question-options"> - <button v-bind:key="answer" v-for="answer in answers">{{ answer }}</button> + <div v-if="type === 'choice'" class="question-options"> + <button v-bind:key="answer.text" + v-for="answer in answers" + v-on:click="answerQuestion(answer.value)">{{ answer.text }}</button> </div> <router-link to="/questionmaire"><button class="alert">Ich brauche Hilfe zu dieser Frage</button></router-link> </div> @@ -29,6 +31,12 @@ selection: 0 } }, + methods: { + answerQuestion(value) { + this.$store.commit('answerQuestion', value) + this.$store.commit('nextQuestion') + } + }, } </script> diff --git a/src/store/index.js b/src/store/index.js index c73e9ac..1f90c16 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -22,11 +22,15 @@ export default new Vuex.Store({ this.state.currentQuestion -= 1 } }, + 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(null, Array(res.data.length)).map(function () {}) }) } }, diff --git a/src/views/Home.vue b/src/views/Home.vue index c309368..ba47046 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -18,9 +18,6 @@ export default { name: 'Home', components: { // HelloWorld - }, - mounted() { - this.$store.commit("init") } } </script> diff --git a/src/views/Questionnaire.vue b/src/views/Questionnaire.vue index 3d7853d..b4061e4 100644 --- a/src/views/Questionnaire.vue +++ b/src/views/Questionnaire.vue @@ -11,7 +11,10 @@ import Question from "@/components/Question"; export default { name: "Questionnaire", - components: {Question} + components: {Question}, + mounted() { + this.$store.commit("init") + } } </script> From 7b44071e0e7a3eefe524ff7dedb8c3beb5c79ab4 Mon Sep 17 00:00:00 2001 From: thilo <thilo.billerbeck@officerent.de> Date: Sat, 21 Mar 2020 21:54:20 +0100 Subject: [PATCH 3/3] add ability to save answers --- src/router/index.js | 6 ++++++ src/store/index.js | 13 +++++++++++-- src/views/Results.vue | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/views/Results.vue diff --git a/src/router/index.js b/src/router/index.js index 8fa89ff..f8265d3 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -3,6 +3,7 @@ import VueRouter from 'vue-router' import Home from '../views/Home.vue' import Warning from '../views/Warning' import Questionnaire from "@/views/Questionnaire"; +import Results from "@/views/Results"; Vue.use(VueRouter) @@ -21,6 +22,11 @@ const routes = [ path: '/questionmaire', name: 'Fragebogen', component: Questionnaire + }, + { + path: '/results', + name: 'Ergebnisse', + component: Results } ] diff --git a/src/store/index.js b/src/store/index.js index 1f90c16..73153c1 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,6 +1,7 @@ import Vue from 'vue' import Vuex from 'vuex' import axios from 'axios'; +import router from "@/router"; Vue.use(Vuex) @@ -13,9 +14,12 @@ export default new Vuex.Store({ }, mutations: { nextQuestion() { - if (this.state.currentQuestion <= + if (this.state.currentQuestion < this.state.questions.length - 1) this.state.currentQuestion += 1 + else { + router.push('/results') + } }, previousQuestion() { if (this.state.currentQuestion > 0) { @@ -30,10 +34,15 @@ export default new Vuex.Store({ .then(res => { console.log(res) this.state.questions = res.data - this.state.answers = Array.apply(null, Array(res.data.length)).map(function () {}) + this.state.answers = Array.apply(0, Array(res.data.length)).map(function () {}) }) } }, + getters: { + numberQuestions: (state) => { + return state.questions.length + } + }, actions: { }, modules: {} diff --git a/src/views/Results.vue b/src/views/Results.vue new file mode 100644 index 0000000..6fc8747 --- /dev/null +++ b/src/views/Results.vue @@ -0,0 +1,24 @@ +<template> + <div> + <p>Ihre Wahrscheinlichkeit sich zu infizieren ist</p> + </div> +</template> + +<script> + export default { + name: "Results", + mounted() { + const biased = this.$store.state.answers.map((x, i) => { + return x * this.$store.state.questions[i].multiplicator + }) + const summed = biased.reduce(function(acc, current) { + return acc + current + }) + console.log(Math.round(summed / 10)) + } + } +</script> + +<style scoped> + +</style>