-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (79 loc) · 2.89 KB
/
app.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";
import { getFirestore, collection, getDocs, addDoc, doc, deleteDoc, query, where, orderBy, limit, onSnapshot } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-firestore.js"
const firebaseConfig = {
apiKey: "AIzaSyCivPyidtXFNDOBvTsf0H2fW9NtSPPMebw",
authDomain: "database-d931d.firebaseapp.com",
projectId: "database-d931d",
storageBucket: "database-d931d.appspot.com",
messagingSenderId: "613550257035",
appId: "1:613550257035:web:6da9f48ded23b6237bb58e",
measurementId: "G-ED57X5NBJX"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const cafeList = document.querySelector('#cafe-list');
const form = document.querySelector('#add-cafe-form');
// create element & render cafe
function renderCafe(doc_) {
let li = document.createElement('li');
let name = document.createElement('span');
let city = document.createElement('span');
let rating = document.createElement('span');
let cross = document.createElement('div');
li.setAttribute('data-id', doc_.id);
name.textContent = doc_.data().name;
city.textContent = doc_.data().city;
rating.textContent = doc_.data().rating;
cross.textContent = 'x';
li.appendChild(name);
li.appendChild(city);
li.appendChild(rating);
li.appendChild(cross);
cafeList.appendChild(li);
//deleting date
cross.addEventListener('click', (e) => {
e.stopPropagation();
let id = e.target.parentElement.getAttribute('data-id');
deleteDoc(doc(db, "cafes", id));
});
}
// getting data
// var querySnapshot = await getDocs(collection(db, "cafes"));
// querySnapshot.forEach((doc) => {
// renderCafe(doc);
// });
// // simple query
// var querySnapshot = collection(db, "cafes");
// // var quer = await getDocs(query(querySnapshot, where("city", "==", "Ha Noi")));
// var quer = await getDocs(query(querySnapshot, orderBy("city"), limit(3)));
// quer.forEach((doc) => {
// renderCafe(doc);
// });
// saving data
form.addEventListener('submit', (e) => {
e.preventDefault();
addDoc(collection(db, "cafes"), {
name: form.name.value,
city: form.city.value,
rating: form.rating.value
});
form.name.value = '';
form.city.value = '';
form.rating.value = '';
})
// real-time listener
var querySnapshot = collection(db, "cafes");
onSnapshot(querySnapshot, snapshot => {
let changes = snapshot.docChanges();
changes.forEach(change => {
console.log(change.doc.data());
if(change.type == 'added'){
renderCafe(change.doc);
} else if (change.type == 'removed'){
let li = cafeList.querySelector('[data-id=' + change.doc.id + ']');
cafeList.removeChild(li);
}
});
});