-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (56 loc) · 1.65 KB
/
Copy pathindex.js
File metadata and controls
62 lines (56 loc) · 1.65 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.3.0/firebase-app.js";
import {
getDatabase,
ref,
push,
onValue,
remove,
} from "https://www.gstatic.com/firebasejs/12.3.0/firebase-database.js";
const firebaseConfig = {
databaseURL:
"https://leads-tracker-app-3cffb-default-rtdb.europe-west1.firebasedatabase.app/",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
console.log(app);
const database = getDatabase(app);
console.log(database);
const referenceInDB = ref(database, "leads");
// let myLeads = [];
const inputEl = document.getElementById("input-el");
const inputBtn = document.getElementById("input-btn");
const ulEl = document.getElementById("ul-el");
const deleteBtn = document.getElementById("delete-btn");
function render(leads) {
let listItems = "";
for (let i = 0; i < leads.length; i++) {
listItems += `
<li>
<a target='_blank' href='${leads[i]}'>
${leads[i]}
</a>
</li>
`;
}
ulEl.innerHTML = listItems;
}
onValue(referenceInDB, function (snapshot) {
if (snapshot.exists()) {
const snapshotValues = snapshot.val();
const leads = Object.values(snapshotValues);
render(leads);
} else {
console.log("No leads found in database");
ulEl.innerHTML = "";
}
});
deleteBtn.addEventListener("dblclick", function () {
console.log("Delete button clicked");
// For now, just render empty array since we don't have delete functionality
remove(referenceInDB);
ulEl.innerHTML = "";
});
inputBtn.addEventListener("click", function () {
push(referenceInDB, inputEl.value);
inputEl.value = "";
});