mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-06-01 12:19:28 +00:00
refactor addTaginTable and deleteTagInTable, add missing logic for automated tags in case of manual adding
This commit is contained in:
@@ -6,36 +6,50 @@ export function addTagInTable(addedTag, rows, matchKey, tagsKey = "tags")
|
||||
rows.forEach(row =>
|
||||
{
|
||||
const rowData = row.getData();
|
||||
|
||||
//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;
|
||||
|
||||
addedTag.response.forEach(tag =>
|
||||
for (const tag of addedTag.response)
|
||||
{
|
||||
if (rowData[matchKey] !== tag[matchKey])
|
||||
return;
|
||||
continue;
|
||||
|
||||
let tags;
|
||||
try {
|
||||
tags = JSON.parse(rowData[tagsKey] || "[]");
|
||||
} catch (e) {
|
||||
tags = [];
|
||||
}
|
||||
//avoid double inserts
|
||||
if (tags.some(x => x.id === tag.id))
|
||||
continue;
|
||||
|
||||
if (!Array.isArray(tags))
|
||||
tags = [];
|
||||
|
||||
if (tags.some(t => t?.id === tag.id))
|
||||
return;
|
||||
|
||||
let newTag = { ...addedTag, id: tag.id };
|
||||
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);
|
||||
|
||||
rowData[tagsKey] = JSON.stringify(tags);
|
||||
updated = true;
|
||||
});
|
||||
}
|
||||
|
||||
if (updated)
|
||||
row.update(rowData);
|
||||
{
|
||||
row.update({
|
||||
[tagsKey]: JSON.stringify(tags)
|
||||
});
|
||||
|
||||
row.reformat();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -49,26 +63,31 @@ 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);
|
||||
@@ -77,7 +96,6 @@ export function deleteTagInTable(deletedTag, rows, tagsKeys = ['tags'])
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function updateTagInTable(updatedTag, rows, fields = ['tags'])
|
||||
{
|
||||
if (!Array.isArray(fields))
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user