Merge branch 'feature/16-rename-lists', fixes #16
This commit is contained in:
commit
390b3b266c
6 files changed
+85
-2
No files matched your search
@@ -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<Void> 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();
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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"));
|
||||
|
||||
@@ -19,7 +19,7 @@ xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<ul class="displayedList">
|
||||
<li th:each="list : ${lists}" class="list" th:id="${list.id}">
|
||||
<a th:text="${list.listName}" th:href="@{/list/{id}(id=${list.id})}"></a>
|
||||
<a class="list__name" th:text="${list.listName}" th:href="@{/list/{id}(id=${list.id})}"></a>
|
||||
<button type="button" class="list__editButton">Liste bearbeiten</button>
|
||||
<button type="button" class="list__deleteButton">Liste löschen</button>
|
||||
</li>
|
||||
|
||||
Reference in new issue
Block a user