fehlerhandling mit handleResponse ersetzt
This commit is contained in:
1 parent
76a4b201b8
commit
6233d27d7b
1 file changed
+10
-49
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user