Compare commits

Author SHA1 Message Date
Technoir 302419802b imports 2026-10-15 12:15:39 +02:00
Technoir 3f5856c86b errmsg hinzugefügt 2026-10-15 12:15:29 +02:00
Technoir 8c347d124a async und await fetch unnötig 2026-10-15 12:15:15 +02:00
Technoir 0b69384fb7 dead code entfernt 2026-10-15 11:38:49 +02:00
Technoir 2d19d9a06f editElement nach shared/editing.js ausgelagert 2026-10-15 11:38:46 +02:00
Technoir 70338266f2 dead code entfert 2026-10-15 11:38:27 +02:00
Technoir 00ac28aebf wsp 2026-10-15 11:38:19 +02:00
Technoir c9c38f07ad sharedAPI gelöscht 2026-10-15 11:38:07 +02:00
Technoir 7af099f022 falsche Klammern korrigiert 2026-10-14 20:34:23 +02:00
Technoir b4eb339657 refactor 2026-10-14 20:19:41 +02:00
Technoir 79443930e2 editElement neue Paraemter eingefügt
das coole an dem neuen model ist, dass wir heade rund das element auch
so bearbeiten können mit einer funktion
2026-10-14 20:19:35 +02:00
Technoir 08315359c7 editElement neue Parameter eingefügt 2026-10-14 20:19:00 +02:00
Technoir b3ec0257d5 editElement mit einem {} objekt ersetzt
Erlaubt es uns besser und übersichtlicher zu parametisieren, da
ansonsten zu übersichtlich
2026-10-14 20:18:41 +02:00
Technoir 1dd335e166 generishce editElement funktion implementiert 2026-10-14 19:54:35 +02:00
Technoir f33b979bcb impots 2026-10-14 19:54:23 +02:00
Technoir 69389afdb7 artefakt 2026-10-14 19:54:20 +02:00
Technoir 593c540d81 spezifische editfunktionen durch generische ersettzt 2026-10-14 19:54:06 +02:00
Technoir dfff051b55 editCateogry entfern 2026-10-14 19:53:27 +02:00
10 changed files with 89 additions and 187 deletions

No files matched your search

@@ -1,4 +1,3 @@
export class ShoppingCategory extends HTMLElement {
static get observedAttributes() { return ["category-name"] }
@@ -1,8 +1,8 @@
export async function addList(listName) {
return await fetch(`/api/lists`,
export function addList(listName) {
return fetch(`/api/lists`,
{
method: "POST",
body: JSON.stringify({ listName: listName }),
@@ -12,14 +12,14 @@ export async function addList(listName) {
}
export async function deleteList(id) {
return await fetch(`/api/lists/${id}`, { method: "DELETE" });
export function deleteList(id) {
return fetch(`/api/lists/${id}`, { method: "DELETE" });
}
export async function editList(listID, toChange) {
return await fetch(`/api/lists/${listID}`,
export function editList(listID, toChange) {
return fetch(`/api/lists/${listID}`,
{
method: "PATCH",
body: JSON.stringify(toChange),
@@ -1,5 +1,5 @@
export async function addCategory(listID, categoryName) {
return await fetch(`/api/categories`,
export function addCategory(listID, categoryName) {
return fetch(`/api/categories`,
{
method: "POST"
, body: JSON.stringify({ "categoryName": categoryName, "listID": listID })
@@ -7,9 +7,8 @@ export async function addCategory(listID, categoryName) {
});
}
export async function editCategory(categoryID, toChange) {
return await fetch(`/api/categories/${categoryID}`,
export function editCategory(categoryID, toChange) {
return fetch(`/api/categories/${categoryID}`,
{
method: "PATCH",
body: JSON.stringify(toChange),
@@ -17,6 +16,6 @@ export async function editCategory(categoryID, toChange) {
});
}
export async function deleteCategory(deleteAllItems, categoryID) {
return await fetch(`/api/categories/${categoryID}?deleteAllItems=${deleteAllItems}`, { method: "DELETE" });
export function deleteCategory(deleteAllItems, categoryID) {
return fetch(`/api/categories/${categoryID}?deleteAllItems=${deleteAllItems}`, { method: "DELETE" });
}
@@ -1,7 +1,5 @@
export async function addItem(listID, itemName, itemAmount, categoryID) {
return await fetch(`/api/items`,
export function addItem(listID, itemName, itemAmount, categoryID) {
return fetch(`/api/items`,
{
method: "POST",
body: JSON.stringify({
@@ -14,15 +12,14 @@ export async function addItem(listID, itemName, itemAmount, categoryID) {
});
}
export function deleteItem(itemID) {
export async function deleteItem(itemID) {
return await fetch(`/api/items/${itemID}`, { method: "DELETE" });
return fetch(`/api/items/${itemID}`, { method: "DELETE" });
}
export async function editItem(itemID, toChange) {
return await fetch(`/api/items/${itemID}`,
export function editItem(itemID, toChange) {
return fetch(`/api/items/${itemID}`,
{
method: "PATCH",
body: JSON.stringify(toChange),
@@ -31,10 +28,6 @@ export async function editItem(itemID, toChange) {
}
export async function deleteAllDoneItems(listID) {
return await fetch(`/api/lists/${listID}/items?done=true`, { method: "DELETE" });
export function deleteAllDoneItems(listID) {
return fetch(`/api/lists/${listID}/items?done=true`, { method: "DELETE" });
}
@@ -1,5 +1,3 @@
export function addCategory(categoryAsJSON) {
const list = document.querySelector(".displayedList");
const category = document.createElement("shopping-category");
@@ -29,36 +27,4 @@ export function deleteCategory(deleteAllItems, categoryID) {
}
category.remove()
document.querySelector(`option[value='${categoryID}']`).remove();
}
export function createEditCategoryForm(categoryName) {
const editForm = document.createElement("form");
const inputName = document.createElement("input");
const abortButton = document.createElement("button");
const saveButton = document.createElement("button");
inputName.setAttribute("name", "newCategoryName");
inputName.setAttribute("value", categoryName);
inputName.classList.add("editForm__name");
abortButton.setAttribute("type", "button");
abortButton.classList.add("editForm__buttonAbort");
abortButton.textContent = "Abbrechen";
saveButton.textContent = "Speichern";
editForm.append(inputName, saveButton, abortButton);
return editForm;
}
export function editCategory(id, formElement, formData, categoryDiv) {
let newName = formData.newCategoryName;
categoryDiv.querySelector(".category__name").textContent = newName;
formElement.replaceWith(categoryDiv);
document.querySelector(`option[value='${id}']`).textContent = newName;
}
@@ -18,47 +18,6 @@ export function addItem(itemAsJSON) {
list.append(li);
}
export function createEditItemForm(currentName, currentAmount) {
const editForm = document.createElement("form");
const inputName = document.createElement("input");
const inputAmount = document.createElement("input");
const abortButton = document.createElement("button");
const saveButton = document.createElement("button");
inputName.setAttribute("name", "newItemName");
inputName.setAttribute("value", currentName);
inputName.classList.add("editForm__name");
inputAmount.setAttribute("name", "newItemAmount");
inputAmount.setAttribute("value", currentAmount);
inputAmount.classList.add("editForm__amount");
abortButton.setAttribute("type", "button");
abortButton.classList.add("editForm__buttonAbort");
abortButton.textContent = "Abbrechen";
saveButton.textContent = "Speichern";
saveButton.setAttribute("type", "submit");
editForm.append(inputName, inputAmount, saveButton, abortButton);
return editForm;
}
export function editItem(shoppingItemElement, formElement, formData) {
let newName = formData.itemName;
let newAmount = formData.itemAmount;
shoppingItemElement.setAttribute("item-name", newName);
shoppingItemElement.setAttribute("item-amount", newAmount);
formElement.replaceWith(shoppingItemElement);
}
export function deleteItem(li) {
li.remove();
}
+39 -77
View File
@@ -4,7 +4,8 @@ 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 { abortEditing, createEditForm, editElement } from "../../shared/sharedUI.js";
import { editElement } from "../../shared/editing.js";
import * as sharedUI from "../../shared/sharedUI.js";
const addCategoryForm = document.querySelector(".addCategoryForm");
const addItemForm = document.querySelector(".addItemForm");
@@ -26,80 +27,10 @@ async function addCategory(categoryName) {
}
async function editElement(elementToEdit, fields, apiPath, errmsg) {
const formElement = sharedUI.createEditForm(fields);
elementToEdit.replaceWith(formElement);
formElement.querySelector("input").focus();
formElement.addEventListener("submit", async (event) => {
event.preventDefault();
let formData = Object.fromEntries(new FormData(formElement));
//erstellen einer Liste mit attr-name : Wert des neuen Attributes
const editedFields = Object.fromEntries(
fields.map((f) => [f.attr, formData[f.name]])
);
let result = await handleResponse(sharedAPI.editElement(apiPath, formData), errmsg);
if (result) sharedUI.editElement(elementToEdit, editedFields);
sharedUI.abortEditing(formElement, elementToEdit);
});
formElement.addEventListener("click", (e) => {
if (e.target.matches(".editForm__buttonAbort")) sharedUI.abortEditing(formElement, elementToEdit);
})
formElement.addEventListener("keydown", (e) => {
if (e.key == "Escape") sharedUI.abortEditing(formElement, elementToEdit);
});
}
function editCategory(categoryElement, id) {
//erstellen das Forms
//wir nutzen für das Element nur das div, weil sonst alle items mitgelöscht werden würden
let categoryName = categoryElement.querySelector(".category__name").textContent;
const formElement = createEditForm([
{ name: "categoryName", value: categoryName, class: "editForm__name" }
]);
categoryElement.replaceWith(formElement);
formElement.querySelector(".editForm__name").focus();
formElement.addEventListener("submit", async (event) => {
event.preventDefault();
let formData = Object.fromEntries(new FormData(formElement));
console.log(`editing catogory ${id} with ${formData}`)
const result = await handleResponse(api.editCategory(id, { categoryName: formData.categoryName }), `error editing category`);
if (result) {
editElement(categoryElement, { "category-name": formData.categoryName });
document.querySelector(`option[value='${id}']`).textContent = formData.categoryName;
abortEditing(formElement, categoryElement);
}
});
formElement.addEventListener("click", (e) => {
if (e.target.matches(".editForm__buttonAbort")) abortEditing(formElement, categoryDiv);
});
formElement.addEventListener("keydown", (e) => {
if (e.key == "Escape") abortEditing(formElement, categoryDiv);
});
}
async function deleteItem(li) {
const json = await handleResponse(api.deleteItem(li.dataset.id), `error deleting item`);
console.log(`deleting item ${li.dataset.id}`)
const json = await handleResponse(api.deleteItem(li.dataset.id), `error deleting item`);
if (json) ui.deleteItem(li);
}
@@ -153,12 +84,43 @@ function init() {
} else if (event.target.closest(".item__text")) {
const shoppingItemElement = event.target.closest("shopping-item");
editItem(shoppingItemElement, shoppingItemElement.closest("li").dataset.id);
const id = event.target.closest("li").dataset.id;
let itemName = shoppingItemElement.querySelector(".item__itemName").textContent;
let itemAmount = shoppingItemElement.querySelector(".item__itemAmount").textContent;
/*
* { name: "", value: "", class: "", attr: ""}
*/
editElement({
element: shoppingItemElement, apiCall: api.editItem, id: id,
fields: [
{ name: "itemName", value: itemName, class: "editForm__name", attr: "item-name" },
{ name: "itemAmount", value: itemAmount, class: "editForm__amount", attr: "item-amount" }
]
, onSuccess: (f) => { sharedUI.setElementAttributes(shoppingItemElement, { "item-name": f.itemName, "item-amount": f.itemAmount }) }
, errmsg: `konnte Item nicht bearbeiten`
});
} else if (event.target.closest(".category__name")) {
const li = event.target.closest("li");
editCategory(event.target.closest("shopping-category"), li.dataset.id);
const categoryElement = event.target.closest("shopping-category");
const categoryHeader = event.target.closest(".category__header");
const id = event.target.closest("li").dataset.id;
let categoryName = categoryElement.querySelector(".category__name").textContent;
editElement({
element: categoryHeader, apiCall: api.editCategory, id: id,
fields: [
{ name: "categoryName", value: categoryName, class: "editForm__name", attr: "category-name" },
]
, onSuccess: (f) => {
sharedUI.setElementAttributes(categoryElement, { "category-name": f.categoryName })
document.querySelector(`option[value='${id}']`).textContent = f.categoryName;
}
, errmsg: `konnte Kategorie nicht bearbeiten`
});
}
});
@@ -172,8 +134,8 @@ function init() {
});
console.log("javascript initialized!");
return 0;
}
document.addEventListener('DOMContentLoaded', init);
@@ -0,0 +1,29 @@
import { abortEditing, createEditForm } from "./sharedUI.js";
import { handleResponse } from "./helper.js";
export async function editElement({ element, fields, apiCall, id, onSuccess, errmsg }) {
const formElement = createEditForm(fields);
element.replaceWith(formElement);
formElement.querySelector("input").focus();
formElement.addEventListener("submit", async (event) => {
event.preventDefault();
let formData = Object.fromEntries(new FormData(formElement));
let result = await handleResponse(apiCall(id, formData), errmsg);
abortEditing(formElement, element);
if (result) onSuccess(formData);
});
formElement.addEventListener("click", (e) => {
if (e.target.matches(".editForm__buttonAbort")) abortEditing(formElement, element);
})
formElement.addEventListener("keydown", (e) => {
if (e.key == "Escape") abortEditing(formElement, element);
});
}
@@ -1,6 +1,5 @@
import { showError } from "./error.js";
export async function handleResponse(promise, errorMsg) {
const response = await promise;
@@ -1,9 +1,7 @@
export function abortEditing(formElement, old) {
formElement.replaceWith(old);
}
export function createEditForm(fields) {
const editForm = document.createElement("form");
const abortButton = document.createElement("button");
@@ -31,10 +29,8 @@ export function createEditForm(fields) {
return editForm;
}
export function editElement(element, attributes) {
export function setElementAttributes(element, attributes) {
for (const [attr, val] of Object.entries(attributes)) element.setAttribute(attr, val);
}