-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteacher_classroom.js
167 lines (165 loc) · 5.46 KB
/
teacher_classroom.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
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
function makeid(length) {
var result = "";
var characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
window.addEventListener("DOMContentLoaded", (e) => {
auth.onAuthStateChanged((user) => {
if (user == null) window.location.replace("faculty-login.html");
});
// if (auth.currentUser == null) {
// window.location.replace("faculty-login.html");
// }
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get("id");
console.log(myParam);
db.collection("messages")
.doc(myParam)
.get()
.then((snapshot) => {
const data = snapshot.data();
for (key in data) {
const div = document.createElement("div");
div.className = "callout callout-primary";
div.style = "border-radius:20px;";
div.innerHTML = `<h4>Faculty - ${data[key].timestamp}</h4>${data[key].message}`;
console.log(div);
document.getElementById("message-container").appendChild(div);
}
});
db.collection("documents")
.doc(myParam)
.get()
.then((snapshot) => {
const data = snapshot.data();
for (key in data) {
const div = document.createElement("div");
div.className = "callout callout-danger";
div.style = "border-radius:20px;";
div.innerHTML = `<h5>${data[key].title}</h5>
<p>${data[key].description}</p>
<a href="${data[key].link}" download
>Click here to download</a
>`;
console.log(div);
document.getElementById("document-container").appendChild(div);
}
});
send_button = document.getElementById("msg_send");
documents_button = document.getElementById("doc_btn");
documents_button.addEventListener("click", (e) => {
e.preventDefault();
const title = document.getElementById("file_title").value;
const description = document.getElementById("file_description").value;
const url = document.getElementById("file_url").value;
document.getElementById("file_title").value = "";
document.getElementById("file_description").value = "";
document.getElementById("file_url").value = "";
const id = makeid(7);
const payload = {
title,
description,
link: url,
};
const data = {};
data[id] = payload;
db.collection("documents")
.doc(myParam)
.update(data)
.then((res) => {
const div = document.createElement("div");
div.className = "callout callout-danger";
div.style = "border-radius:20px;";
div.innerHTML = `<h5>${title}</h5>
<p>${description}</p>
<a href="${url}" download
>Click here to download</a
>`;
console.log(div);
document.getElementById("document-container").appendChild(div);
document.getElementById("close_documents").click();
})
.catch((err) => {
db.collection("documents")
.doc(myParam)
.set(data)
.then((res) => {
const div = document.createElement("div");
div.className = "callout callout-danger";
div.style = "border-radius:20px;";
div.innerHTML = `<h5>${title}</h5>
<p>${description}</p>
<a href="${url}" download
>Click here to download</a
>`;
console.log(div);
document.getElementById("document-container").appendChild(div);
document.getElementById("close_documents").click();
});
});
});
send_button.addEventListener("click", (e) => {
e.preventDefault();
let today = new Date();
let date =
today.getFullYear() +
"-" +
(today.getMonth() + 1) +
"-" +
today.getDate();
let time =
today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
let dateTime = date + " " + time;
const message = document.getElementById("msg_val").value;
document.getElementById("msg_val").value = "";
const id = makeid(7);
const payload = {
message,
timestamp: dateTime,
};
const data = {};
data[id] = payload;
db.collection("messages")
.doc(myParam)
.update(data)
.then((res) => {
const div = document.createElement("div");
div.className = "callout callout-primary";
div.style = "border-radius:20px;";
div.innerHTML = `<h4>Faculty - ${dateTime}</h4>${message}`;
console.log(div);
document.getElementById("message-container").appendChild(div);
})
.catch((err) => {
db.collection("messages")
.doc(myParam)
.set(data)
.then((res) => {
const div = document.createElement("div");
div.className = "callout callout-primary";
div.style = "border-radius:20px;";
div.innerHTML = `<h4>Faculty - ${dateTime}</h4>${message}`;
console.log(div);
document.getElementById("message-container").appendChild(div);
});
});
});
let logoutBtn = document.getElementById("logout");
logoutBtn.addEventListener("click", (e) => {
console.log("Hello imma leave now");
e.preventDefault();
auth
.signOut()
.then(function () {
window.location.replace("faculty-login.html");
})
.catch(function (error) {
console.error(error);
});
});
});