diff --git a/functions/index.js b/functions/index.js index 540b97b59..052ad1364 100644 --- a/functions/index.js +++ b/functions/index.js @@ -452,6 +452,67 @@ function checkIfPB(uid, obj, userdata) { } } +async function checkIfTagPB(uid, obj) { + if (obj.tags.length === 0) { + return []; + } + let dbtags = []; + let restags = obj.tags; + try { + let snap = await db.collection(`users/${uid}/tags`).get(); + snap.forEach((doc) => { + if (restags.includes(doc.id)) { + let data = doc.data(); + data.id = doc.id; + dbtags.push(data); + } + }); + } catch (e) { + return []; + } + let wpm = obj.wpm; + let ret = []; + for (let i = 0; i < dbtags.length; i++) { + let dbtag = dbtags[i]; + if (dbtag.pb === undefined || dbtag.pb < wpm) { + //no pb found, meaning this one is a pb + await db.collection(`users/${uid}/tags`).doc(dbtag.id).update({ + pb: wpm, + }); + ret.push(dbtag.id); + } + } + return ret; +} + +exports.clearTagPb = functions.https.onCall((request, response) => { + try { + return db + .collection(`users/${request.uid}/tags`) + .doc(request.tagid) + .update({ + pb: 0, + }) + .then((e) => { + return { + resultCode: 1, + }; + }) + .catch((e) => { + console.error( + `error deleting tag pb for user ${request.uid}: ${e.message}` + ); + return { + resultCode: -999, + message: e.message, + }; + }); + } catch (e) { + console.error(`error deleting tag pb for ${request.uid} - ${e}`); + return { resultCode: -999 }; + } +}); + function stdDev(array) { const n = array.length; const mean = array.reduce((a, b) => a + b) / n; @@ -1040,11 +1101,13 @@ exports.testCompleted = functions.https.onRequest(async (request, response) => { emailVerified ), checkIfPB(request.uid, request.obj, userdata), + checkIfTagPB(request.uid, request.obj), ]) .then(async (values) => { let globallb = values[0].insertedAt; let dailylb = values[1].insertedAt; let ispb = values[2]; + let tagPbs = values[3]; // console.log(values); if (obj.mode === "time" && String(obj.mode2) === "60") { @@ -1096,6 +1159,7 @@ exports.testCompleted = functions.https.onRequest(async (request, response) => { createdId: createdDocId, needsToVerify: values[0].needsToVerify, needsToVerifyEmail: values[0].needsToVerifyEmail, + tagPbs: tagPbs, }; if (ispb) { diff --git a/src/js/cloud-functions.js b/src/js/cloud-functions.js index b861d647b..4c420900f 100644 --- a/src/js/cloud-functions.js +++ b/src/js/cloud-functions.js @@ -26,3 +26,4 @@ export const namecheck = firebase export const getLeaderboard = firebase .functions() .httpsCallable("getLeaderboard"); +export const clearTagPb = firebase.functions().httpsCallable("clearTagPb"); diff --git a/src/js/settings.js b/src/js/settings.js index 9fbbca29a..9252b3dc2 100644 --- a/src/js/settings.js +++ b/src/js/settings.js @@ -569,6 +569,11 @@ function refreshTagsSettingsSection() { if (firebase.auth().currentUser !== null && db_getSnapshot() !== null) { let tagsEl = $(".pageSettings .section.tags .tagsList").empty(); db_getSnapshot().tags.forEach((tag) => { + let tagPbString = "No PB found"; + let balloon = ""; + if (tag.pb != undefined && tag.pb > 0) { + tagPbString = `PB: ${tag.pb}`; + } if (tag.active === true) { tagsEl.append(` @@ -578,6 +583,7 @@ function refreshTagsSettingsSection() {
${tag.name}
+
@@ -591,6 +597,7 @@ function refreshTagsSettingsSection() {
${tag.name}
+
@@ -812,6 +819,17 @@ $(document).on("click", ".pageSettings .section.tags .addTagButton", (e) => { showEditTags("add"); }); +$(document).on( + "click", + ".pageSettings .section.tags .tagsList .tag .clearPbButton", + (e) => { + let target = e.currentTarget; + let tagid = $(target).parent(".tag").attr("id"); + let tagname = $(target).siblings(".title")[0].innerHTML; + simplePopups.clearTagPb.show([tagid, tagname]); + } +); + $(document).on( "click", ".pageSettings .section.tags .tagsList .tag .editButton", diff --git a/src/js/simple-popups.js b/src/js/simple-popups.js index ef4d0b6d1..2f2d64d3c 100644 --- a/src/js/simple-popups.js +++ b/src/js/simple-popups.js @@ -7,8 +7,10 @@ class SimplePopup { inputs = [], text = "", buttonText = "Confirm", - execFn + execFn, + beforeShowFn ) { + this.parameters = []; this.id = id; this.type = type; this.execFn = execFn; @@ -18,6 +20,7 @@ class SimplePopup { this.wrapper = $("#simplePopupWrapper"); this.element = $("#simplePopup"); this.buttonText = buttonText; + this.beforeShowFn = beforeShowFn; } reset() { this.element.html(` @@ -30,16 +33,16 @@ class SimplePopup { init() { let el = this.element; el.find("input").val(""); - if (el.attr("popupId") !== this.id) { - this.reset(); - el.attr("popupId", this.id); - el.find(".title").text(this.title); - el.find(".text").text(this.text); + // if (el.attr("popupId") !== this.id) { + this.reset(); + el.attr("popupId", this.id); + el.find(".title").text(this.title); + el.find(".text").text(this.text); - this.initInputs(); + this.initInputs(); - el.find(".button").text(this.buttonText); - } + el.find(".button").text(this.buttonText); + // } } initInputs() { @@ -73,7 +76,9 @@ class SimplePopup { this.hide(); } - show() { + show(parameters) { + this.parameters = parameters; + this.beforeShowFn(); this.init(); this.wrapper .stop(true, true) @@ -161,5 +166,54 @@ simplePopups.updateEmail = new SimplePopup( } catch (e) { Misc.showNotification("Something went wrong: " + e, 5000); } + }, + () => {} +); + +simplePopups.clearTagPb = new SimplePopup( + "clearTagPb", + "text", + "Clear Tag PB", + [], + `Are you sure you want to clear this tags PB?`, + "Clear", + () => { + let tagid = eval("this.parameters[0]"); + showBackgroundLoader(); + CloudFunctions.clearTagPb({ + uid: firebase.auth().currentUser.uid, + tagid: tagid, + }) + .then((res) => { + hideBackgroundLoader(); + if (res.data.resultCode === 1) { + let tag = db_getSnapshot().tags.filter((t) => t.id === tagid)[0]; + tag.pb = 0; + $( + `.pageSettings .section.tags .tagsList .tag[id="${tagid}"] .clearPbButton` + ).attr("aria-label", "No PB found"); + Misc.showNotification("Tag PB cleared.", 1000); + } else { + console.error(res.data.message); + Misc.showNotification( + "Something went wrong: " + res.data.message, + 5000 + ); + } + }) + .catch((e) => { + hideBackgroundLoader(); + console.error(e); + Misc.showNotification( + "Something went wrong while clearing tag pb " + e, + 5000 + ); + }); + // console.log(`clearing for ${eval("this.parameters[0]")} ${eval("this.parameters[1]")}`); + }, + () => { + eval( + "this.text = `Are you sure you want to clear PB for tag ${eval('this.parameters[1]')}?`" + ); } ); diff --git a/src/sass/style.scss b/src/sass/style.scss index dd71a89ec..228f5888c 100644 --- a/src/sass/style.scss +++ b/src/sass/style.scss @@ -2250,7 +2250,7 @@ key { .tag { display: grid; display: grid; - grid-template-columns: auto 1fr auto auto; + grid-template-columns: auto 1fr auto auto auto; color: var(--text-color); .title { @@ -2258,6 +2258,7 @@ key { } .editButton, + .clearPbButton, .removeButton, .active { display: grid;