-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.js
110 lines (97 loc) · 3.28 KB
/
sandbox.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
103
104
105
106
107
108
109
110
const bandList = document.getElementById('bandlist');
const form = document.getElementById('addMember');
// getData();
async function getData() {
let snapshot = await db.collection('band').get()
let array = [];
snapshot.docs.forEach(doc => {
let e = doc.data();
e.id = doc.id;
array.push(e);
})
array.sort(function(a, b) {
let aTime = a.time.substring(11, 23).split(':');
let bTime = b.time.substring(11, 23).split(':');
let aSecounds = parseInt(aTime[0] * 60 * 60) + parseInt(aTime[1] * 60) + parseFloat(aTime[2])
let bSecounds = parseInt(bTime[0] * 60 * 60) + parseInt(bTime[1] * 60) + parseFloat(bTime[2])
return bSecounds - aSecounds;
});
console.log(array)
array.forEach(doc => {
renderData(doc);
})
}
function renderData(doc, id) {
bandList.innerHTML += `
<div data-id="${id}" style="background-color:lightgrey;margin:10px;width:250px">
Name: ${doc.name}
<div onclick="removeDiv(this)" style="float:right">X</div>
<br>
comment: ${doc.comment}
<br><br>
</div>
`;
// let cross = document.getElementById('cross')
// console.log(cross)
// cross.addEventListener('click', e => {
// console.log(e.target)
// // db.collection('band').doc(id).delete();
// })
}
function removeDiv(e) {
let parentId = e.parentElement.getAttribute('data-id')
db.collection('band').doc(parentId).delete();
}
// document.getElementById(id).addEventListener('click', e => {
// console.log(e);
// })
// cross.addEventListener('click', e => {
// // let kek = e.target.parentElement.getAttribute('data-id');
// console.log(e)
// // console.log(id)
// // db.collection('band').doc(id).delete();
// })
form.addEventListener('submit', e => {
e.preventDefault();
db.collection('band').add({
name: form.name.value,
comment: form.comment.value,
time: new Date().toISOString()
})
form.name.value = '';
form.comment.value = '';
})
getLiveData();
async function getLiveData() {
await db.collection('band').onSnapshot(snapshot => {
let change = snapshot.docChanges();
change.forEach(change => {
if (change.type == 'added') renderData(change.doc.data(), change.doc.id);
else if (change.type == 'removed') {
let id = document.getElementById(change.doc.id);
// let x = bandList.querySelector('[data-id=cX2J2JMMuKvaESIZWlV0]')
let y = bandList.querySelector('[data-id="' + change.doc.id + '"]')
// console.log(y);
y.remove();
}
})
})
// console.log(data)
}
// db.collection('band').get().then(snapshot => {
// snapshot.docs.forEach(doc => {
// console.log(doc.data());
// });
// })
// function renderData(doc) {
// let li = document.createElement('li');
// let name = document.createElement('span');
// let instrument = document.createElement('span');
// li.setAttribute('data-id', doc.id);
// name.textContent = doc.data().name;
// instrument.textContent = doc.data().instrument;
// li.appendChild(name);
// li.appendChild(instrument);
// bandList.appendChild(li);
// }
// console.log(new Date().toISOString().substring(14, 19))