diff --git a/src/main/resources/static/js/pages/index/index.js b/src/main/resources/static/js/pages/index/index.js index bc704c6..d21deac 100644 --- a/src/main/resources/static/js/pages/index/index.js +++ b/src/main/resources/static/js/pages/index/index.js @@ -10,7 +10,34 @@ async function addList(listName) { if (listAsJson) ui.addList(listAsJson); } -function editList() { +function editList(listLi) { + //erstellen das Forms + //wir nutzen für das Element nur das div, weil sonst alle items mitgelöscht werden würden + const formElement = ui.createEditListForm(listLi); + let id = listLi.id; + + listLi.replaceWith(formElement); + + formElement.querySelector(".editForm__name").focus(); + + function save() { + let formData = Object.fromEntries(new FormData(formElement)); + ui.editList(id, formElement, formData, listLi); + + console.log(`editing catogory ${id} with ${formData}`) + api.editList(id, { newListName: formData.newListName }); + + } + + formElement.addEventListener("submit", (event) => { + event.preventDefault(); + save(); + }); + + formElement.addEventListener("keydown", (e) => { + if (e.key == "Escape") formElement.replaceWith(listLi); + }) + } @@ -31,6 +58,7 @@ function init() { document.querySelector(".index").addEventListener("click", (event) => { if (event.target.matches(".list__editButton")) { + editList(event.target.closest("li")); } else if (event.target.matches(".list__deleteButton")) { deleteList(event.target.closest("li"));