-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
145 lines (126 loc) · 4.48 KB
/
index.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Question and Answer Site</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Question and Answer Site</h1>
</header>
<main>
<section id="ask-question">
<h2>Ask a Question</h2>
<form id="question-form">
<input type="text" id="question-input" placeholder="Enter your question" required>
<textarea id="details-input" placeholder="Enter additional details"></textarea>
<button type="submit">Submit</button>
</form>
</section>
<section id="recent-questions">
<h2>Recent Questions</h2>
<ul id="question-list">
<!-- Question items will be dynamically added here -->
</ul>
</section>
</main>
<script src="script.js"></script>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.0.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.0.0/firebase-analytics.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyDC2bWlRt-MH0ACGEYr-ZXXsyoP8P80Bk4",
authDomain: "ques-n-ans.firebaseapp.com",
projectId: "ques-n-ans",
storageBucket: "ques-n-ans.appspot.com",
messagingSenderId: "460448830416",
appId: "1:460448830416:web:b158e9daf4de5f0f509fd5",
measurementId: "G-TDGYMGPN1Z"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
</script>
<script>
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
var db = firebase.firestore();
var collectionName = "q-n-a";
function addQuestion(question, details) {
db.collection(collectionName).add({
question: question,
details: details
})
.then(function (docRef) {
console.log("Question added with ID: ", docRef.id);
})
.catch(function (error) {
console.error("Error adding question: ", error);
});
// You can also update the UI to show the newly added question immediately
var questionList = document.getElementById('question-list');
var li = document.createElement('li');
li.innerHTML = '<h3>' + question + '</h3><p>' + details + '</p>';
questionList.appendChild(li);
}
</script>
<script>
// Fetch and display the recent questions
function fetchRecentQuestions() {
db.collection(collectionName)
.orderBy("timestamp", "desc") // Assuming you have a 'timestamp' field in your documents
.limit(10) // Fetching the 10 most recent questions
.onSnapshot(function (snapshot) {
var questionList = document.getElementById('question-list');
questionList.innerHTML = ""; // Clear existing questions
snapshot.forEach(function (doc) {
var questionData = doc.data();
var question = questionData.question;
var details = questionData.details;
var li = document.createElement('li');
li.innerHTML = '<h3>' + question + '</h3><p>' + details + '</p>';
questionList.appendChild(li);
});
});
}
// Add a new question to Firestore
function addQuestion(question, details) {
db.collection(collectionName).add({
question: question,
details: details,
timestamp: firebase.firestore.FieldValue.serverTimestamp() // Add a timestamp field to the document
})
.then(function (docRef) {
console.log("Question added with ID: ", docRef.id);
})
.catch(function (error) {
console.error("Error adding question: ", error);
});
}
// Event listener for the form submission
document.getElementById('question-form').addEventListener('submit', function (e) {
e.preventDefault();
var questionInput = document.getElementById('question-input').value;
var detailsInput = document.getElementById('details-input').value;
addQuestion(questionInput, detailsInput);
clearForm();
});
// Clear the form after submission
function clearForm() {
document.getElementById('question-input').value = '';
document.getElementById('details-input').value = '';
}
// Fetch the recent questions when the page loads
window.addEventListener('load', function () {
fetchRecentQuestions();
});
</script>
</body>
</html>