added likes

This commit is contained in:
Nuno Coração
2022-09-28 23:43:50 +01:00
parent 0ee898836d
commit cc13795b9e
11 changed files with 130 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
if (typeof auth !== 'undefined') {
var viewsCollection = db.collection('views');
var likesCollection = db.collection('likes');
function numberWithCommas(x) {
//return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
@@ -16,6 +17,16 @@ if (typeof auth !== 'undefined') {
})
}
var update_likes = function (oid, id) {
likesCollection.doc(id).onSnapshot(doc => {
var data = doc.data();
if (data) {
var label = document.querySelectorAll("span[id='" + oid + "']")[0].innerText.split(' ')[1]
document.querySelectorAll("span[id='" + oid + "']")[0].innerText = numberWithCommas(data.likes) + " " + label
}
})
}
auth.signInAnonymously()
.then(() => {
@@ -28,6 +39,16 @@ if (typeof auth !== 'undefined') {
update_views(oid, id)
}
}
var likes_nodes = document.querySelectorAll("span[id^='likes_']")
for (var i in likes_nodes) {
var oid = likes_nodes[i].id
var id = oid ? oid.replaceAll("/", "-") : oid
if (id) {
update_likes(oid, id)
}
}
})
.catch((error) => {
var errorCode = error.code;