Refactor some variable and method names

This commit is contained in:
2022-10-28 21:48:13 +02:00
parent 58fac72355
commit 9419a5904e
7 changed files with 119 additions and 62 deletions

View File

@@ -52,6 +52,7 @@ import { collection, doc, addDoc, writeBatch } from 'firebase/firestore'
// https://vee-validate.logaretm.com/v3/guide/basics.html // https://vee-validate.logaretm.com/v3/guide/basics.html
import { ValidationProvider, ValidationObserver, extend } from 'vee-validate' import { ValidationProvider, ValidationObserver, extend } from 'vee-validate'
import { required } from 'vee-validate/dist/rules' import { required } from 'vee-validate/dist/rules'
import { ClosedEndedQuestion, ClosedEndedQuestionConverter } from '~/plugins/closed-ended-question'
// Override the default error message of required fields // Override the default error message of required fields
extend('required', { extend('required', {
@@ -77,18 +78,19 @@ export default {
submit () { submit () {
this.loading = true this.loading = true
const q = { const q = new ClosedEndedQuestion(
ersteller: this.$auth.currentUser.uid, // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.User null,
erstellt: Date.now() / 1000, // Current UNIX timestamp in seconds this.question,
frage: this.question, this.answers,
antworten: this.answers, this.correctAnswer,
richtig: this.correctAnswer, this.$auth.currentUser.uid, // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.User
kommentare: this.comment ? [this.comment] : [], Date.now() / 1000, // Current UNIX timestamp in seconds
status: 'neu' this.comment ? [this.comment] : [],
} 'neu'
)
// Add a new document with a generated id // Add a new document with a generated id
addDoc(collection(this.$db, `kurse/${this.$store.state.selectedCourse}/fragenGeschlossen`), q) addDoc(collection(this.$db, `kurse/${this.$store.state.selectedCourse}/fragenGeschlossen`).withConverter(ClosedEndedQuestionConverter), q)
.then((docRef) => { .then((docRef) => {
// Successfully added new question to database // Successfully added new question to database
this.$toast({ content: 'Deine Frage wurde eingereicht!', color: 'success' }) this.$toast({ content: 'Deine Frage wurde eingereicht!', color: 'success' })
@@ -121,17 +123,18 @@ export default {
// Generate random integer between 0 (inlcusive) and 3 (inclusive) // Generate random integer between 0 (inlcusive) and 3 (inclusive)
const correctAnswer = Math.floor(Math.random() * (3 + 1)) const correctAnswer = Math.floor(Math.random() * (3 + 1))
const q = { const q = new ClosedEndedQuestion(
ersteller: this.$auth.currentUser.uid, // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.User null,
erstellt: Date.now() / 1000, // Current UNIX timestamp in seconds `Frage ${randomNumber}: Was ist die richtige Antwort? (Richtig: ${correctAnswer+1})`,
frage: `Frage ${randomNumber}: Was ist die richtige Antwort? (Richtig: ${correctAnswer+1})`, answers,
antworten: answers, answers[correctAnswer],
richtig: answers[correctAnswer], this.$auth.currentUser.uid, // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.User
kommentare: [], Date.now() / 1000, // Current UNIX timestamp in seconds
status: 'genehmigt' [],
} 'genehmigt'
)
const questionRef = doc(collection(this.$db, `kurse/${this.$store.state.selectedCourse}/fragenGeschlossen`)) const questionRef = doc(collection(this.$db, `kurse/${this.$store.state.selectedCourse}/fragenGeschlossen`).withConverter(ClosedEndedQuestionConverter))
batch.set(questionRef, q) batch.set(questionRef, q)
} }

View File

@@ -34,6 +34,7 @@
</v-row> </v-row>
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
<v-btn v-if="$config.NODE_ENV !== 'production'" depressed color="warning" @click="addQuestionsForTesting">+10 Fragen</v-btn>
<v-spacer /> <v-spacer />
<v-btn text color="primary" @click="clear">Reset</v-btn> <v-btn text color="primary" @click="clear">Reset</v-btn>
<v-btn type="submit" depressed color="primary" :loading="loading" :disabled="invalid">Einreichen</v-btn> <v-btn type="submit" depressed color="primary" :loading="loading" :disabled="invalid">Einreichen</v-btn>
@@ -44,11 +45,12 @@
</template> </template>
<script> <script>
import { collection, addDoc } from 'firebase/firestore' import { collection, doc, addDoc, writeBatch } from 'firebase/firestore'
// We use vee-validate@3 for form validation. // We use vee-validate@3 for form validation.
// https://vee-validate.logaretm.com/v3/guide/basics.html // https://vee-validate.logaretm.com/v3/guide/basics.html
import { ValidationProvider, ValidationObserver, extend } from 'vee-validate' import { ValidationProvider, ValidationObserver, extend } from 'vee-validate'
import { required, min } from 'vee-validate/dist/rules' import { required, min } from 'vee-validate/dist/rules'
import { OpenEndedQuestion, OpenEndedQuestionConverter } from '~/plugins/open-ended-question'
extend('required', { extend('required', {
...required, ...required,
@@ -76,17 +78,18 @@ export default {
submit () { submit () {
this.loading = true this.loading = true
const q = { const q = new OpenEndedQuestion(
ersteller: this.$auth.currentUser.uid, // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.User null,
erstellt: Date.now() / 1000, // Current UNIX timestamp in seconds this.question,
frage: this.question, this.solution,
musterloesung: this.solution, this.$auth.currentUser.uid, // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.User
bewertungen: [], Date.now() / 1000, // Current UNIX timestamp in seconds
schwierigkeit: [] [],
} []
)
// Add a new document with a generated id. // Add a new document with a generated id.
addDoc(collection(this.$db, `kurse/${this.$store.state.selectedCourse}/fragenOffen`), q) addDoc(collection(this.$db, `kurse/${this.$store.state.selectedCourse}/fragenOffen`).withConverter(OpenEndedQuestionConverter), q)
.then((docRef) => { .then((docRef) => {
// Successfully added new question to database // Successfully added new question to database
this.$toast({ content: 'Deine Frage wurde eingereicht!', color: 'success' }) this.$toast({ content: 'Deine Frage wurde eingereicht!', color: 'success' })
@@ -103,6 +106,58 @@ export default {
this.question = '' this.question = ''
this.solution = '' this.solution = ''
this.$refs.observer.reset() this.$refs.observer.reset()
},
// TODO: This method is used for writing test data to the database and should be removed in production
addQuestionsForTesting () {
this.loading = true
const solution =
`Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.\n\n
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat,
vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim
qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod
tincidunt ut laoreet dolore magna aliquam erat volutpat.`
const batch = writeBatch(this.$db)
for (let i = 1; i <= 10; i++) {
// Generate random integer between 1 (inlcusive) and 1000 (inclusive)
const randomNumber = Math.floor(Math.random() * (1000 - 1 + 1) + 1)
const q = new OpenEndedQuestion(
null,
`Frage ${randomNumber}: Erläutere den Sinn des Lebens.`,
solution,
this.$auth.currentUser.uid, // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.User
Date.now() / 1000, // Current UNIX timestamp in seconds
[],
[]
)
const questionRef = doc(collection(this.$db, `kurse/${this.$store.state.selectedCourse}/fragenOffen`).withConverter(OpenEndedQuestionConverter))
batch.set(questionRef, q)
}
// Commit the batch
batch.commit().then((empty) => {
// Successfully added new questions to the database
this.$toast({ content: '10 Fragen wurden hinzugefügt!', color: 'success' })
}).catch((error) => {
// Failed to add questions to the database; display error message
this.$toast({content: error, color: 'error'})
}).then(() => {
this.loading = false
})
} }
} }
} }

View File

@@ -1,5 +1,5 @@
<template> <template>
<v-navigation-drawer v-if="$store.state.userLoggedIn" app clipped :mini-variant="miniVariant"> <v-navigation-drawer v-if="$store.state.user" app clipped :mini-variant="miniVariant">
<v-list> <v-list>
<v-list-item link class="px-2" @click="gotoProfilePage"> <v-list-item link class="px-2" @click="gotoProfilePage">
<v-list-item-avatar> <v-list-item-avatar>

View File

@@ -33,7 +33,7 @@ export default {
}, },
computed: { computed: {
loggedIn () { loggedIn () {
return this.$store.state.userLoggedIn return this.$store.state.user
} }
}, },
methods: { methods: {

View File

@@ -1,22 +1,11 @@
<template> <template>
<v-container> <v-container fluid fill-height>
<v-row> <v-row>
<v-col cols="12"> <v-col cols="12">
<span class="text-h4">{{ title }}</span> <span class="text-h4">{{ title }}</span>
</v-col> </v-col>
<v-col cols="auto">
<v-btn depressed color="primary" @click="playVersus">Challenge Mode</v-btn>
</v-col>
<v-col cols="auto">
<v-btn depressed color="primary" @click="playCoop">Co-op Mode</v-btn>
</v-col>
<v-col cols="12">
<AddClosedEndedQuestion />
</v-col>
<v-col cols="12">
<AddOpenEndedQuestion />
</v-col>
</v-row> </v-row>
<NuxtChild />
</v-container> </v-container>
</template> </template>
@@ -35,16 +24,12 @@ export default {
}, },
created () { created () {
this.courseID = this.$route.params.course this.courseID = this.$route.params.course
},
methods: {
playVersus () {
// TODO
this.$toast({ content: 'Todo: Challenge Mode implementieren', color: 'info' })
},
playCoop () {
// TODO
this.$toast({ content: 'Todo: Co-op Mode implementieren', color: 'info' })
}
} }
} }
</script> </script>
<style>
.tete {
flex: 1 1 auto;
}
</style>

View File

@@ -6,6 +6,8 @@
<script> <script>
import { onAuthStateChanged } from 'firebase/auth' import { onAuthStateChanged } from 'firebase/auth'
import { doc, getDoc } from 'firebase/firestore'
import { UserConverter } from '~/plugins/user'
export default { export default {
name: 'IndexPage', name: 'IndexPage',
@@ -17,15 +19,27 @@ export default {
onAuthStateChanged(this.$auth, (user) => { onAuthStateChanged(this.$auth, (user) => {
this.$store.commit('initFirebase') this.$store.commit('initFirebase')
if (user) { if (user) {
// User is signed in; redirect to main page (dashboard) // User is signed in; get user data from db, then redirect to main page (dashboard)
this.$store.commit('setUserLoggedIn', true) this.getUser()
this.$router.push({ name: 'dashboard' })
} else { } else {
// User is signed out; redirect to login page // User is signed out; redirect to login page
this.$store.commit('setUserLoggedIn', false) this.$store.commit('setUser', null)
this.$router.push({ name: 'login' }) this.$router.push({ name: 'login' })
} }
}) })
},
methods: {
getUser () {
const ref = doc(this.$db, `benutzer/${this.$auth.currentUser.uid}`).withConverter(UserConverter)
getDoc(ref).then((docSnap) => {
if (docSnap.exists()) {
this.$store.commit('setUser', docSnap.data())
this.$router.push({ name: 'dashboard' })
} else {
this.$toast({ content: 'Benutzer konnte in DB nicht gefunden werden!', color: 'error' })
}
})
}
} }
} }
</script> </script>

View File

@@ -1,6 +1,6 @@
export const state = () => ({ export const state = () => ({
firebaseInitialized: false, firebaseInitialized: false,
userLoggedIn: false, user: null,
courses: {}, courses: {},
selectedCourse: undefined selectedCourse: undefined
}) })
@@ -15,8 +15,8 @@ export const mutations = {
initFirebase (state) { initFirebase (state) {
state.firebaseInitialized = true state.firebaseInitialized = true
}, },
setUserLoggedIn (state, isLoggedIn) { setUser (state, user) {
state.userLoggedIn = isLoggedIn state.user = user
}, },
setCourses (state, courses) { setCourses (state, courses) {
state.courses = courses state.courses = courses