Compare commits

Author SHA1 Message Date
Technoir 75d9591f2d Merge branch 'enhancement/26-wrong-input', fixes #26
es wurde vor allem das required vom eingabefeld entfernt weil die ganzen
fehlerdemlungen vom server kommen und das für bessere Fehlermeldungen un
Eingabealdiderung erlaubt
2026-10-17 16:10:20 +02:00
Technoir 3d01278a62 requried entfernt 2026-10-17 16:09:20 +02:00
Technoir f38b8b547f falsche property 2026-10-17 16:09:16 +02:00
Technoir 083dfa6e7b addListForm flex-wrap 2026-10-17 16:09:00 +02:00
Technoir ce55b3b2c8 Zeilenumbruch volle Breite 2026-10-17 16:08:28 +02:00
Technoir a63ecc4de5 clientseitige falsche eingabe wird rot hervorgeoben 2026-10-17 15:55:43 +02:00
Technoir 433445290c indexseite zeigt fehlermeldungen an 2026-10-17 15:48:33 +02:00
Technoir a5f0fa9ba3 input listenname hat jtz eine klasse 2026-10-17 15:48:23 +02:00
Technoir 0109a65d0b Fehlermeldung hizugefügt 2026-10-17 15:43:43 +02:00
Technoir 5a1bac447e err zu errSpan 2026-10-17 15:43:38 +02:00
Technoir d79be68bed json zu response geändert und success-checking angepasst 2026-10-17 15:43:32 +02:00
Technoir 719c63862e Fehler wird angezegit beim hinzufügen von Gegenstand und Kategorie 2026-10-17 15:43:20 +02:00
Technoir 3962a58bc9 edit Funktionen nutzen jtz showError 2026-10-17 15:34:54 +02:00
Technoir 921d7aef72 showError implementiert für modularität 2026-10-17 15:34:43 +02:00
Technoir 4b83562e7b fehler wird angezeigt wenn item falsch editiert 2026-10-17 15:28:46 +02:00
Technoir 3b2de4a2e4 editForma ls ganzes gestyled, stylen einzelner knöpfe weg 2026-10-17 15:24:04 +02:00
Technoir f2538a39bc error-message stying 2026-10-17 15:23:53 +02:00
Technoir c20841e475 reihenfolge geändert weilt sonst error 2026-10-17 15:23:23 +02:00
Technoir 003fba3a49 styling auf innerHeader umgegelgt, header selbst ist flex 2026-10-17 15:23:15 +02:00
Technoir 5b6a8e67bf editCategory angepasst, dass es den innerHeader entfernt und die fehlernachricht anzeigt, die felder rot markiert 2026-10-17 15:22:55 +02:00
Technoir 569e5223f2 editForm hat jtz Klasse für bessere Kontrolle 2026-10-17 15:15:25 +02:00
Technoir ea36b16a87 category hat jtz einen innerHeader div für bessere anordnung 2026-10-17 15:15:12 +02:00
Technoir 882c4d2707 an neues handleResponse return angepasst 2026-10-17 15:14:46 +02:00
Technoir 719f633615 handleResponse gibt ein objekt mit ok und json zurück 2026-10-17 15:06:56 +02:00
Technoir 9f63b689fc error.js erstellt jtz einen span mit de fehlernachricht 2026-10-17 15:06:45 +02:00
Technoir 1c796e4bad eingabefelder werden rot markiert bei falscher iengabe 2026-10-17 14:14:24 +02:00
Technoir c8cddd01df editing nimmt jtz eine Funktion onFailure die ausgeführt wird bei einem fehler 2026-10-17 14:14:15 +02:00
Technoir 1afabf80ce Merge branch 'feature/30-styling', fixes #30 2026-10-17 13:57:00 +02:00
Technoir 774212f63a editForm nach base verschoben 2026-10-17 13:55:34 +02:00
Technoir 3a5064f52f category hat eine durchegehende abgerundete border 2026-10-17 13:55:21 +02:00
Technoir 0a0a48233e padding erhöht und Items und Kategorien haben farbe wenn man hovert 2026-10-17 13:39:48 +02:00
Technoir 3d82b3561d editForm Elemente sind nicht mehr aneinenader 2026-10-17 13:23:15 +02:00
Technoir 90be6ce76e seite gestyled 2026-10-17 13:19:56 +02:00
Technoir b2c741510c Listen > zu Listen geändert 2026-10-17 13:18:24 +02:00
Technoir 37cf02dae5 Merge branch 'maintainance/36-class-names', fixes #36 2026-10-15 14:20:00 +02:00
Technoir 2c9a205c1e refactor: var und klassenanmen vereinheitlicht 2026-10-15 14:10:15 +02:00
Technoir a007e62c8e refactor: var und klassennamen vereinfheitlicht 2026-10-15 14:09:53 +02:00
Technoir b3eed14ede refactor: navbar__list zu nav__list 2026-10-15 14:09:37 +02:00
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
19 changed files with 470 additions and 225 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) {
}
@@ -4,5 +4,5 @@ import jakarta.validation.constraints.NotBlank;
import java.util.UUID;
public record ShoppingCategoryPostDTO(@NotBlank String categoryName, UUID listID) {
public record ShoppingCategoryPostDTO(@NotBlank(message = "categoryName must not be blank!") String categoryName, UUID listID) {
}
+123 -17
View File
@@ -1,28 +1,134 @@
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
.nav__dropdown {
position: relative;
:root {
/* Akzent (teal) */
--c-accent: #0f766e;
--c-accent-hover: #134e4a;
--c-accent-soft: #f0fdfa;
/* Spacing-Skala */
--space-1: .25rem; /* 4px */
--space-2: .5rem; /* 8px */
--space-3: 1rem; /* 16px */
--space-4: 1.5rem; /* 24px */
--space-5: 2rem; /* 32px */
--space-6: 3rem; /* 48px */
/* Neutrale (zinc) */
--c-bg: #ffffff;
--c-surface: #fafafa;
--c-border: #e4e4e7;
--c-muted: #71717a;
--c-text: #18181b;
--c-danger: #dc2626;
--radius: 6px;
--maxw: 640px;
--shadow: 0 4px 12px rgb(0 0 0 / .12);
}
.nav__dropdownMenu {
display: none;
position: absolute;
top: 100%;
left: 0;
body {
font-family: system-ui, sans-serif;
color: var(--c-text);
background-color: var(--c-bg);
line-height: 1.5;
}
.nav__dropdown:hover .nav__dropdownMenu,
.nav__dropdown.is-open .nav__dropdownMenu{
display: block;
.index, .title, .nav, body > .list {
max-width: var(--maxw);
margin-inline: auto;
padding: var(--space-3);
}
.nav > ul {
/* ---- Links ---- */
a, a:link, a:visited { color: var(--c-text); }
/* ---- Formular-Elemente ---- */
input:not([type="checkbox"]):not([type="radio"]), select {
font: inherit;
padding: var(--space-2) var(--space-3);
border: 1px solid var(--c-border);
border-radius: var(--radius);
background: var(--c-bg);
color: var(--c-text);
}
input::placeholder { color: var(--c-muted); }
input.is-invalid:not([type="checkbox"]):not([type="radio"]),
input:user-invalid:not([type="checkbox"]):not([type="radio"]),
select.is-invalid {
border-color: var(--c-danger);
}
.is-invalid:focus-visible {
outline-color: var(--c-danger);
}
button {
font: inherit;
padding: var(--space-2) var(--space-3);
border: 1px solid transparent;
border-radius: var(--radius);
background: var(--c-accent);
color: #fff;
cursor: pointer;
}
button:hover { background: var(--c-accent-hover); }
:focus-visible {
outline: 2px solid var(--c-accent);
outline-offset: 2px;
}
/* ---- Bearbeitung ---- */
.error-message{
flex-basis: 100%;
font-size: .75rem;
color: var(--c-danger);
}
.editForm {
display: flex;
gap: 1rem;
list-style: none;
margin: 1;
padding: 1;
flex-wrap: wrap;
align-items: center;
gap: var(--space-2);
align-items: center;
margin: var(--space-1);
}
.nav__dropdownMenu {
background-color: grey;
/* ---- Navigation ---- */
.nav { border-bottom: 1px solid var(--c-border); }
.nav__links {
list-style: none;
display: flex;
align-items: center;
gap: var(--space-4);
}
.nav a { text-decoration: none; }
.nav a:hover { color: var(--c-accent); }
.nav__dropdown { position: relative; cursor: default; }
.nav__dropdownMenu {
display: none;
position: absolute;
top: 100%;
left: 0;
min-width: 12rem;
margin-top: var(--space-2);
list-style: none;
background: var(--c-bg);
border: 1px solid var(--c-border);
border-radius: var(--radius);
box-shadow: var(--shadow);
padding: var(--space-2);
}
.nav__dropdown:hover .nav__dropdownMenu,
.nav__dropdown.is-open .nav__dropdownMenu { display: block; }
.nav__list a { display: block; padding: var(--space-1) var(--space-2); }
.nav__list a:hover { background: var(--c-surface); border-radius: var(--radius); }
+45
View File
@@ -0,0 +1,45 @@
.addListForm {
display: flex;
gap: var(--space-2);
margin-bottom: var(--space-4);
flex-wrap: wrap;
}
.addListForm input { flex: 1; }
.displayedList {
list-style: none;
display: flex;
flex-direction: column;
gap: var(--space-2);
}
.index .list {
padding: var(--space-2) var(--space-3);
background: var(--c-surface);
border: 1px solid var(--c-border);
border-radius: var(--radius);
}
shopping-list {
display: flex;
align-items: center;
gap: var(--space-2);
width: 100%;
}
.list__name {
flex: 1;
text-decoration: none;
font-weight: 500;
}
.list__name:hover { color: var(--c-accent); }
.list__editButton,
.list__deleteButton {
background: var(--c-bg);
color: var(--c-text);
border: 1px solid var(--c-border);
}
.list__editButton:hover,
.list__deleteButton:hover { background: var(--c-surface); }
.list__deleteButton { color: var(--c-danger); }
+94 -12
View File
@@ -1,22 +1,104 @@
.category__name--bold {
font-weight: bold;
.title h1 { font-size: 2rem; }
.addCategoryForm,
.addItemForm {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--space-2);
margin-bottom: var(--space-3);
}
.addItemForm__nameInput,
.addItemForm__amountInput { flex: 1; min-width: 8rem; }
.deleteAllDoneItemsButton {
margin-bottom: var(--space-4);
background: var(--c-bg);
color: var(--c-danger);
border: 1px solid var(--c-border);
}
.deleteAllDoneItemsButton:hover { background: var(--c-surface); }
.displayedList {
list-style: none;
display: flex;
flex-direction: column;
gap: var(--space-2);
}
.category__name--big {
font-size : 1.5rem;
shopping-category {
display: block;
border-left: 3px solid var(--c-border);
border-radius: var(--radius)
}
shopping-item[is-checked="true"] .item__text{
text-decoration: line-through;
.category__header{
display: flex;
}
.item {
.category__innerHeader {
display: flex;
align-items: center;
gap: .5em;
gap: var(--space-2);
padding: var(--space-2);
}
.category__name--big { font-size: 1.5rem; }
.category__name--bold { font-weight: bold; }
.category__deleteButton {
background: var(--c-bg);
color: var(--c-danger);
border: 1px solid var(--c-border);
}
.category__deleteButton:hover { background: var(--c-surface); }
.category__deleteAllLabel {
display: flex;
align-items: center;
gap: var(--space-1);
font-size: .875rem;
color: var(--c-muted);
margin-left: auto;
}
shopping-category > ul {
list-style: none;
display: flex;
flex-direction: column;
gap: var(--space-1);
padding-left: var(--space-3);
margin-top: var(--space-2);
}
.item__text{
display: inline-flex;
gap: .25em;
.item { padding: var(--space-1) 0; }
shopping-item:hover , shopping-category:hover {
background-color: var(--c-surface);
}
shopping-item {
display: flex;
align-items: center;
gap: var(--space-4);
padding: var(--space-1);
width: 100%;
}
.item__text {
display: inline-flex;
gap: var(--space-1);
flex: 1;
}
.item__amount { color: var(--c-muted); }
shopping-item:has(.item__doneCheckbox:checked) .item__text {
text-decoration: line-through;
color: var(--c-muted);
}
.item__deleteButton {
padding: var(--space-1) var(--space-2);
font-size: .875rem;
background: var(--c-bg);
color: var(--c-danger);
border: 1px solid var(--c-border);
}
.item__deleteButton:hover { background: var(--c-surface); }
@@ -9,31 +9,36 @@ export class ShoppingCategory extends HTMLElement {
connectedCallback() {
if (this._rendered) return;
const categoryHeaderDiv = document.createElement("div");
const categoryHeaderInnerDiv = document.createElement("div");
const span = document.createElement("span");
const deleteCategoryButton = document.createElement("button");
const labelDeleteAllItems = document.createElement("label");
const deleteAllItemsLabel = document.createElement("label");
const deleteAllItemsCheckbox = document.createElement("input");
const ul = document.createElement("ul");
categoryHeaderDiv.classList.add("category__header");
categoryHeaderInnerDiv.classList.add("category__innerHeader");
span.textContent = this.getAttribute("category-name");
span.classList.add("category__name", "category__name--bold", "category__name--big");
labelDeleteAllItems.classList.add("category__labelDeleteAll");
labelDeleteAllItems.textContent = "Gegenstände mit Kategorie löschen?";
deleteAllItemsLabel.classList.add("category__deleteAllLabel");
deleteAllItemsLabel.textContent = "Gegenstände mit Kategorie löschen?";
deleteCategoryButton.classList.add("category__deleteButton");
deleteCategoryButton.textContent = "Kategorie löschen";
deleteAllItemsCheckbox.setAttribute("type", "checkbox");
labelDeleteAllItems.append(deleteAllItemsCheckbox);
deleteAllItemsLabel.append(deleteAllItemsCheckbox);
categoryHeaderDiv.append(span, deleteCategoryButton, labelDeleteAllItems);
categoryHeaderInnerDiv.append(span, deleteCategoryButton, deleteAllItemsLabel);
categoryHeaderDiv.append(categoryHeaderInnerDiv);
this.prepend(categoryHeaderDiv);
@@ -10,33 +10,33 @@ export class ShoppingItem extends HTMLElement {
if (this._rendered) return;
console.log(`rendering element shopping-item`);
const checkboxDone = document.createElement("input");
const spanText = document.createElement("span");
const spanName = document.createElement("span");
const spanAmount = document.createElement("span");
const buttonDelete = document.createElement("button");
const doneCheckbox = document.createElement("input");
const textSpan = document.createElement("span");
const nameSpan = document.createElement("span");
const amountSpan = document.createElement("span");
const deleteButton = document.createElement("button");
checkboxDone.classList.add("item__checkboxDone");
checkboxDone.setAttribute("type", "checkbox");
checkboxDone.setAttribute("autocomplete", "off");
checkboxDone.checked = this.getAttribute("is-checked") === "true";
doneCheckbox.classList.add("item__doneCheckbox");
doneCheckbox.setAttribute("type", "checkbox");
doneCheckbox.setAttribute("autocomplete", "off");
doneCheckbox.checked = this.getAttribute("is-checked") === "true";
spanText.classList.add("item__text");
textSpan.classList.add("item__text");
spanName.classList.add("item__itemName");
spanAmount.classList.add("item__itemAmount");
nameSpan.classList.add("item__name");
amountSpan.classList.add("item__amount");
spanName.textContent = this.getAttribute("item-name");
spanAmount.textContent = this.getAttribute("item-amount");
nameSpan.textContent = this.getAttribute("item-name");
amountSpan.textContent = this.getAttribute("item-amount");
buttonDelete.classList.add("item__buttonDeleteItem");
buttonDelete.setAttribute("type", "button ");
buttonDelete.textContent = "Löschen";
deleteButton.classList.add("item__deleteButton");
deleteButton.setAttribute("type", "button ");
deleteButton.textContent = "Löschen";
spanText.append(spanName, spanAmount);
textSpan.append(nameSpan, amountSpan);
this.append(checkboxDone, spanText, buttonDelete);
this.append(doneCheckbox, textSpan, deleteButton);
this._rendered = true;
}
@@ -46,10 +46,10 @@ export class ShoppingItem extends HTMLElement {
if (newVal == oldVal) return;
switch (name) {
case "item-name":
this.querySelector(".item__itemName").textContent = newVal;
this.querySelector(".item__name").textContent = newVal;
break;
case "item-amount":
this.querySelector(".item__itemAmount").textContent = newVal;
this.querySelector(".item__amount").textContent = newVal;
break;
default:
break;
@@ -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(`.nav__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,52 +1,32 @@
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("nav__list")
a.setAttribute("href", `/list/${listAsJSON.listID}`);
a.textContent = listAsJSON.listName;
navLi.append(a);
navList.append(navLi);
}
export function deleteList(listElement) {
document.querySelector(`li.navbar__list[data-id='${listElement.dataset.id}']`).remove();
document.querySelector(`li.nav__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,52 +1,33 @@
import { initErrors } from "../../shared/error.js";
import { ShoppingList } from "../../models/ShoppingList.js";
import { editElement } from "../../shared/editing.js";
import { showError } 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"
const addListForm = document.querySelector(".addListForm");
async function addList(listName) {
const json = await handleResponse(api.addList(listName), `error creating list`);
if (json) ui.addList(json);
const response = await handleResponse(api.addList(listName), `error creating list`);
if (response.ok) {
ui.addList(response.json);
} else {
const name = document.querySelector(".addListForm__inputName");
showError({ highlightFields: [name], elementForMessage: addListForm, message: response.json.msg });
}
}
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);
const response = await handleResponse(api.deleteList(listElement.dataset.id), `error deleting list`);
if (response.ok) 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 +36,35 @@ 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
}
, onFailure: (response) => {
const editForm = document.querySelector(".editForm");
const name = document.querySelector("input.editForm__name");
showError({ highlightFields: [name], elementForMessage: editForm, message: response.json.msg });
}
, errmsg: `liste konnte nicht aktualsiiert werden`
})
} else if (event.target.matches(".list__deleteButton")) {
deleteList(event.target.closest("li"));
}
});
}
document.addEventListener('DOMContentLoaded', init);
+46 -18
View File
@@ -1,11 +1,11 @@
import { ShoppingCategory } from "../../models/ShoppingCategory.js";
import { ShoppingItem } from "../../models/ShoppingItem.js";
import { initErrors } from "../../shared/error.js";
import * as api from "./api/listAPI.js"
import * as ui from "./frontend/listUI.js"
import { handleResponse } from "../../shared/helper.js";
import { editElement } from "../../shared/editing.js";
import * as sharedUI from "../../shared/sharedUI.js";
import { createErrorSpan, showError } from "../../shared/error.js";
const addCategoryForm = document.querySelector(".addCategoryForm");
const addItemForm = document.querySelector(".addItemForm");
@@ -15,41 +15,53 @@ const deleteAllDoneItemsButton = document.querySelector(".deleteAllDoneItemsButt
async function addItem(itemName, itemAmount, categoryID) {
console.log(` adding item with id:${currentList.dataset.id}, name: ${itemName}, amount: ${itemAmount}, category: ${categoryID}`)
const json = await handleResponse(api.addItem(currentList.dataset.id, itemName, itemAmount, categoryID), `item couldnt be created!`);
if (json) ui.addItem(json);
const response = await handleResponse(api.addItem(currentList.dataset.id, itemName, itemAmount, categoryID), `item couldnt be created!`);
if (response.ok) {
ui.addItem(response.json);
} else {
const form = document.querySelector(".addItemForm");
const name = document.querySelector(".addItemForm__nameInput");
const amount = document.querySelector(".addItemForm__amountInput");
showError({ highlightFields: [name, amount], elementForMessage: form, message: response.json.msg });
}
}
async function addCategory(categoryName) {
console.log(`adding category ${categoryName}`)
const json = await handleResponse(api.addCategory(currentList.dataset.id, categoryName), `category couldnt be added!`);
if (json) ui.addCategory(json);
const response = await handleResponse(api.addCategory(currentList.dataset.id, categoryName), `category couldnt be added!`);
if (response.ok) {
ui.addCategory(response.json);
} else {
const form = document.querySelector(".addCategoryForm");
const name = document.querySelector(".addCategoryForm__nameInput");
showError({ highlightFields: [name], elementForMessage: form, message: response.json.msg });
}
}
async function deleteItem(li) {
console.log(`deleting item ${li.dataset.id}`)
const json = await handleResponse(api.deleteItem(li.dataset.id), `error deleting item`);
if (json) ui.deleteItem(li);
const response = await handleResponse(api.deleteItem(li.dataset.id), `error deleting item`);
if (response.ok) ui.deleteItem(li);
}
async function deleteCategory(deleteAllItems, categoryID) {
console.log(`deleting category with allItems: ${deleteAllItems} id: ${categoryID}`);
const json = await handleResponse(api.deleteCategory(deleteAllItems, categoryID), `error deleting category`);
const response = await handleResponse(api.deleteCategory(deleteAllItems, categoryID), `error deleting category`);
if (json) ui.deleteCategory(deleteAllItems, categoryID);
if (response.ok) ui.deleteCategory(deleteAllItems, categoryID);
}
async function deleteAllDoneItems() {
console.log(`deleting all done items...`)
const json = await handleResponse(api.deleteAllDoneItems(currentList.dataset.id), `error deleting all done items`);
if (json) ui.deleteAllDoneItems();
const response = await handleResponse(api.deleteAllDoneItems(currentList.dataset.id), `error deleting all done items`);
if (response.ok) ui.deleteAllDoneItems();
}
function init() {
initErrors(document.querySelector(".nav"));
customElements.define("shopping-item", ShoppingItem);
customElements.define("shopping-category", ShoppingCategory);
@@ -72,7 +84,7 @@ function init() {
//event delegation ist schon geil
document.querySelector(".displayedList").addEventListener("click", (event) => {
if (event.target.matches(".item__buttonDeleteItem")) {
if (event.target.matches(".item__deleteButton")) {
deleteItem(event.target.closest("li"));
} else if (event.target.matches(".category__deleteButton")) {
@@ -86,8 +98,8 @@ function init() {
const shoppingItemElement = event.target.closest("shopping-item");
const id = event.target.closest("li").dataset.id;
let itemName = shoppingItemElement.querySelector(".item__itemName").textContent;
let itemAmount = shoppingItemElement.querySelector(".item__itemAmount").textContent;
let itemName = shoppingItemElement.querySelector(".item__name").textContent;
let itemAmount = shoppingItemElement.querySelector(".item__amount").textContent;
/*
* { name: "", value: "", class: "", attr: ""}
*/
@@ -99,19 +111,27 @@ function init() {
{ name: "itemAmount", value: itemAmount, class: "editForm__amount", attr: "item-amount" }
]
, onSuccess: (f) => { sharedUI.setElementAttributes(shoppingItemElement, { "item-name": f.itemName, "item-amount": f.itemAmount }) }
, onFailure: (response) => {
const editForm = document.querySelector(".editForm");
const name = document.querySelector("input.editForm__name");
const amount = document.querySelector("input.editForm__amount")
showError({ highlightFields: [name, amount], elementForMessage: editForm, message: response.json.msg });
}
, errmsg: `konnte Item nicht bearbeiten`
});
} else if (event.target.closest(".category__name")) {
const categoryElement = event.target.closest("shopping-category");
const categoryHeader = event.target.closest(".category__header");
const categoryInnerHeader = event.target.closest(".category__innerHeader");
const id = event.target.closest("li").dataset.id;
let categoryName = categoryElement.querySelector(".category__name").textContent;
editElement({
element: categoryHeader, apiCall: api.editCategory, id: id,
element: categoryInnerHeader, apiCall: api.editCategory, id: id,
fields: [
{ name: "categoryName", value: categoryName, class: "editForm__name", attr: "category-name" },
]
@@ -119,6 +139,14 @@ function init() {
sharedUI.setElementAttributes(categoryElement, { "category-name": f.categoryName })
document.querySelector(`option[value='${id}']`).textContent = f.categoryName;
}
, onFailure: (response) => {
const editForm = document.querySelector(".editForm");
const name = document.querySelector(".editForm__name");
showError({ highlightFields: [name], elementForMessage: editForm, message: response.json.msg });
}
, errmsg: `konnte Kategorie nicht bearbeiten`
});
@@ -126,7 +154,7 @@ function init() {
});
document.querySelector(".displayedList").addEventListener("change", (e) => {
if (e.target.matches(".item__checkboxDone")) {
if (e.target.matches(".item__doneCheckbox")) {
const item = e.target.closest("li");
e.target.closest("shopping-item").setAttribute("is-checked", e.target.checked);
api.editItem(item.dataset.id, { isDone: e.target.checked });
@@ -1,7 +1,7 @@
import { abortEditing, createEditForm } from "./sharedUI.js";
import { handleResponse } from "./helper.js";
export async function editElement({ element, fields, apiCall, id, onSuccess, errmsg }) {
export async function editElement({ element, fields, apiCall, id, onSuccess, onFailure, errmsg }) {
const formElement = createEditForm(fields);
element.replaceWith(formElement);
@@ -14,13 +14,18 @@ export async function editElement({ element, fields, apiCall, id, onSuccess, err
let formData = Object.fromEntries(new FormData(formElement));
let result = await handleResponse(apiCall(id, formData), errmsg);
abortEditing(formElement, element);
if (result) onSuccess(formData);
if (result.ok) {
abortEditing(formElement, element);
onSuccess(formData);
} else {
onFailure(result);
}
});
formElement.addEventListener("click", (e) => {
if (e.target.matches(".editForm__buttonAbort")) abortEditing(formElement, element);
if (e.target.matches(".editForm__abortButton")) abortEditing(formElement, element);
})
formElement.addEventListener("keydown", (e) => {
+22 -14
View File
@@ -1,22 +1,30 @@
const errorDiv = document.createElement("div");
const errorSpan = document.createElement("span");
let errorTimer;
export function createErrorSpan(msg) {
const span = document.createElement("span");
span.textContent = msg;
span.classList.add("error-message");
return span;
}
export function showError(msg) {
errorSpan.textContent = msg;
errorDiv.hidden = false
export function showError({ highlightFields, elementForMessage, message }) {
highlightFields.forEach(field => {
field.classList.add("is-invalid");
});
const errSpan = createErrorSpan(message);
elementForMessage.append(errSpan);
let errorTimer;
clearTimeout(errorTimer);
errorTimer = (setTimeout(() => {
errorDiv.hidden = true;
errorSpan.textContent = "";
}, 3000));
}
highlightFields.forEach(field => {
field.classList.remove("is-invalid");
})
export function initErrors(element) {
errorDiv.hidden = true;
errorDiv.append(errorSpan);
elementForMessage.removeChild(errSpan);
}, 5000));
element.append(errorDiv);
}
@@ -1,21 +1,19 @@
import { showError } from "./error.js";
export async function handleResponse(promise, errorMsg) {
const response = await promise;
//weil 204 kein body ist un damit unparsbar, geben wir vorab true zurück
if (response.status == 204) {
return true;
return { ok: true, json: null };
}
const json = await response.json();
if (!response.ok) {
showError(json.msg);
console.error(errorMsg);
return false;
return { ok: false, json: json };
}
return json;
return { ok: true, json: json };
}
@@ -16,15 +16,16 @@ export function createEditForm(fields) {
});
abortButton.setAttribute("type", "button");
abortButton.classList.add("editForm__buttonAbort");
abortButton.classList.add("editForm__abortButton");
abortButton.textContent = "Abbrechen";
saveButton.textContent = "Speichern";
saveButton.classList.add("editForm__buttonSave");
saveButton.classList.add("editForm__saveButton");
saveButton.setAttribute("type", "submit");
editForm.append(...inputs, saveButton, abortButton);
editForm.classList.add("editForm");
return editForm;
}
+4 -6
View File
@@ -15,9 +15,9 @@ xmlns:th="http://www.thymeleaf.org">
<nav class="nav">
<ul class="nav__links">
<li class="nav__link"><a href="/">Alle Einkaufslisten</a></li>
<li class="nav__dropdown">Listen &gt;
<li class="nav__dropdown">Listen
<ul class="nav__dropdownMenu">
<li th:each="list : ${lists}" th:data-id="${list.id}" class="navbar__list">
<li th:each="list : ${lists}" th:data-id="${list.id}" class="nav__list">
<a th:text="${list.listName}" th:href="@{/list/{id}(id=${list.id})}"></a>
</li>
</ul>
@@ -27,15 +27,13 @@ xmlns:th="http://www.thymeleaf.org">
<div class="index">
<form class="addListForm">
<input type="text" name="listName" required />
<input class="addListForm__inputName" type="text" name="listName"/>
<button type="submit">Neue Liste erstellen</button>
</form>
<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>
+2 -2
View File
@@ -13,9 +13,9 @@
<nav class="nav">
<ul class="nav__links">
<li class="nav__link"><a href="/">Alle Einkaufslisten</a></li>
<li class="nav__dropdown">Listen &gt;
<li class="nav__dropdown">Listen
<ul class="nav__dropdownMenu">
<li th:each="list : ${lists}" th:data-id="${list.id}" class="navbar__list">
<li th:each="list : ${lists}" th:data-id="${list.id}" class="nav__list">
<a th:text="${list.listName}" th:href="@{/list/{id}(id=${list.id})}"></a>
</li>
</ul>