-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforums.js
More file actions
69 lines (61 loc) · 2.13 KB
/
forums.js
File metadata and controls
69 lines (61 loc) · 2.13 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
console.log("Initializing Database...")
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
import { getDatabase, ref, push, onValue } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-database.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: "AIzaSyAHKaVTCeF_7nOSv8UW5AFRA5iUCaxkW1M",
authDomain: "tmas-academy.firebaseapp.com",
projectId: "tmas-academy",
storageBucket: "tmas-academy.appspot.com",
messagingSenderId: "1085855121963",
appId: "1:1085855121963:web:bf449b4cbbc01b507e606e",
measurementId: "G-63SFYHGS25"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const database = getDatabase(app);
console.log("Database loaded!: ", database)
console.log(ref)
let postRef = ref(database, "posts");
onValue(postRef, (data) => {
let q = data.val();
let values = Object.values(q);
renderAllPosts(values);
}, (errorObject) => {
alert(`Error! ${errorObject.name}`)
})
function pushToDatabase(title, text, time) {
let obj = {
title: title,
text: text,
time: time
}
const getRef = ref(database, "posts")
console.log(getRef)
push(getRef, obj)
}
document.getElementById("sendPost").onclick = () => {
pushToDatabase(document.getElementById("postCreateTitle").value, document.getElementById("postCreateContent").value, new Date().valueOf());
//renderNewPost()
}
function renderAllPosts(list) {
let postDiv = document.getElementById('postDivs');
postDiv.innerHTML = "";
list.reverse();
list.forEach((post) => {
console.log(post);
postDiv.innerHTML += `<div class = "post">
<h1 class = "title">
${post.title}
</h1>
<time>${new Date(post.time)}</time>
<p>
${post.text}
<p>
</div>`
})
}