refactor addTaginTable and deleteTagInTable, add missing logic for automated tags in case of manual adding

This commit is contained in:
ma0068
2026-07-21 13:35:31 +02:00
parent 7cda22f807
commit 1ae02d8ed8
2 changed files with 61 additions and 10 deletions
+57 -10
View File
@@ -6,9 +6,52 @@ export function addTagInTable(addedTag, rows, matchKey, tagsKey = "tags")
rows.forEach(row =>
{
const rowData = row.getData();
let updated = false;
addedTag.response.forEach(tag =>
//add save check if String or Array and avoid same reference later for mutations
let raw = rowData[tagsKey];
let tags = typeof raw === "string"
? JSON.parse(raw || "[]")
: Array.isArray(raw)
? [...raw]
: [];
let updated = false;
for (const tag of addedTag.response)
{
if (rowData[matchKey] !== tag[matchKey])
continue;
//avoid double inserts
if (tags.some(x => x.id === tag.id))
continue;
const newTag = {
id: tag.id,
prestudent_id: tag.prestudent_id,
//add also information of addedTag
beschreibung: addedTag.beschreibung,
notiz: addedTag.notiz ?? "",
style: addedTag.style,
done: addedTag.done ?? false,
typ_kurzbz: addedTag.tag_typ_kurzbz ?? addedTag.typ_kurzbz, //here seem to be 2 variations
automatisiert: addedTag.automatisiert
};
tags.unshift(newTag);
updated = true;
}
if (updated)
{
row.update({
[tagsKey]: JSON.stringify(tags)
});
row.reformat();
}
});
/* addedTag.response.forEach(tag =>
{
if (rowData[matchKey] !== tag[matchKey])
return;
@@ -36,7 +79,7 @@ export function addTagInTable(addedTag, rows, matchKey, tagsKey = "tags")
if (updated)
row.update(rowData);
});
});*/
}
export function deleteTagInTable(deletedTag, rows, tagsKeys = ['tags'])
@@ -49,26 +92,30 @@ export function deleteTagInTable(deletedTag, rows, tagsKeys = ['tags'])
let updates = {};
let changed = false;
tagsKeys.forEach(key => {
let tags;
for (const key of tagsKeys) {
let raw = rowData[key];
let tags = [];
try {
tags = JSON.parse(rowData[key] || "[]");
if (typeof raw === "string") {
tags = JSON.parse(raw || "[]");
} else if (Array.isArray(raw)) {
tags = [...raw];
}
} catch (e) {
tags = [];
}
if (!Array.isArray(tags))
return;
continue;
let filtered = tags.filter(tag => tag?.id !== deletedTag);
if (filtered.length !== tags.length)
{
if (filtered.length !== tags.length) {
updates[key] = JSON.stringify(filtered);
changed = true;
}
});
}
if (changed) {
row.update(updates);
+4
View File
@@ -49,6 +49,10 @@ export function tagFormatter(cell, tagComponent)
tagElement.title = tag.notiz;
tagElement.className = "tag " + tag.style;
if (tag.done) tagElement.className += " tag_done";
if (tag.automatisiert){
tagElement.className += " tag_auto";
tagElement.innerHTML = "<i class='fa-solid fa-lock'></i> " + tag.beschreibung;
}
const tagDef = mappedData.find(t => t.typ_kurzbz === tag.typ_kurzbz);