diff --git a/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java b/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java index 858ae39..ae09c8a 100644 --- a/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java +++ b/src/main/java/de/timaldinger/einkaufsliste/Controller/ShoppingListController.java @@ -1,5 +1,6 @@ package de.timaldinger.einkaufsliste.Controller; +import de.timaldinger.einkaufsliste.Dto.Patch.ShoppingListPatchDTO; import de.timaldinger.einkaufsliste.Dto.Post.ShoppingListPostDTO; import de.timaldinger.einkaufsliste.Dto.Response.ShoppingListResponseDTO; import de.timaldinger.einkaufsliste.Models.ShoppingItem; @@ -40,4 +41,15 @@ public class ShoppingListController { shoppingListService.deleteList(listID); return ResponseEntity.noContent().build(); } + + @PatchMapping("/api/list/{listID}") + public ResponseEntity editList(@PathVariable UUID listID,@RequestBody ShoppingListPatchDTO toChange){ + ShoppingList list = shoppingListRepository.findById(listID).orElseThrow(); + + if(toChange.newListName() != null) list.setListName(toChange.newListName()); + + shoppingListRepository.save(list); + + return ResponseEntity.noContent().build(); + } } diff --git a/src/main/resources/static/js/models/ShoppingList.js b/src/main/resources/static/js/models/ShoppingList.js index afa859c..2a1d0b5 100644 --- a/src/main/resources/static/js/models/ShoppingList.js +++ b/src/main/resources/static/js/models/ShoppingList.js @@ -24,6 +24,7 @@ export class ShoppingList { li.id = this.listID; a.setAttribute("href", `/list/${this.listID}`); + a.classList.add("list__name"); a.textContent = this.listName; editButton.classList.add("list__editButton"); diff --git a/src/main/resources/static/js/pages/index/api/indexAPI.js b/src/main/resources/static/js/pages/index/api/indexAPI.js index f02b2d2..32966f9 100644 --- a/src/main/resources/static/js/pages/index/api/indexAPI.js +++ b/src/main/resources/static/js/pages/index/api/indexAPI.js @@ -19,3 +19,15 @@ export async function deleteList(id) { return response; } + + +export async function editList(listID, toChange) { + const response = await fetch(`/api/list/${listID}`, + { + method: "PATCH", + body: JSON.stringify(toChange), + headers: { "Content-Type": "application/json" } + }); + + return response; +} diff --git a/src/main/resources/static/js/pages/index/frontend/indexUI.js b/src/main/resources/static/js/pages/index/frontend/indexUI.js index fdd9764..4728daf 100644 --- a/src/main/resources/static/js/pages/index/frontend/indexUI.js +++ b/src/main/resources/static/js/pages/index/frontend/indexUI.js @@ -10,3 +10,33 @@ export function addList(listAsJSON) { export function deleteList(listElement) { listElement.remove(); } + +export function createEditListForm(textElement) { + const editForm = document.createElement("form"); + const inputName = document.createElement("input"); + const abortButton = document.createElement("button"); + const saveButton = document.createElement("button"); + + inputName.setAttribute("name", "newListName"); + inputName.setAttribute("value", textElement.querySelector(".list__name").textContent); + inputName.classList.add("editForm__name"); + + abortButton.textContent = "Abbrechen"; + saveButton.textContent = "Speichern"; + + editForm.appendChild(inputName); + editForm.appendChild(saveButton); + editForm.appendChild(abortButton); + + return editForm; +} + +export function editList(id, formElement, formData, listLi) { + + let newName = formData.newListName; + + listLi.querySelector(".list__name").textContent = newName; + listLi.setAttribute("href", `/list/${id}`); + formElement.replaceWith(listLi); + +} 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")); diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 20dd7e0..5948bda 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -19,7 +19,7 @@ xmlns:th="http://www.thymeleaf.org">