-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.mjs
88 lines (75 loc) · 3.05 KB
/
firebase.mjs
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
85
86
87
88
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js";
import { getFirestore, collection, addDoc, doc, getDoc, getDocs, deleteDoc } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js";
import { getAuth } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
const firebaseConfig = {
apiKey: "AIzaSyCOUTKC3w9_Xs6KWU8RfSLseGWrHatVew8",
authDomain: "eid-project-7ea45.firebaseapp.com",
projectId: "eid-project-7ea45",
storageBucket: "eid-project-7ea45.appspot.com",
messagingSenderId: "857659291828",
appId: "1:857659291828:web:0e589f214cb0a86134bfe1"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
let thanks = new Audio('./Assets/Thanks.mp3');
document.addEventListener('DOMContentLoaded', async (event) => {
await loadAllData();
var btn = document.querySelector('#submit');
btn.addEventListener('click', async (event) => {
event.preventDefault();
var userName = document.getElementById('userName');
var meatQuantity = document.getElementById('meatQuantity');
if (userName.value.trim() === "" || meatQuantity.value.trim() === "") {
alert("Please fill in all fields.");
return;
}
try {
const docRef = await addDoc(collection(db, "Eid-data"), {
personName: userName.value,
quantity: meatQuantity.value,
});
userName.value = "";
meatQuantity.value = "";
console.log("Document written with ID: ", docRef.id);
await loadAllData();
} catch (e) {
console.error("Error adding document: ", e);
}
});
document.querySelector('#Table').addEventListener('click', async (event) => {
if (event.target.classList.contains('delete-btn')) {
const tr = event.target.closest('tr');
const personName = tr.querySelector('td:nth-child(1)').innerText.trim();
const quantity = tr.querySelector('td:nth-child(2)').innerText.trim();
event.target.closest('tr').style.backgroundColor = 'rgba(188, 143, 143, 0.354)'
try {
const querySnapshot = await getDocs(collection(db, "Eid-data"));
querySnapshot.forEach(async (doc) => {
if (doc.data().personName === personName && doc.data().quantity === quantity) {
await deleteDoc(doc.ref);
thanks.play();
console.log("Document successfully deleted!");
tr.remove();
}
});
} catch (error) {
console.error("Error deleting document: ", error);
}
}
});
});
async function loadAllData() {
const querySnapshot = await getDocs(collection(db, "Eid-data"));
const tableBody = document.getElementById('Table').getElementsByTagName('tbody')[0];
tableBody.innerHTML = "";
querySnapshot.forEach((doc) => {
tableBody.innerHTML += `
<tr>
<td>${doc.data().personName}</td>
<td>${doc.data().quantity}</td>
<td><button class="delete-btn">Done</button></td>
</tr>
`;
});
}