-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (66 loc) · 2.16 KB
/
index.js
File metadata and controls
84 lines (66 loc) · 2.16 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
import {
getDatabase,
ref,
push,
onValue,
remove,
} from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
// import 'dotenv/config.js';
const appSettings = {
databaseURL: "https://testground-68a87-default-rtdb.europe-west1.firebasedatabase.app",
};
const app = initializeApp(appSettings);
const database = getDatabase(app);
const listItemsInDB = ref(database, "items");
onValue(listItemsInDB, function (snapshot) {
if (snapshot.exists()) {
clearListEl();
var value = snapshot.val();
let itemsArray = Object.entries(value);
for (let i = 0; i < itemsArray.length; i++) {
let currentItems = itemsArray[i];
let currentItemID = currentItems[0];
let currentItemValue = currentItems[1];
appendListToItemListEl(currentItems);
}
} else {
shoppingListEl.innerHTML = "No Items here... Yet!";
}
});
const listEl = document.getElementsByTagName("ul");
const shoppingListEl = document.getElementById("shopping-list");
const inputEl = document.getElementById("input-field");
const addButtonEl = document.getElementById("add-button");
addButtonEl.addEventListener("click", () => {
let inputValue = inputEl.value;
push(listItemsInDB, inputValue);
// appendList(inputValue);
clearInputFieldEl();
});
function clearInputFieldEl() {
inputEl.value = "";
}
function clearListEl() {
shoppingListEl.innerHTML = "";
}
function appendListToItemListEl(item) {
// shoppingListEl.innerHTML += `<li>${currentItems}</li>`;
let itemID = item[0];
let itemValue = item[1];
let newEl = document.createElement("li");
newEl.textContent = itemValue;
newEl.addEventListener("click", () => {
let exactLocationOfItemInDB = ref(database, `items/${itemID}`);
remove(exactLocationOfItemInDB);
});
shoppingListEl.append(newEl);
}
const calcBtn = document.querySelector('#calc-btn');
calcBtn.addEventListener("click", toggleCalc);
const calculator = document.querySelector('#calc');
function toggleCalc() {
calculator.classList.toggle("visible");
calcBtn.classList.toggle("active");
calculator.classList.toggle("hidden");
};