-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (56 loc) · 1.77 KB
/
index.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
// javascript
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
import {
getDatabase,
ref,
push,
onValue,
} from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
const appSettings = {
databaseURL: "https://realtime-database-a4496-default-rtdb.firebaseio.com/",
};
const app = initializeApp(appSettings);
const database = getDatabase(app);
const endorsementInDb = ref(database, "endorsementList");
const inputEl = document.getElementById("input-field");
const publishEl = document.getElementById("publish-btn");
const displayEl = document.getElementById("endorsement-list");
publishEl.addEventListener("click", function () {
let list = inputEl.value.trim();
if (list !== "") {
push(endorsementInDb, list);
clearEl();
}
})
onValue(endorsementInDb, function (snapshot) {
displayEl.innerHTML = "";
if (snapshot.exists()) {
let endorsementArray = Object.values(snapshot.val()).reverse();
clearEl();
for (let i = 0; i < endorsementArray.length; i++) {
let currentEndorsement = endorsementArray[i];
appendNewEndorsement(currentEndorsement);
}
} else {
displayEl.innerHTML = `<p>No Endorsement Yet</p>`;
}
});
function clearEl() {
inputEl.value = "";
}
function appendNewEndorsement(item) {
let newEndorsement = document.createElement("p");
newEndorsement.textContent = item;
let loveEmoji = document.createElement("span");
loveEmoji.innerHTML = "❤";
loveEmoji.classList.add("love-emoji");
newEndorsement.append(loveEmoji);
loveEmoji.addEventListener("click", function () {
if (loveEmoji.style.color === "red") {
loveEmoji.style.color = "black";
} else {
loveEmoji.style.color = "red";
}
});
displayEl.append(newEndorsement);
}