-
Notifications
You must be signed in to change notification settings - Fork 2
/
firebaseAddQue.js
93 lines (81 loc) · 3.09 KB
/
firebaseAddQue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.4.0/firebase-app.js'
import {
getFirestore,
collection,
addDoc,
getDocs,
getDoc,
where,
query,
} from 'https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js'
// Your web app's Firebase configuration
import firebaseConfig from './firebaseConfig.js'
// Initialize Firebase
const app = initializeApp(firebaseConfig)
const db = getFirestore(app)
const addLink = document.getElementById('addQuestion')
addLink.addEventListener('click', () => {
document.getElementById('slideid').style.display = 'none'
document.getElementById('quizform').style.display = 'none'
document.getElementById('results-container').style.display = 'none'
document.getElementById('addlangform').style.display = 'none'
const resultContainer2 = document.getElementById('allquestion-container')
resultContainer2.style.display = 'none'
document.getElementById('filterdata').style.display = 'none'
const container = document.getElementById('add-form')
container.style.display = 'block'
document.getElementById('addQuestion-container').style.display = 'block'
document.getElementById('popup2').style.display = 'none'
document.getElementById('added').addEventListener('click', function () {
document.getElementById('popup2').style.display = 'flex'
})
document.getElementById('closePopup2').addEventListener('click', function () {
document.getElementById('popup2').style.display = 'none'
})
document.getElementById('reloadPage2').addEventListener('click', function () {
container.reset()
document.getElementById('popup2').style.display = 'none'
})
})
// Function to handle form submission
document
.getElementById('add-form')
.addEventListener('submit', function (event) {
event.preventDefault()
var question = document.getElementById('question').value
var options = [
document.getElementById('option1').value,
document.getElementById('option2').value,
document.getElementById('option3').value,
document.getElementById('option4').value,
]
var correctAnswer = document.getElementById('correct-answer').value
var correctAnswerText = document.getElementById(correctAnswer).value
var level = document.getElementById('level').value
var language = document.getElementById('languageinput').value
console.log('question ' + question)
console.log('Options ' + options)
console.log('ansOption ' + correctAnswer)
console.log('andText ' + correctAnswerText)
console.log('level ' + level)
console.log('Lang ' + language)
// Create data object
var questionData = {
question: question,
options: options,
ans: correctAnswerText,
level: level,
lang: language,
}
// Reference to the 'questions' collection
try {
// Add the question to Firebase
const docRef = addDoc(collection(db, 'questions'), {
questionData,
})
console.log('Question added with ID: ', docRef.id)
document.getElementById('add-form').reset()
} catch (error) {
console.error('Error adding question: ', error)
}
})