-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
197 lines (154 loc) · 5.72 KB
/
admin.js
File metadata and controls
197 lines (154 loc) · 5.72 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.1.0/firebase-app.js";
import { getFirestore, doc, where , query, getDocs, collection, getCountFromServer, addDoc, deleteDoc, orderBy, updateDoc, setDoc, limit} from "https://www.gstatic.com/firebasejs/10.1.0/firebase-firestore.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.1.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: "AIzaSyAuGqUNO-61s5l1eEnZDN2RV2qvtrTNfp4",
authDomain: "interviewapp-6083d.firebaseapp.com",
projectId: "interviewapp-6083d",
storageBucket: "interviewapp-6083d.appspot.com",
messagingSenderId: "417828000872",
appId: "1:417828000872:web:45ffa9bc7572eb7c4b17dd",
measurementId: "G-MERW8KK43H"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getFirestore();
const coll = collection(db, "questions");
const questionList = document.querySelector('#question-list');
const form = document.querySelector("#add-question-form");
//THIS DOES EVERYTHING FOR ADMIN.HTML
async function displayAllDocs(){
const questionRef = collection(db, 'questions');
const q = query(questionRef, orderBy("number"));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
let li = document.createElement('li');
li.className = "question-li";
// let quesNum = document.createElement('span');
// let question = document.createElement('span');
let cross = document.createElement('span');
cross.className = "remove-button";
li.setAttribute('data-id', doc.id);
// quesNum.textContent = doc.data().number;
// quesNum.textContent += ". "
// question.textContent = doc.data().question;
li.appendChild(document.createTextNode(doc.data().question));
cross.textContent = 'x';
// li.appendChild(quesNum);
// li.appendChild(question);
li.appendChild(cross);
questionList.appendChild(li);
cross.addEventListener('click', (e) => {
let id = e.target.parentElement.getAttribute('data-id');
deleteDocument(id);
})
});
}
async function deleteDocument(id){
const ques = await getQuestionFromLast();
const num = await getNumberFromLast();
// console.log("the question: " + ques);
// console.log("the number: " + num);
await deleteDoc(doc(db, "questions", id));
await updateList(id, num, ques);
location.reload();
}
async function getQuestionFromLast(){
const questionRef = collection(db, 'questions');
const q = query(questionRef, orderBy("number", "desc"), limit(1));
const querySnapshot = await getDocs(q);
let ques;
querySnapshot.forEach((doc) => {
ques = doc.data().question;
})
// console.log("to reutnr : " + ques);
return ques;
}
async function getNumberFromLast() {
const questionRef = collection(db, 'questions');
const q = query(questionRef, orderBy("number", "desc"), limit(1));
const querySnapshot = await getDocs(q);
let num;
querySnapshot.forEach((doc) => {
num = doc.data().number;
})
// console.log("to return : " + num);
return num;
}
displayAllDocs();
form.addEventListener('submit', (e) =>{
e.preventDefault();
addDocument();
})
async function getDbCount() {
const collectioz = collection(db, "questions");
const snapshot = await getCountFromServer(collectioz);
// console.log(snapshot.data().count);
return snapshot.data().count;
}
async function getActualCount() {
return getDbCount().then(x => {
return x;
})
}
async function addDocument(){
// const docRef = await addDoc(collection(db, "questions"), {
// number: await findMissingNum(),
// question: form.question.value
// });
let docId = await findMissingNum();
let stringId = docId.toString();
await setDoc(doc(db, "questions", stringId), {
number: docId,
question: form.question.value
});
form.question.value = '';
location.reload();
}
async function findMissingNum() {
if (await getActualCount() === 0) {
return 1;
}
//if found first missing number in between, return that missing number
//if the entire array has gone pass without finding missing number,
//return 1 + array size
let arr = [];
const questionRef = collection(db, 'questions');
const q = query(questionRef, orderBy("number"));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
arr.push(doc.data().number);
})
console.log(arr);
for(let i = 1; i <= arr.length; i++){
if(arr.indexOf(i) == -1) {
return i;
}
}
return await getActualCount() + 1;
}
async function updateList(id, numToDelete, ques){
const numToUpdate = parseInt(id);
const questionRef = collection(db, 'questions');
const actualCount = await getActualCount();
const missingNum = await findMissingNum();
if (missingNum === (actualCount + 1)) {
return; //nothing to update
}
else {
const q = query(questionRef, orderBy("number", "desc"), limit(1));
const querySnapshot = await getDocs(q);
await setDoc(doc(db, "questions", id), {
number: numToUpdate,
question: ques
});
let docToDelete = numToDelete.toString();
await deleteDoc(doc(db, "questions", docToDelete));
}
}