Compare commits

Author SHA1 Message Date
Technoir 0a9fea3665 Merge branch 'feature/37-custom-element-list', fixes #37 2026-10-15 13:21:24 +02:00
Technoir 5783814697 wsp 2026-10-15 13:21:10 +02:00
Technoir 607db109b6 editList entfernt, Liste wird mit editElement bearbeitet 2026-10-15 13:21:03 +02:00
Technoir 75cc93f141 editList Funktionen entfernt 2026-10-15 13:20:52 +02:00
Technoir 6c8b61fdda bug 2026-10-15 13:20:43 +02:00
Technoir 200f23c5e1 neuen Listenname zu listName im DTO geändert 2026-10-15 13:20:40 +02:00
Technoir fcd902fd26 addList ersetllt shopping-list und nav-Element für die Navigation 2026-10-15 12:59:58 +02:00
Technoir cb9572e52c elemente mit shopping-list ersetzt 2026-10-15 12:57:22 +02:00
Technoir f8dfbbcd96 shopping-list definiert 2026-10-15 12:57:07 +02:00
Technoir e6ccba0219 ShoppingList zu einem custom html-element gemacht 2026-10-15 12:56:57 +02:00
Technoir 725879970f Merge branch 'maintanance/23-code-refactor', fixes #23
edit... wurde durch ein generisches editElement ersetzt, was eine config
als Parameter nimmt
2026-10-15 12:16:26 +02:00
7 changed files with 69 additions and 115 deletions

No files matched your search

@@ -61,7 +61,7 @@ public class ShoppingListController {
public ResponseEntity<Void> editList(@PathVariable UUID listID,@Valid @RequestBody ShoppingListPatchDTO toChange){
ShoppingList list = shoppingListRepository.findById(listID).orElseThrow(() -> new ResourceNotFoundException("listID", listID));
if(toChange.newListName() != null) list.setListName(toChange.newListName());
if(toChange.listName() != null) list.setListName(toChange.listName());
shoppingListRepository.save(list);
@@ -2,5 +2,5 @@ package de.timaldinger.einkaufsliste.Dto.Patch;
import jakarta.validation.constraints.NotBlank;
public record ShoppingListPatchDTO(@NotBlank(message = "listName must not be blank!") String newListName) {
public record ShoppingListPatchDTO(@NotBlank(message = "listName must not be blank!") String listName) {
}
@@ -1,31 +1,24 @@
export class ShoppingList extends HTMLElement {
static get observedAttributes() { return ["list-name"] }
export class ShoppingList {
constructor(name) {
this.listName = name;
this.lastModified = "";
this.created = "";
this.listID = 0;
constructor() {
super();
this._rendered = false
}
static fromJSON(listAsJSON) {
const list = new ShoppingList(listAsJSON.listName);
list.listID = listAsJSON.listID;
return list;
}
connectedCallback() {
if (this._rendered) return;
render() {
const li = document.createElement("li");
const a = document.createElement("a");
const editButton = document.createElement("button");
const deleteButton = document.createElement("button");
li.classList.add("list");
li.dataset.id = this.listID;
const listID = this.closest("li").dataset.id;
a.setAttribute("href", `/list/${this.listID}`);
a.setAttribute("href", `/list/${listID}`);
a.classList.add("list__name");
a.textContent = this.listName;
a.textContent = this.getAttribute("list-name");
editButton.classList.add("list__editButton");
editButton.textContent = "Liste bearbeiten";
@@ -33,23 +26,24 @@ export class ShoppingList {
deleteButton.classList.add("list__deleteButton");
deleteButton.textContent = "Liste löschen";
li.append(a, editButton, deleteButton);
return li;
this.append(a, editButton, deleteButton);
this._rendered = true;
}
renderNavElement() {
const li = document.createElement("li");
const a = document.createElement("a");
attributeChangedCallback(name, oldVal, newVal) {
if (!this._rendered) return;
if (oldVal == newVal) return;
li.classList.add("navbar__list");
li.dataset.id = this.listID;
const listLi = this.closest("li");
a.setAttribute("href", `/list/${this.listID}`);
a.textContent = this.listName;
li.append(a);
return li;
switch (name) {
case "list-name":
this.querySelector(".list__name").textContent = newVal;
document.querySelector(`.navbar__list[data-id='${listLi.dataset.id}'] > a`).textContent = newVal;
break;
default:
break;
}
}
}
@@ -1,23 +1,16 @@
export function addList(listName) {
return fetch(`/api/lists`,
{
method: "POST",
body: JSON.stringify({ listName: listName }),
headers: { "Content-Type": "application/json" }
}
);
});
}
export function deleteList(id) {
return fetch(`/api/lists/${id}`, { method: "DELETE" });
}
export function editList(listID, toChange) {
return fetch(`/api/lists/${listID}`,
{
@@ -1,13 +1,28 @@
import { ShoppingList } from "../../../models/ShoppingList.js";
export function addList(listAsJSON) {
const list = document.querySelector(".displayedList");
const li = document.createElement("li");
const navList = document.querySelector(".nav__dropdownMenu");
const listElement = ShoppingList.fromJSON(listAsJSON);
const listElement = document.createElement("shopping-list");
list.append(listElement.render());
navList.append(listElement.renderNavElement());
const navLi = document.createElement("li");
const a = document.createElement("a");
li.dataset.id = listAsJSON.listID;
li.classList.add("list");
listElement.setAttribute("list-name", listAsJSON.listName);
li.append(listElement);
list.append(li);
navLi.dataset.id = listAsJSON.listID;
navLi.classList.add("navbar__list")
a.setAttribute("href", `/list/${listAsJSON.listID}`);
a.textContent = listAsJSON.listName;
navLi.append(a);
navList.append(navLi);
}
export function deleteList(listElement) {
@@ -15,38 +30,3 @@ export function deleteList(listElement) {
document.querySelector(`li.navbar__list[data-id='${listElement.dataset.id}']`).remove();
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";
saveButton.classList.add("editForm__saveButton");
abortButton.classList.add("editForm__abortButton");
editForm.append(inputName, saveButton, abortButton);
return editForm;
}
export function editList(id, formElement, formData, listLi) {
let newName = formData.newListName;
document.querySelector(`.navbar__list[data-id='${listLi.dataset.id}'] > a`).textContent = newName;
listLi.querySelector(".list__name").textContent = newName;
listLi.setAttribute("href", `/list/${id}`);
formElement.replaceWith(listLi);
}
@@ -1,6 +1,8 @@
import { ShoppingList } from "../../models/ShoppingList.js";
import { editElement } from "../../shared/editing.js";
import { initErrors } from "../../shared/error.js";
import { handleResponse } from "../../shared/helper.js";
import { abortEditing } from "../../shared/sharedUI.js";
import { setElementAttributes } from "../../shared/sharedUI.js";
import * as api from "./api/indexAPI.js";
import * as ui from "./frontend/indexUI.js"
@@ -11,42 +13,16 @@ async function addList(listName) {
if (json) ui.addList(json);
}
function editList(listLi) {
//erstellen das Forms
const formElement = ui.createEditListForm(listLi);
let id = listLi.dataset.id;
listLi.replaceWith(formElement);
formElement.querySelector(".editForm__name").focus();
formElement.addEventListener("submit", async (event) => {
event.preventDefault();
let formData = Object.fromEntries(new FormData(formElement));
const result = await handleResponse(api.editList(id, { newListName: formData.newListName }), `error editing list`);
if (result) ui.editList(id, formElement, formData, listLi);
});
formElement.querySelector(".editForm__abortButton").addEventListener("click", () => {
abortEditing(formElement, listLi);
})
formElement.addEventListener("keydown", (e) => {
if (e.key == "Escape") abortEditing(formElement, listLi);
})
}
async function deleteList(listElement) {
const json = await handleResponse(api.deleteList(listElement.dataset.id), `error deleting list`);
if (json) ui.deleteList(listElement);
}
function init() {
initErrors(document.querySelector(".nav"));
customElements.define("shopping-list", ShoppingList);
addListForm.addEventListener("submit", (e) => {
e.preventDefault();
const formData = Object.fromEntries(new FormData(addListForm));
@@ -55,14 +31,27 @@ function init() {
document.querySelector(".index").addEventListener("click", (event) => {
if (event.target.matches(".list__editButton")) {
editList(event.target.closest("li"));
const id = event.target.closest("li").dataset.id;
const shoppingListElement = event.target.closest("shopping-list");
const listName = shoppingListElement.querySelector(".list__name").textContent;
editElement({
element: shoppingListElement, apiCall: api.editList, id: id,
fields: [
{ name: "listName", value: listName, class: "editForm__name", attr: "list-name" }
]
, onSuccess: (f) => {
setElementAttributes(shoppingListElement, { "list-name": f.listName });
//nav wird hier ^ aktualisiert
}
, errmsg: `liste konnte nicht aktualsiiert werden`
})
} else if (event.target.matches(".list__deleteButton")) {
deleteList(event.target.closest("li"));
}
});
}
document.addEventListener('DOMContentLoaded', init);
+1 -3
View File
@@ -33,9 +33,7 @@ xmlns:th="http://www.thymeleaf.org">
<ul class="displayedList">
<li th:each="list : ${lists}" class="list" th:data-id="${list.id}">
<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>
<shopping-list th:attr="list-name=${list.listName}"></shopping-list>
</li>
</ul>
</div>