Delete tag when update reveals its a duplicate

This commit is contained in:
Henrik Giesel 2021-06-25 03:32:09 +02:00
parent 473517c3b3
commit 2993a7b744
2 changed files with 21 additions and 8 deletions

View File

@ -30,6 +30,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
if (name.length === 0) {
deleteTag(event);
} else {
dispatch("tagupdate");
event.stopPropagation();
}
}
</script>

View File

@ -15,11 +15,20 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
let newInput: HTMLInputElement;
let newName: string = "";
function focusInputNew(): void {
function focusNewInput(): void {
newInput.focus();
}
const insertTagAt = (index: number) => () => {
function checkForDuplicateAt(index: number): void {
const names = tags.map(getName);
const nameToUpdateTo = names.splice(index, 1)[0];
if (names.includes(nameToUpdateTo)) {
deleteTagAt(index);
}
}
function insertTagAt(index: number): void {
const names = tags.map(getName);
const nameToInsert = names.splice(index, 1)[0];
@ -29,12 +38,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
tags.splice(index, 0, attachId(nameToInsert));
tags = tags;
};
}
const deleteTagAt = (index: number) => () => {
function deleteTagAt(index: number): void {
tags.splice(index, 1);
tags = tags;
};
}
function appendTag(): void {
const names = tags.map(getName);
@ -49,13 +58,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<StickyBottom>
<div class="d-flex flex-wrap">
<AddTagBadge on:click={focusInputNew} />
<AddTagBadge on:click={focusNewInput} />
{#each tags as tag, index (tag.id)}
<Tag
bind:name={tag.name}
on:tagadd={insertTagAt(index)}
on:tagdelete={deleteTagAt(index)}
on:tagupdate={() => checkForDuplicateAt(index)}
on:tagadd={() => insertTagAt(index)}
on:tagdelete={() => deleteTagAt(index)}
/>
{/each}