mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-09-17 04:00:47 +08:00
added functions to edit and remove tags
This commit is contained in:
parent
6363c41d96
commit
6eff1bdb34
4 changed files with 127 additions and 22 deletions
|
@ -347,4 +347,48 @@ exports.addTag = functions.https.onCall((request,response) => {
|
|||
console.error(`error adding tag for ${request.uid} - ${e}`);
|
||||
return {status:-999};
|
||||
}
|
||||
})
|
||||
|
||||
exports.editTag = functions.https.onCall((request,response) => {
|
||||
try{
|
||||
|
||||
if(!isTagValid(request.name)){
|
||||
return {status:-1};
|
||||
}else{
|
||||
return admin.firestore().collection(`users/${request.uid}/tags`).doc(request.tagid).update({
|
||||
name: request.name
|
||||
}).then(e => {
|
||||
console.log(`user ${request.uid} updated a tag: ${request.name}`);
|
||||
return {
|
||||
status:1
|
||||
};
|
||||
}).catch(e => {
|
||||
console.error(`error while updating tag for user ${request.uid}: ${e.message}`);
|
||||
return {status:-999};
|
||||
})
|
||||
}
|
||||
|
||||
}catch(e){
|
||||
console.error(`error updating tag for ${request.uid} - ${e}`);
|
||||
return {status:-999};
|
||||
}
|
||||
})
|
||||
|
||||
exports.removeTag = functions.https.onCall((request,response) => {
|
||||
try{
|
||||
|
||||
return admin.firestore().collection(`users/${request.uid}/tags`).doc(request.tagid).delete().then(e => {
|
||||
console.log(`user ${request.uid} deleted a tag`);
|
||||
return {
|
||||
status:1
|
||||
};
|
||||
}).catch(e => {
|
||||
console.error(`error deleting tag for user ${request.uid}: ${e.message}`);
|
||||
return {status:-999};
|
||||
})
|
||||
|
||||
}catch(e){
|
||||
console.error(`error deleting tag for ${request.uid} - ${e}`);
|
||||
return {status:-999};
|
||||
}
|
||||
})
|
|
@ -33,7 +33,7 @@
|
|||
</div>
|
||||
<div class="notification">Signed in</div>
|
||||
<div id="tagsWrapper" class="hidden">
|
||||
<div id="tagsEdit" action="">
|
||||
<div id="tagsEdit" action="" tagid="">
|
||||
<div class="title"></div>
|
||||
<input type="text">
|
||||
<div class="button"><i class="fas fa-plus"></i></div>
|
||||
|
|
|
@ -33,6 +33,8 @@ let customText = "The quick brown fox jumps over the lazy dog";
|
|||
|
||||
const testCompleted = firebase.functions().httpsCallable('testCompleted');
|
||||
const addTag = firebase.functions().httpsCallable('addTag');
|
||||
const editTag = firebase.functions().httpsCallable('editTag');
|
||||
const removeTag = firebase.functions().httpsCallable('removeTag');
|
||||
|
||||
|
||||
function showNotification(text, time) {
|
||||
|
@ -1264,28 +1266,43 @@ function applyExtraTestColor(tc){
|
|||
}
|
||||
}
|
||||
|
||||
function showEditTags(action){
|
||||
function showEditTags(action,id,name){
|
||||
if(action === "add"){
|
||||
$("#tagsWrapper #tagsEdit").attr('action','add');
|
||||
$("#tagsWrapper #tagsEdit .title").html('Add new tag');
|
||||
$("#tagsWrapper #tagsEdit .button").html(`<i class="fas fa-plus"></i>`);
|
||||
$("#tagsWrapper #tagsEdit input").val('');
|
||||
$("#tagsWrapper #tagsEdit input").removeClass('hidden');
|
||||
}else if(action === "edit"){
|
||||
$("#tagsWrapper #tagsEdit").attr('action','edit');
|
||||
$("#tagsWrapper #tagsEdit").attr('tagid',id);
|
||||
$("#tagsWrapper #tagsEdit .title").html('Edit tag name');
|
||||
$("#tagsWrapper #tagsEdit .button").html(`<i class="fas fa-pen"></i>`);
|
||||
$("#tagsWrapper #tagsEdit input").val(name);
|
||||
$("#tagsWrapper #tagsEdit input").removeClass('hidden');
|
||||
}else if(action === "remove"){
|
||||
$("#tagsWrapper #tagsEdit").attr('action','remove');
|
||||
$("#tagsWrapper #tagsEdit").attr('tagid',id);
|
||||
$("#tagsWrapper #tagsEdit .title").html('Remove tag '+name);
|
||||
$("#tagsWrapper #tagsEdit .button").html(`<i class="fas fa-check"></i>`);
|
||||
$("#tagsWrapper #tagsEdit input").addClass('hidden');
|
||||
}
|
||||
|
||||
if ($("#tagsWrapper").hasClass("hidden")) {
|
||||
$("#tagsWrapper")
|
||||
.stop(true, true)
|
||||
.css("opacity", 0)
|
||||
.removeClass("hidden")
|
||||
.animate(
|
||||
{
|
||||
opacity: 1
|
||||
},
|
||||
100
|
||||
);
|
||||
.animate({opacity: 1},100,e=>{
|
||||
$("#tagsWrapper #tagsEdit input").focus();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function hideEditTags(){
|
||||
if (!$("#tagsWrapper").hasClass("hidden")) {
|
||||
$("#tagsWrapper #tagsEdit").attr('action','');
|
||||
$("#tagsWrapper #tagsEdit").attr('tagid','');
|
||||
$("#tagsWrapper")
|
||||
.stop(true, true)
|
||||
.css("opacity", 1)
|
||||
|
@ -1307,21 +1324,57 @@ $("#tagsWrapper").click(e => {
|
|||
$("#tagsWrapper #tagsEdit .button").click(e => {
|
||||
let action = $("#tagsWrapper #tagsEdit").attr('action');
|
||||
let inputVal = $("#tagsWrapper #tagsEdit input").val();
|
||||
let tagid = $("#tagsWrapper #tagsEdit").attr('tagid');
|
||||
hideEditTags();
|
||||
addTag({uid:firebase.auth().currentUser.uid,name:inputVal}).then(e => {
|
||||
let status = e.data.status;
|
||||
if(status === 1){
|
||||
showNotification('Tag added',2000);
|
||||
dbSnapshot.tags.push({
|
||||
name: inputVal,
|
||||
id: e.data.id
|
||||
})
|
||||
}else if(status === -1){
|
||||
showNotification('Invalid tag name',3000);
|
||||
}else if(status < -1){
|
||||
showNotification('Unknown error',3000);
|
||||
}
|
||||
})
|
||||
if(action === "add"){
|
||||
addTag({uid:firebase.auth().currentUser.uid,name:inputVal}).then(e => {
|
||||
let status = e.data.status;
|
||||
if(status === 1){
|
||||
showNotification('Tag added',2000);
|
||||
dbSnapshot.tags.push({
|
||||
name: inputVal,
|
||||
id: e.data.id
|
||||
})
|
||||
updateSettingsPage();
|
||||
}else if(status === -1){
|
||||
showNotification('Invalid tag name',3000);
|
||||
}else if(status < -1){
|
||||
showNotification('Unknown error',3000);
|
||||
}
|
||||
})
|
||||
}else if(action === "edit"){
|
||||
editTag({uid:firebase.auth().currentUser.uid,name:inputVal,tagid:tagid}).then(e => {
|
||||
let status = e.data.status;
|
||||
if(status === 1){
|
||||
showNotification('Tag updated',2000);
|
||||
dbSnapshot.tags.forEach(tag => {
|
||||
if(tag.id === tagid){
|
||||
tag.name = inputVal;
|
||||
}
|
||||
})
|
||||
updateSettingsPage();
|
||||
}else if(status === -1){
|
||||
showNotification('Invalid tag name',3000);
|
||||
}else if(status < -1){
|
||||
showNotification('Unknown error',3000);
|
||||
}
|
||||
})
|
||||
}else if(action === "remove"){
|
||||
removeTag({uid:firebase.auth().currentUser.uid,tagid:tagid}).then(e => {
|
||||
let status = e.data.status;
|
||||
if(status === 1){
|
||||
showNotification('Tag removed',2000);
|
||||
dbSnapshot.tags.forEach((tag,index) => {
|
||||
if(tag.id === tagid){
|
||||
dbSnapshot.tags.splice(index, 1);
|
||||
}
|
||||
})
|
||||
updateSettingsPage();
|
||||
}else if(status < -1){
|
||||
showNotification('Unknown error',3000);
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
|
|
@ -338,4 +338,12 @@ $(document).on("click",".pageSettings .section.tags .addTagButton",e => {
|
|||
|
||||
$(document).on("click",".pageSettings .section.tags .tagsList .tag .editButton",e => {
|
||||
let tagid = $(e.currentTarget).parent('.tag').attr('id');
|
||||
let name = $(e.currentTarget).siblings('.title').text();
|
||||
showEditTags('edit',tagid,name);
|
||||
})
|
||||
|
||||
$(document).on("click",".pageSettings .section.tags .tagsList .tag .removeButton",e => {
|
||||
let tagid = $(e.currentTarget).parent('.tag').attr('id');
|
||||
let name = $(e.currentTarget).siblings('.title').text();
|
||||
showEditTags('remove',tagid,name);
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue