From 6233d27d7bc7c9371342b01c827e521db441a6d6 Mon Sep 17 00:00:00 2001 From: Tim Aldinger Date: Wed, 14 Oct 2026 15:49:13 +0200 Subject: [PATCH] fehlerhandling mit handleResponse ersetzt --- .../resources/static/js/pages/list/list.js | 59 ++++--------------- 1 file changed, 10 insertions(+), 49 deletions(-) diff --git a/src/main/resources/static/js/pages/list/list.js b/src/main/resources/static/js/pages/list/list.js index 2a9ef86..1a13757 100644 --- a/src/main/resources/static/js/pages/list/list.js +++ b/src/main/resources/static/js/pages/list/list.js @@ -13,34 +13,16 @@ 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 response = await api.addItem(currentList.dataset.id, itemName, itemAmount, categoryID); - const json = await response.json(); - - if (!response.ok) { - console.error(`Error, item couldnt be created ${json}`); - showError(json.msg); - return; - } - - console.log(`rendering frontend item: ${json}`); - ui.addItem(json); + const json = await handleResponse(api.addItem(currentList.dataset.id, itemName, itemAmount, categoryID), `item couldnt be created!`); + if (json) ui.addItem(json); } async function addCategory(categoryName) { console.log(`adding category ${categoryName}`) - const response = await api.addCategory(currentList.dataset.id, categoryName); - const json = await response.json(); + const json = await handleResponse(api.addCategory(currentList.dataset.id, categoryName), `category couldnt be added!`); + if (json) ui.addCategory(json); - if (!response.ok) { - console.error(`Error, category couldnt be created ${json}`); - showError(json.msg); - - return; - } - - ui.addCategory(json); - return 0; } //shoppingItem ist immer das li mit der id @@ -140,43 +122,22 @@ function editCategory(categoryElement, id) { } async function deleteItem(li) { - const response = await api.deleteItem(li.dataset.id); + const json = await handleResponse(api.deleteItem(li.dataset.id), `error deleting item`); - if (!response.ok) { - const json = await response.json(); - console.error(`Error deleting item ${json}`); - showError(json.msg); - return; - } - - ui.deleteItem(li); + if (json) ui.deleteItem(li); } async function deleteCategory(deleteAllItems, categoryID) { console.log(`deleting category with allItems: ${deleteAllItems} id: ${categoryID}`); - const response = await api.deleteCategory(deleteAllItems, categoryID); + const json = await handleResponse(api.deleteCategory(deleteAllItems, categoryID), `error deleting category`); - if (!response.ok) { - const json = await response.json(); - console.error(`Error deleting category ${json}`); - showError(json.msg); - return; - } - ui.deleteCategory(deleteAllItems, categoryID); + if (json) ui.deleteCategory(deleteAllItems, categoryID); } async function deleteAllDoneItems() { console.log(`deleting all done items...`) - const response = await api.deleteAllDoneItems(currentList.dataset.id); - - if (!response.ok) { - const json = await response.json(); - console.error(`Error deleting all done items ${json}`); - showError(json.msg); - - return; - } - ui.deleteAllDoneItems(); + const json = await handleResponse(api.deleteAllDoneItems(currentList.dataset.id), `error deleting all done items`); + if (json) ui.deleteAllDoneItems(); } function init() {