-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (50 loc) · 2 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
77
78
79
80
81
82
83
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.0.0/firebase-app.js"
import { getDatabase, ref, push, onValue, remove} from "https://www.gstatic.com/firebasejs/10.0.0/firebase-database.js"
const appSettings ={
databaseURL :"https://we-are-champions-428f0-default-rtdb.europe-west1.firebasedatabase.app/"
}
const app = initializeApp(appSettings)
const database = getDatabase(app)
const endorsementsInDB = ref(database, "Endorsements")
const endorsements = document.getElementById("endorsements")
const textEl = document.getElementById("text")
const publishBtn = document.getElementById("publish-btn")
publishBtn.addEventListener("click", function(){
let outPut = textEl.value.trim()
if (outPut !==""){
push(endorsementsInDB, outPut)
clearInputField()
}
})
onValue(endorsementsInDB, function(snapshot){
let endorsementArray = []
if(snapshot.exists()){
endorsementArray = Object.entries(snapshot.val())
clearEndorsementListEl()
for(let i = 0; i < endorsementArray.length; i++){
let currentEndorsement = endorsementArray[i]
let currentEndorsementID = currentEndorsement[0]
let currentEndorsementValue = currentEndorsement[1]
displayEndorsements(currentEndorsement)
}
}else{
endorsements.innerHTML ="No endorsements written yet!"
}
})
function clearEndorsementListEl(){
endorsements.innerHTML=""
}
function clearInputField(){
textEl.value = ""
}
function displayEndorsements(endorsement){
let endorsementID = endorsement[0]
let endorsementValue =endorsement[1]
let newEndorsement = document.createElement("p")
newEndorsement.textContent = endorsementValue
newEndorsement.addEventListener("click", function(){
let exactLocationOfEndorsementInDB = ref(database, `Endorsements/${endorsementID}`)
remove(exactLocationOfEndorsementInDB)
})
endorsements.append(newEndorsement)
}